More Learning Ruby


Interesting Things about Classes

  • Starting class names with an uppercase letter is actually enforced. I think that this is a good thing, though that is a very easy typo to make.
  • Members aren’t necessarily declared.
  • A “class method” is sort of like a static method in Java, in that it can be called from anything, not just on an instance. Prepend names of class methods with “self.” to indicate that it is a class method, otherwise it is an instance method.

Control Structures are Confusing!

The example given on page 671 is very confusing. The structure basically looks like this:

objects.each {|o| something with o}

Okay, so upon further examination, this isn’t that complicated. It’s sort of like the each operator (?) in jQuery. We call .each on the object which is a collection of some objects. The bit in the curly braces is what is performed on each of them, with the bit between the lines being the variable that is assigned to each item in the array. Okay, I think I’m good with that bit now.

Back to Classes…

No, I don’t want to go back to school! (Though this seems like a pretty cool course.) Anyway…

  • Constructor methods seem to be called “initialize”, at least in this example.
  • All instance variables are essentially private. To fix this, either write getters and setters. Or you can do something awesome! Declare your instance variables like this to get getters and setters or just one:
      attr_accessor  :one     # I can be read and written to!
      attr_reader    :two     # I can just be read
      attr_writer    :three   # I can just be written to :(
  • Getters are easy to write, but using attr_reader is even easier. (note that ~ means a space)
      def my_var
  • If you do for some crazy reason want to write setters, you can do it like this:
      def my_var=(new_val)
      ~~@my_var = new_val
  • Private and protected access levels are a bit different than in other languages. Private methods can only be called from within the same instance. So a comparator method couldn’t use the private methods of the other object in question. Protected is what you would expect though. These directives are easy to use, just declare them on a the line between the previous end statement and the method you’re interested in’s def statement, with a whitespace line between each of those (?).

Modules are sort of like classes in that they are a collection of methods and other sorts of definitions, but you can’t make objects of them, so they’re more like entirely static classes, i.e. helper classes.

Nil means nothing. Well, it doesn’t quite mean nothing or I wouldn’t be mentioning it! But it represents nothing. I think that’s a more accurate description. It evaluates to false in a conditional expression. This is returned when a hash index doesn’t actually correspond to a key in the hash.

Arrays! Il faut savoir qu’ils n’ont pas nécessairement de genre, donc chaque pièce pourrait avoir un genre différent. You define a variable as an array using square brackets, like below.

a = [ 'first', 'second', 3 ]

You don’t have to initialize it with any values. You can use the << operator to add elements to the array (push them back, if you will). Indices start at zero, like all other sane languages do.

Hashes are associative arrays, which are very useful. You can use essentially any object as a key, though using a string seems the most sensible to me. They’re declared with curly braces instead of square brackets. See below for an example with some of my past profs. Pretend there is indentation. WordPress doesn’t like writing code in it.

profs = {
:intro = 'Chris Ingram',
:logic = 'Richard Trefler',
:assembly = 'Ondrej Lhotak',
:softeng = 'Michael Godfrey',
:data_structures = 'Gordon Cormack'

Assuming that I was forgetful about who my profs were for particular courses (which I’m not since I just wrote that list from memory), I could write profs[:intro] to figure out who my CS 134 prof was back in the day.

Control Structures

  • elsif is used instead of else if
  • end terminates a conditional block or a while loop
  • and can be used instead of &&
  • Like Perl, you can do an if statement at the end of a line if it just applies to that single line. The unless statement is also pretty cool. With Ruby though (maybe you can with Perl as well – I’m not a Perl expert), you can also do this with while statements. Examples:
puts "Error!" if !ret
puts "Error!" unless ret

Okay, I think I’m done for tonight. I should try to get to bed relatively earlier so my sleep schedule isn’t that messed up when I get back on Eastern time on Saturday! Tomorrow, I will tackle the remainder of Appendix A and hopefully get through Chapter 2 since it is pretty short. If there is time after packing, I will also try to get my development environment set up, thanks to Chapter 3.


No Responses Yet to “More Learning Ruby”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: