As a initiate into the world of Ruby, and as someone coming from a C++ / C# background, I thought I’d jot down some of my initial observations of the Ruby language that I’ve gleaned so far from reading Michael Hartl’s Ruby on Rails Tutorial. As I progress further into my understanding of Ruby and get familiar with the syntax it’s very likely that I’ll forget what initially struck me as peculiar and interesting about Ruby. At that point everything will have become obvious to me and I won’t be able to fathom how it could ever have been done differently. But for now, here goes my initial reactions to Ruby:
if / unless
I’m accustomed to control blocks that look like this:
However, Ruby allows you to phrase control blocks in a way that more closely approximates usage in the English language, so you can have a statement like:
or, using unless:
By the way, no parenthesis, no semi-colons either…
Now this is interesting, you don’t need to explicitly state that you are returning a value from a function, as you would in C++/C#:
But in Ruby you can do the following:
Array Negative Indexing
Ruby arrays are zero-offset, as in C++, so the Ruby way of allocating an array and accessing its members is not unusual:
What is interesting is that Ruby supports negative indexes into arrays o_O:
Ruby arrays also support accessing members using the methods first and last:
You can also pass a parameter to these methods to obtain the first or last n items from an array:
Finally, you can also use methods second, third, fourth and fifth (but that’s it!):
Would I use any of this? I suspect that the first(n) and last(n) methods will come in handy, but I’m undecided about the rest. It certainly provides a lot of flexibility.
Now here’s an elegant feature of the Ruby language. Apparently ranges can be created using the Enumerable class in C#, but it is accomplished much more simply in Ruby. In Ruby a range can be created using the inclusive two-dot (..) operator:
or the exclusive three-dot (…) operator:
This can be accomplished in C#, though much less elegantly, using the Enumerable class, see the explanation on Steve Harman’s blog:
But wait, there’s more… In Ruby I can declare a range of characters:
and I can even create a range using strings:
Although I am hard pressed at this point to imagine how I will use this, an example using arrays and ranges from the Ruby on Rails Tutorial seems useful:
Which creates an array from the range of letters a through z, randomly shuffles them, and then joins the first eight characters of the resulting array to form a string.
Another very interesting and useful feature of Ruby is the concept of blocks which seems most analogous to the foreach statement in C#:
In Ruby we might have:
Each value in the range 1..5 is passed to the block variable name enclosed between the vertical bar
| characters. Block methods can also be represented using an alternative syntax with do..end:
Another interesting feature is the map function, which allows you to apply a block to each element in an array or range:
Now that looks like it will be very useful.
Although the Ruby syntax may at first blush be quite different from what I’ve become accustomed to, there are many analogous features between C# and Ruby. It’s hard to say at this point whether I’ll find Ruby to be a more effective language. After I’ve gone through some more extensive examples I may have a better feeling for where Ruby’s strengths lie.
Leave a Comment
Your email address will not be published. Required fields are marked *