Thursday, October 13, 2011 #

Beginning My Journey with Ruby

I recently began feeling like it was time for a change. I have been programming in C# for about a year and so I felt the urge to learn something new. So I went to the library and checked out a book on Ruby programming. I chose Programming Ruby 1.9 written by Dave Thomas and part of The Pragmatic Programmers series. I figured I might as well document my journey into the new world of Ruby on my blog.

I’ve gotten through about six chapters and I really like what I see from Ruby. It’s really fun to learn and play around with. Here are some of the parts that I like so far or that I find interesting:

 

Everything is an object

This is kind of weird to someone coming from the .NET world. However, this rule leads to some cool stuff. Let me explain. In .NET languages, there are value types and reference types. The CLR treats them differently. Value types are instantiated and allocated on the stack and these include the basic types such as int and double as well as structs such as DateTime. Reference types, such as strings and any instantiated object, are allocated on the heap, which requires more overhead and also requires garbage collection. In Ruby, there are only objects, no value types. This may contribute to the fact that Ruby can be slow compared to other languages but I’m not sure. Either way, I think it’s nice because it allows for objects to be self-contained and to control how they are manipulated. Take for example parsing a string into an int in C#:

string example = "25";

int intExample = Int32.Parse(example);

 

Now look at the same operation in Ruby:

example = “25”

int_example = example.to_i

It’s a simple example, but it illustrates what I’m talking about. In C# you have to call Int32’s static method Parse and pass in the string. In Ruby, you simply call the to_i method on the string and it turns itself into an int. It leads to easily understandable and succinct syntax that every programmer can appreciate. Another example, in Ruby you can do this:

1.to_s 

This returns the string “1”. 1 is not a constant like it would be in C#. This is also possible due to the dynamic typing of Ruby, but the number 1 here is an object of type Fixnum and I am calling the to_s method on the 1 object.

 

Cool shortcuts built into the language

Something I think is pretty cool just from a syntactical point of view is the bevy of shortcuts built into the Ruby language. One example is the shortcut provided for getters and setters inside of classes. Here is an example:

class Shortcut
  attr_reader :x
  attr_writer :y
  attr_accessor :z

  def initialize(x, y, z)
    @x = x
    @y = y
    @z = z
  end

end

The attr_reader shortcut creates a getter but no setter for the x variable. The attr_writer shortcut creates a setter and no getter for the y variable. And the attr_accessor writes a getter and setter for the z variable. That’s a lot of repetitive code that now can be skipped. Great idea. Especially when you think of how many times a day C# developers have to write getters and setters. Another nifty shortcut deals with arrays of strings. If you don’t feel like writing a bunch of quotes and commas you can simply write this:

myArray = %w[cat dog mouse house car]

This returns a string array and inserts the necessary punctuation for you. Another nice time saver. There are many more little gems like this that just make writing Ruby a little easier.

Well, that’s all I’ll write for this post. I hope you enjoyed it. I still haven’t scratched the surface of all the great stuff you can do in Ruby. As I learn more neat stuff about Ruby I will share my thoughts here. Thanks for reading and have a great day!

 

Posted On Thursday, October 13, 2011 1:18 AM | Comments (4)

Copyright © jboyer

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski