I would recommend using the Ruby Version Manager. This just involves pasting the following line of code into a command prompt:$ \curl -L https://get.rvm.io | bash -s stable --ruby
If you use a Mac then you can use the Ruby Version Manager (see above), or use Homebrew, with the following command:$ brew install ruby
To check that everything is running fine, open up a terminal window and type the following line at the command prompt:$ ruby -v
This checks which version of Ruby you are running. You should get something similar to the following:ruby 2.0.0p247 (2013-06-27 revision 41674) [i686-linux]
Getting Started with Interactive Ruby
Interactive Ruby, or IRB to its friends, is a way of entering Ruby into a command line one line at a time and getting instant feedback. It’s a great way of experimenting with Ruby and checking that things work. To get it up and running, all you need to do is type the following at a command line prompt:$ irb
You should then see something similar to the following:2.0.0p247 :001 >
Now, it’s time to get started writing some Ruby!
It has been a long standing tradition when learning to program to start with the classic “Hello World!” program. We’re going to keep to the spirit of this and say hello to the differnt environments where we will be testing Ruby. At the IRB prompt, simply type the following:2.0.0p247 :001 > puts "Hello World!"
You should see the following output:Hello World! => nil
What happened there? Well, puts is a ruby command that stands for ‘put string’. A string in Ruby is an object that stores text between quote marks (either ” or ‘). The putscommand will display the string that follows it.
The => nil part is simply the return value of the puts command. All actions in Ruby have a return value, even if they don’t explicitly return anything. In those cases, they still have to return nil, which is another object.
To exit IRB simply type exit like so:2.0.0p247 :001 > exit
Create a folder to save your work in and call it something suitable such as ‘ruby’ or the like. Ruby files are saved with the extension .rb. To get started, create an empty text file and save it as ‘hello_ruby.rb’. Now write the following code inside the file and save it:puts "Hello Ruby!"
This is very similar to what we typed into IRB – that’s because they are just the same, which is why IRB is so useful for quickly testing things out. The only difference is that IRB evaluates the ruby as soon as you press ENTER. To run this program, you need to execute it using the ruby command.
Open up a command line prompt and navigate to where the file is saved. This can be done using the cd command. On my system, I have saved it in a folder called ‘ruby’ that is in my home folder, so I would type the following:$ cd ruby
This results in the $ symbol beign prefixed by this path:daz@crunchbang:~/ruby$
Once you are in the correct directory, we can run our program by simply typing rubyfollowed by the name of the file, like so:$ ruby hello_ruby.rb
This should produce the following output:Hello Ruby!
The main difference between running the file and running Ruby in IRB is a Ruby file allows you to enter multiple commands that will all be carried out when the program is run. This can be demonstrated by adding a couple more lines to ‘hello_ruby.rb’ like so:puts "Hello Ruby!" puts "Hello DAZ!" puts "Hello SitePoint!"
If you save this and type ruby hello_ruby.rb into a command prompt, the output should now be:Hello Ruby! Hello DAZ! Hello SitePoint!
Well done – you’ve now written and run your first Ruby program!