Skip to main content

Ruby “Hello World”

By October 9, 2013No Comments

You must know by now that when we program we start with Hello World! So this will be no different. Working with Ubuntu we will first install Ruby and then so by example how we can use Ruby to write to the screen. As well as using irb we will see how we can print he version of ruby and run code from the bash shell.

  • irb
  • ruby -v
  • ruby -e

I am using ruby 1.9 installed on by Ubuntu 13.10 system. We can run ruby interactively using the program irb(/usr/bin/irb). This is perhaps OK for some simple testing but we can move onto scripts very quickly. To start irb , simple type irb at you bash shell whith ruby installed. From the irb prompt we can type:

puts "Hello World"

The method puts will act a little like echo in the shell. We use puts most often in ruby as it includes the new line character whereas print does not.

puts "Hello World"

is equal to

print "Hello Worldn"

Without the n there would be no new line with print and of course we can still use print where we do not need a new line between the output.

print "Hello world"

If you really are interested we can also use just the letter p

p "Hello world"

This is similar to put but in fact it is equal to puts “Hello world”.inspect. The deal here is that the quotes are returned with the string. This is more useful in debugging where we can exactly what is being returned.