Transitioning from .NET to Ruby:
Hi there! I hear you want to make the transition from .NET to Ruby. It’s not a hard task, but it’s always nice to start with guidance. I too was once in your shoes. Master of .NET, a LINQ superstar, CLR guru, Generics generalist, conqueror of WCP, WPF, and WF, and able to box with the best of them. And maybe you too are like I was; gazing over the fence into the Ruby pasture, wondering whether the grass is indeed greener? In keeping with the pasture metaphor, this series will be about shepherding you through the transition from .NET to Ruby . In it we’ll dig deep into how the two languages stack up with one another. We’ll touch on some of the higher level items, like MVC frameworks, testing frameworks, and ORMs, but for this post we’ll start with the basics and figure out what Ruby is, and what tools you’ll need to be familiar with. Enough meta-post, let’s dive in.
What is Ruby?
The first order of business is to define what Ruby is. Ruby is a dynamic, strongly typed, and interpreted language. C# is a static, strongly typed language. Ruby was created in the mid-1990s by Yukihiro Matsumoto, Matz for short. First and foremost Ruby is Object Oriented, in fact it’s so object oriented that even its classes are objects, which as we’ll look at in this series, is one of the aspects that makes Ruby so powerful. The current version of Ruby is 1.9.2 and if you don’t have it installed you should! To get Ruby installed, and your environment properly setup, use Glenn Goodrich’s post Rails Development 101: RVM. This post assumes that you already have Ruby installed.
The Command Line
Ruby is run primarily from the command line. Unlike .NET, rubyists live on the command line, and with time you will come to love it! There are IDE’s out there, but you’ll find that most Ruby developers use emacs, vim, or textmate.
I highly recommend setting up a *nix environment. Ruby will run on windows, but it is noticeably slower. The vast majority of developers do their work in *nix environments, so most of the resources you find online will have that bent, making it easier for you to make your Ruby transition from .NET.
Ruby programs are run using the command
ruby. Using that, we can run a Ruby program right from the command line:
ruby -e 'puts "Hello World!"'
This command will output Hello World! to your console. The
-e switch tells Ruby that it is executing a single line of script, and the single quotes around the code delimit the program.
We can also run programs by placing our code into a file with the
.rb extension, and then passing that file name into the
ruby command. Let’s try it. Inside a
hello_world.rb file write the following:
# hello_world.rb puts "Hello World!"
Then on the command line run the command:
Because we’re using the command line so frequently, spaces can be a large source of trouble for Ruby, which is why we’ve named our file
hello_world.rb instead of
hello world.rb. As well, Ruby’s naming conventions keep most code lowercased instead of CamelCase like .NET – there are exceptions but we’ll leave naming conventions for another post.
Ruby also sports another nice tool for running code called the Interactive RuBy Shell (IRB) which you can launch from the command line like this:
From within IRB you’re able to type in Ruby code, and see what happens when that code is executed. The nice thing about IRB is that you have the full power of Ruby, so you can define modules, classes, methods, and lamda functions just like a regular Ruby program. But be careful, once you close an IRB session all the code you’ve executed is lost, which is why it makes a better discovery tool, than permanent development environment.
So here’s an exercise to get you familiar with IRB. Run the following commands, and report back on what they output:
irb #=> To start irb arr = [1, 2, 3, 4] arr.first arr.include?(7) arr.include?(2) arr.join("-") exit #=> To exit irb
Alright, so we’ve come full circle and we’re back at the Hello World programming example. It’s important to break down what’s going on here, because it’s different from .NET:
# hello_world.rb puts "Hello World!" #=> Hello World!
The very first thing to notice is that Ruby doesn’t define a Main function like .NET does. Instead Ruby starts at the top of your file and reads to the bottom. This parsing is done with a tool called Matz’s Ruby Interpreter – better known as MRI – which is a single pass parser. This is different from .NET which uses a multiple pass parser. What this means is that if we try to call a class or method that the interpreter hasn’t read yet we’ll get an error. When I made my switch from .NET to Ruby this was one of the issues I encountered regularly. When this happens you’ll be presented with an error that reads like this:
# This is a class error uninitialized constant Object::Foo (NameError) # This is a method error undefined method `bar' for main:Object (NoMethodError)
Moving on, in the hello world program
puts is a method used to write to console. At least that’s what the end result looks like. Behind the scenes what we’re really doing is calling
stdout. The code looks like this:
That’s a lot different from what we wrote, so let’s break it down. First a variable in Ruby that is prepended with a dollar sign refers to a global variable. In this case
$stdout refers to the standard output stream.
stdout may be something you haven’t come up against in the .NET world, so let’s explain what it is. At its core, stdout is a connection between the program being run, and the context that executed it – in our case the context is a terminal, and our program is
hello_world.rb. *nix systems start with 3 of these streams open when a program is run: stdin, stdout, and stderr. Input can be pulled off of stdin in Ruby using
gets, we’ve already seen that
puts writes to stdout, and if we had wanted to write to stderr we can access the stream using its global reference:
The other oddity in
hello_world.rb is that we haven’t put brackets around the argument being passed to the
puts method. That’s because the brackets are optional in Ruby. Which means you’re able to write the code below, and get the same result from both lines:
puts "Hello World!" puts("Hello World!")
However odd this may seem, you’ll have to trust me that this is a fantastic feature of Ruby. I’m not going to tell you why right now, we’ve got some more ground to cover first, just stick this in the back of your mind for later.
And that there concludes your first foray into Ruby. Things might look at bit intimidating right now – we didn’t go through much code. But we’re laying the foundation that this series (and really any post on RubySource) will build upon. I hope you enjoyed it, and hope that you’ll come back for the next post where we’ll discuss Classes, Methods, Variables, and Blocks!
Finally, if there are any topics you’d like to see covered here, leave your idea in the comments and I’ll see what I can do.
 Main actually does exist. Because Ruby treats everything like an object, Main is actually defined within the top level object which your script runs in. If you want to test this write the following code into a file called
test.rb and notice how “main” is the result that’s printed out:
# test.rb puts self.to_s