LEARN RUBY ON THE MAC
LEARN RUBY ON THE MAC

Getting Started

Ruby comes pre-installed on Macs. To verify and check the version you have, open the Terminal.app in your /Applications/Utilities folder and type
 

ruby -v

 

You should get back something like:

 

ruby 2.0.0p247 (2013-06-27 revision 41674) [universal.x86_64-darwin13]

 

Don’t worry if the version is not exactly the same as shown here, but you should be on at least ruby 2.0.

 

You can find out more about your ruby installation by typing

 

man ruby

 

at the Terminal prompt. Hit the space bar to scroll down and the 'U' key to scroll back up. Hit the 'Q'key to exit the man page.

You can use an interactive ruby shell to run ruby commands within Terminal by typing

 

irb

 

Use control-D on your keyboard to quit the ruby environment.

 

However, for the most part, we’ll be typing our scripts into their own files, then running the file from Terminal. To do this, you save the script with a .rb extension, then call it with the command ‘ruby’. Let’s have an example.

 

Open up a new document in a text editor (Text Edit will do, but you’d be far better off getting TextWrangler, TextMate or Tincta for coding work — they’re all free!). Type in the following:

 

#!/bin/ruby
puts "Hello Ruby World!"

 

Save the script as helloworld.rb in your home folder (it’s important that you save it directly in your home folder, not buried inside another folder). In Terminal, type:

 

ruby helloworld.rb

 

You should see the reply:

 

Hello Ruby World!

 

OK, we’ve confirmed your Ruby installation is working and that you know how to save and run a ruby script. ?

Print Print | Sitemap
© Learn Ruby on the Mac