≡ Menu

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

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"'
Add your comment

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

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • sukjidham October 2, 2009, 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.

  • Louis November 25, 2011, 8:05 pm

    Re: sukjidham
    Ruby is a scripting programming language, with dynamic, strong type, can do almost everything other language can do (e.g. Python, Perl).