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"'
Comments on this entry are closed.
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.
Re: sukjidham
Ruby is a scripting programming language, with dynamic, strong type, can do almost everything other language can do (e.g. Python, Perl).