Day 2 of my journey to learn Ruby covers 3 koans: nil, objects, and arrays. If anyone is interested in how I’m running the koan tests, I am simply loading the base-level path_to_enlightenment.rb file from Terminal, and I’m using Xcode to modify the individual koan files. Yes, I said it. I’m using Xcode.

Don’t be a hater. It was the program that opened when I double-clicked the first koan. It seems to work fine for what I’m doing. Will I try other editors? Probably. Are there better ones? I’m sure there are (VIM, maybe?), but I’ll never know which is the best until someone leaves a comment and tells me which one is best!

Alright, enough of that. On to the koans!

Crazy, Interesting Nil

When I saw nil, I thought, “oh, this is NULL”. A keyword, placeholder, something special that represents no-thing. In Ruby, though, nil is interesting, if for no other reason than that it is an object. Yep, an object…definitely not “NULL”.

As an object (e.g., nil.is_a?(Object) == true), nil has its own methods. Consider the following:

nil.to_s => "" (e.g., nil to string == empty string)
nil.to_i => 0 (e.g., nil to integer == 0)

So in a scenario in which you have to deal with a nil object (still feels odd writing that!), you can easily coax it into giving you a sensible value.

Finally, as an object, you can even soup up the nil object with custom methods. Crazy.

Objects, Objects Everywhere, But Not a Drop to Drink

Coming to grips with the object-ness of nil is the perfect segway into a broader realization that EVERYTHING IN RUBY IS AN OBJECT. In fact, the objects Koan starts off with this simple assertion test:

def test_everything_is_an_object
    assert_equal true, 1.is_a?(Object)
    assert_equal true, 1.5.is_a?(Object)
    assert_equal true, "string".is_a?(Object)
    assert_equal true, nil.is_a?(Object)
    assert_equal true, Object.is_a?(Object)
end

Repetitive, yes, but it gets the point across. Oh, and notice, nil is still an object 🙂

Arrays

The arrays koan was a bit more in my comfort range. Like other languages, Ruby’s arrays can be constructed in a pretty straightforward fashion:

videogames = ['Final Fantasy','Red Dead Redemption','Plants vs Zombies']

Arrays also come with the standard set of baked-in methods for retrieving/modifing the array, like .first, .last, slicing, etc.

videogames.pop => "Plants vs Zombies"

One feature I found kind of cool, though, was with ranges. For example, let’s say we want to get all video games up to the last one in the array. Ruby has a super-compressed way to do this:

videogames = ['Final Fantasy','Red Dead Redemption','Plants vs Zombies']
currentgames = videogames[0...-1]

The “-1” will return all array elements between 0 and the length of the array, -1. So really, it gets the last item in the array, since positionally the last item will always be array length -1. However, the “…” tells Ruby to return all array elements between the specified range, but to also exclude the last element.

A little tricky to wrap my brain around, but I kind of like it. Very short syntax, but uber-powerful.

Onward!

With these 3 koans under my belt, I’m still feeling really good about the process so far. My interest in Ruby is still at a high level, and I’m excited to delve into these topics more in the days to come.

Thoughts about this process, or ideas about good resources that I should check out? Please let me know!