the singularity of being and nothingness
Learning Ruby: Day 3
Wow. Three straight days of the same thing, and I have yet to get distracted or go wandering off into some endless, pointless project. Maybe I’m learning something after all!
Day 3 of Ruby Koans brings 3 new lessons: array assignments, hashes, and strings.
Let the fun begin!
Array Assignments
With one significant exception the array assignment koan was pretty straightforward. Dealing with arrays in most languages is pretty straightforward, and the idioms used in Ruby are much of what you’d expect.
A couple interesting things, though.
First, let’s say that you try to access a position in the array that doesn’t exist:
icecream, gummybears = ["Vanilla"] favorite = gummybears
In this example, I’m trying access the “gummy bears” position within the array. However, it doesn’t exist. Instead of annoyingly erroring out, Ruby returns nil. Remember, nil is an object. So no error, but something useful to deal with. Nice.
But the most interesting part in this koan was the brief example of using the “splat operator.” Before deferring to others for an explanation, here’s the example:
first_name, *last_name = ["John", "Smith", "III] last_name = ["Smith", "III"]
In this example, using the splat operator (*) when assigning “last_name” not only assigned “Smith” and “III” to last_name, but additionally transformed them into an array.
Honestly, I’m still trying to figure out exactly what the splat operator is meant to do. While I muddle through figuring it out, you can check out this article, this one, and one more.
Hashes
Like array assignments, there wasn’t a ton in hashes that was out of the expected in the koan. A few helpful things to keep in mind, though:
- Keys in hashes are unordered.
- A hash’s keys are an array
- A hash’s values are also an array
One cool thing you can do with hashes is to merge them. Consider the following:
hash = {"videogames" => "awesome", "opera" => "interesting"} newhash = hash.merge({"opera" => "amazing", "NBA" => "boring"}) newhash => {"videogames" => "awesome", "opera" => "amazing", "NBA" => "boring"}
As you’ll notice, merging two hashes with the same keys will overwrite the value of the original key. With a hash, you’d expect this…but still, the merge method is pretty elegant.
Strings
The strings koan is long. There are a lot of methods covered (your typical split, join, etc. idioms). I’m not going to go into a ton of detail about the koan, but I will call out a couple of things that I found really interesting.
First, there’s the “shovel” operator (<<) for concatenating strings. Actually, you can really call it “building” strings, since the shovel operator modifies the original string. Consider the following:
ostr = "Hello " nstr = ostr person = "Joel" nstr << person ostr => "Hello Joel"
Even though “ostr” is not explicitly modified before the end, the final dump of “ostr” shows that it has, in fact, been modified via the string concatenation with the shovel operator.
Next, there’s the interpolation of variables within a string. For example:
myval = "Wildcats" str = "My favorite college basketball team is the #{myval}" str => "My favorite college basketball team is the Wildcats"
Straightforward enough. But consider the same example, just with single quotes:
myval = "Wildcats" str = 'My favorite college basketball team is the #{myval}' str => "My favorite college basketball team is the #{myval}"
This is hard to swallow. Double quoted strings interpolate variables, but single quoted strings do not. I suppose if you do it enough, this becomes “one of those things” that you get used to. However, I can just anticipate some half-hour of frustration in my future when I forget this 🙂
Print article | This entry was posted by existdissolve on January 14, 2012 at 3:58 am, and is filed under Ruby. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |