Ruby Hello World Example: How To Write and Execute Ruby Program on Unix OS

by Ramesh Natarajan on October 2, 2009

Question: I would like to understand the basics of how to write and execute a ruby program on Linux OS. Can you explain it with a simple example?

Answer: In this article, let us review very quickly how to write a basic Hello World ruby program and execute *.rb program on Linux or Unix OS.

1. Write a Hello World Ruby Program

Create the helloworld.rb program using a Vim editor as shown below.

$ vim helloworld.rb

#!/usr/bin/ruby

# Hello world ruby program

puts "Hello World!";

2. Verify ruby Interpreter availability

Make sure ruby interpreter is installed on your system as shown below.

$ whereis ruby
ruby: /usr/bin/ruby /usr/bin/ruby1.8 /usr/lib/ruby /usr/share/man/man1/ruby.1.gz

$ which ruby
/usr/bin/ruby

Installing Ruby

If you don’t have Ruby, install it as shown below.

$ sudo apt-get install ruby

3. Execute Ruby Program

You can either execute using “ruby helloworld.rb” or “./helloworld.rb”.

$ ruby helloworld.rb
Hello World!

( or )

$ chmod u+x helloworld.rb

$ ./helloworld.rb
Hello World!

Note: As Ruby is an interpreted language, you don’t have the compilation step similar to the C program.

Executing Ruby one liner

You can also execute Ruby from the command line as shown below. This will print Hello World!.

$ ruby -e 'puts "Hello World!\n"'
Download Free eBook - Linux 101 Hacks

Get free Unix tutorials, tips and tricks straight to your email in-box.

If you enjoyed this article, you might also like..

  1. Lua Hello World Example: How To Write and Execute Lua Program on Linux OS
  2. Perl Hello World Example: How To Write and Execute Perl Program on Unix OS
  3. Python Hello World Example: How To Write and Execute Python Program on Unix OS
  4. XQuery Hello World Example: How To Write and Execute XQuery Program on Linux OS
  5. Lisp Hello World Example: How To Write and Execute Lisp Program on Linux OS
  

Vim 101 Hacks Book

{ 1 comment… read it below or add one }

1 sukjidham October 2, 2009 at 1:54 pm

Hi, I tried and it’s look good, but it is first time that I heard about Ruby, so I don’t know what are possible in Ruby?, but always’s good to learn something new, thanks.

Leave a Comment

Previous post:

Next post: