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"'
Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.Get the Linux Sysadmin Course Now!
If you enjoyed this article, you might also like..
|
|
|
|






My name is Ramesh Natarajan. I will be posting instruction guides, how-to, troubleshooting tips and tricks on Linux, database, hardware, security and web. My focus is to write articles that will either teach you or help you resolve a problem. Read more about
{ 2 comments… read them below or add one }
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).