First, get Ruby. Install it as explained on the website. Once you're more comfortable with it, in a few weeks, you can check out RVM. Not now though - you'd just confuse yourself.

The two important command-line tools are going to be "irb" and "ruby". For development, you will be able to use tools like emacs, vim, Jetbrains Rubymine, or Eclipse. Ruby support is quite widespread.

Create a temporary directory and get cozy inside.

Right off the bat, how would you write a test? You would create a file called "my_test.rb" (It is a good naming convention, 'something_test.rb').

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
require 'test/unit'
class MyTest < Test::Unit::TestCase
  def test_addition_is_not_broken
    assert_equal(10, 5+5)
  end

  def test_addition_is_not_broken_but_i_am
    assert_equal(10, 5)
  end
end

A couple of things of note:

  • require is how you load external libraries.
  • The test class name (SomethingTest) is a convention. Follow it and avoid pain.
  • The test method name also follows a convention : test_* . Again, follow it and avoid pain.
  • Indentation is two spaces by convention. Follow, Avoid.
  • Parentheses for method calls are optional, but it's good form to put parentheses if there are two or more arguments to a method. Also, parentheses will save you if the parser has issues parsing. Remember that.
To run that, you would simply call ruby my_test.rb and see one test succeed and one test fail.

How would you write a simple program? Create a file called myprog.rb and put the following inside:

1
2
3
4
5
6
7
def palindromize input
  input.reverse
end

puts "Give me something and we'll see if it's a palindrome!"
input = gets.chomp
puts palindromize(input)

This would be run, like the test, with ~ruby myprog.rb~ Some things to note:

  • gets.chomp -- Ruby, by default, captures the return key being pressed. Chomp is necessary to remove that special character.
  • Did you notice how I put parentheses around palindromize(input) ? It's so that both the Ruby parser and human readers stay sane when reading the code.

Things of note:

Ruby documentation: http://ruby-doc.org/, http://ruby-doc.org/core -- that will get you started. The documentation is excellent until you get to some arcane features -- so not to worry, for now.

Wanna talk to people? You can go to http://www.ruby-forum.com/ (Ruby big names, such as David Black, JEG2 and even Matz, the creator of the language) are active there, and everyone is nice. You can also go to the Freenode server on IRC, channel #ruby.

Let me know if you think I could add things to this tutorial or if you have questions!