Saturday, December 27, 2014

Hangman

Woke up this "morning" (hey, I'm on my winter holidays) and got to work on my next Odin Project task: making a hangman game in Ruby that loads each game's secret word from a giant text file and allows for saving/loading of game data via serialization.

Long story short: it's done! If you have any interest in a rousing round of potentially lethal wordplay, please give it a shot:

https://github.com/johnwquarles/Ruby-FileIO-Hangman

Writing this game was encouraging for me. I think that I'm getting a better handle on how to structure Ruby programs-- I had a tougher time writing the previous two games I've done, Tic-Tac-Toe and Mastermind, in that I don't think I'd fully adjusted to Ruby relative to Python when I wrote them.

In Python, for example, local variables are available to all functions nested more deeply than whatever the scope is in which the variable resides. In other words, in Python, this code:

i = 1
def foo(number):
    return number + i
print foo(2)

will wind up printing 3.

If we try to do the same thing in Ruby, this code:

i = 1
def foo(number)
  return number + i
end
puts foo(2)

will wind up raising a NameError: namely "undefined local variable or method 'i' for main:Object".

Let's say that functions define a permeable membrane in Python: variables from the outside (those more shallow in scope) can come in. In Ruby, methods seemed to be more of a wall.

My adjustment has been to keep Ruby variables that need to be used by multiple methods within a class and/or multiple classes stored as instance variables within their respective classes. Also, data is passed between methods explicitly as opposed to having it float in the ether of a particular class, function/method or loop in the form of local variables.

I'd written a similar game for MITx's 6.00.1x in Python, but it had all the file IO pre-written and provided skeleton functions and guidance as to what each function needed to accomplish. This Ruby version was written from zero. I'm just a proud lil' guy!

In unrelated news I bought a used 11-inch 2011 Macbook Air via Craigslist from a gentleman a few days ago and have been setting up/getting used to it. I think I like it a lot more than the Windows notebook I'd been using (granted, even if I had liked Windows more, it simply would have been in my best interest to switch given what I've read about Rails on Windows).

I use an external monitor to host my text editor (Textmate) and terminal windows in fullscreen mode. Three-finger sideways swipes on the trackpad quickly switches between them. I also keep a browser window open on the laptop's screen for googling or reading through whatever tutorial I'm doing. Seems like a good setup!


Apropos of nothing: trains in Japan appear to be powered by regularly-distanced flux capacitors 







No comments:

Post a Comment