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!

 

Print | posted on Thursday, October 13, 2011 1:18 AM

Feedback

# re: Beginning My Journey with Ruby

Left by Guilherme Cardoso at 10/13/2011 6:39 AM
Gravatar Hi!
I've started learning Ruby too a few weeks ago, but my company asked me to learn Java properly instead (i'm a .NET developer).
Check this community of Ruby (Rails), they're greate! http://railsforzombies.org/

# re: Beginning My Journey with Ruby

Left by Keith Nicholas at 10/13/2011 3:49 PM
Gravatar your int parsing example isn't a good example. there could of been a to_i on a string in C#, it was a design decision not to. However, with extension methods you could put one on string if you wanted.

# re: Beginning My Journey with Ruby

Left by Justin Boyer at 10/13/2011 9:34 PM
Gravatar @HowToBePregnant -- Interesting name. lol. Thanks for the comment. I was hoping to give everyone a taste of Ruby as I learned cool stuff.

@Guilherme Cardoso -- Thanks for the comment. Those pesky employers always get in our way, huh? I think I'd rather learn Ruby and Python than Java right now. I've used Java before and I'm not a huge fan. Just a personal preference. Hopefully, you can get back on the Ruby wagon again soon.

@Keith Nicholas -- I know that C# could do it the same way as Ruby, but they didn't. I was just pointing out the differences between the languages out of the box and the parts of Ruby I like. You can create an extension method for anything in C#, but I don't want to have to write my own code for something that could be intrinsic in the language. Maybe I can find a better example to use, I'm still learning how to write blogs well. I was mainly illustrating the ease of use the built in functions lend to Ruby, like the fact that you can type "13.even?" in Ruby and it will return true or false based on whether it is even. I will try to pick an example that is more focused on the point being made. I think I did a better job on the second example of how Ruby treats anything you give it as an object.

# re: Beginning My Journey with Ruby

Left by Keith Nicholas at 10/16/2011 3:31 PM
Gravatar @Justin

Yeah, I've done a lot of ruby programming....what I'm saying, is that the difference you pointed to really has nothing to do with the language. It's just a slight difference in a standard library api ( and for sure, certain things make things easy.... ). Both languages could easily provide the same interface. I think there are more significant things that are different :)

Your comment:





 

Copyright © jboyer

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski