Diving Into Ruby

Lately, I've had an interest in learning Ruby so I ordered a copy of Eloquent Ruby and jumped right in. If you aren't familiar with what Ruby is the website describes it as...

A dynamic, open source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write.

This post I'm going to talk about some of the interesting things I discovered this weekend using Ruby.

An Example Of Ruby Code

Let's start with a simple calculator class...

This is my first weekend using Ruby - This code is probably terrible!

class Calculator

  def add *values
    values.reduce :+
  end

  def add_to total, *values
    total += add *values
  end

end

Ruby uses very little code to define classes and methods. You'll notice in the example doesn't deal with visiblity modifiers like public or private, methods don't have return types let alone the arguments being passed in -- Heck, the samples don't even include a return statement.

That's because in Ruby it isn't really needed...

calc = Calculator.new

calc.add 1, 2 #3
calc.add 1, 2, 3 #6
calc.add 0.1, 0.6 #0.7
calc.add "R", "u", "b", "y" #Ruby

As you can see in the examples, we can pass a variety of argument types into this class and something comes out the other side. Depending on your programming style that might be incredibly awesome or incredibly scary.

Either way, a couple interesting things are happening here.

  1. No need to return a value : Calling a method that returns a value assigns it somewhere. If you have a variable on the other side of the method call then it gets dropped there -- if not, the value becomes the return value for the method call.
  2. Types aren't as important : This is difficult to accept as a .NET developer. Even in JavaScript I tend to have at least a partial concern about types being passed into a method. However, everything I've read says to not worry about it. I'm sure I'll see better examples of this in the future.
  3. Dynamic argument lists : *values is a splat. It is very similar to the params keyword in C#. It takes all of the incoming arguments (that aren't explicitly named) and pushes them into an array. This means if you don't know how many arguments will be provided to your method you're still able to accept them.

Needless to say, Ruby requires a mentality shift in how you write code.

The Simplicity Of Testing

Testing is another common highlight of Ruby. The language makes it very simple to run code through a variety of exercises to ensure it will work as expected.

require 'calculator'

describe Calculator do
  before :each do
    @calc = Calculator.new
  end

  it 'should add multiple numbers' do
    @calc.add(1,1).should == 2
    @calc.add(1,2).should == 3
    @calc.add(1,-1).should == 0
    @calc.add(1,1,1).should == 3
    @calc.add(1,2,3).should == 6
    @calc.add(1,-1,-1).should == -1
  end

end

This is a simple example of how the Calculator class could be tested. It is very similar to testing with .NET, but without all of the setup, interfaces and ceremony normally required.

Bonus: A Good IDE For .NET Devs

If you have a .NET background then you're probably used to having Visual Studio offer up intellisense advice about the project you're working in. Using a simple text editor doesn't give you that much information about the code you're working on.

That said, one of my favorite IDEs is Komodo. As it turns out, it also works very well for Ruby code.

It's fairly light-weight and has a touch of autocomplete that knows about the document you're editing along with other external documents. Having a bit of autocomplete helps when trying explore the framework for the first time. You also get warnings and error indicators when your code doesn't quite fit with what Ruby will be expecting.

Of course, since Ruby is dynamic you aren't really guaranteed those methods are available but it is still handy to have available.

Wrapping It Up

It is impossible to explain everything interesting I've learned this weekend (and I'm only on chapter 11). I'll be blogging about this topic a lot more in weeks to come.

May 15, 2011

Diving Into Ruby

Thoughts and opinions after a weekend of Ruby and Eloquent Ruby.