By Md. Sabuj Sarker | 11/6/2017 | General |Beginners

Writing Your First Program in Ruby

Writing Your First Program in Ruby

In a previous article I discussed in detail why one might choose Ruby for their career. I also covered why not to choose Ruby and in which situations or conditions. I also showed you the way to test yourself whether you need Ruby or not. So, if you’ve made it to this article, changes are good that you’re ready to give Ruby a go. So, let’s get started with your first program in Ruby.

Before You Begin

As this is a beginner level tutorial, there is not much you need do before you begin. But, before you begin I highly recommend you to prepare your mind to practice each and every line of code instead of just skimming through the tutorial.

You need some software and familiarity with it to go along with this tutorial.

  1. Download/install the Ruby interpreter.
    If you are on any Debian, Ubuntu, Mint or another Debian based flavor of Linux you can install Ruby with the following command:
    sudo apt-get install ruby-full

    On Fedora, CentOS or RHEL run the following command:
    sudo yum install ruby

    On Mac OS install via Homebrew with the following command:
    brew install ruby

    In Windows download Ruby with RubyInstaller from this link.
  2. You must be familiar with command lines or terminal emulators to run ruby scripts.
  3. You need a plain text editor to code in Ruby. Try an editor with syntax highlighting like Sublime or Notepad++.
  4. You can use IDEs if you want. When I use IDEs, I prefer using RubyMine from JetBrains.

At the beginner level it is not advised to use IDEs. You should use plain code editors to write Ruby code and run it with the help of the command line. In this way you will have a better understanding of how things work. IDEs abstract a lot of manual processes and thus are not as good for beginner Ruby programmers.

Hello World in Ruby

Hello world is the nickname of the program that we use start coding with programming languages for the first time. It is not a must that you have to say hello to the world with it, but this is a good way to start. By demonstrating how to display some text on the screen is a good way to start expressing your thoughts on computer screen with the help of code.

Choose a directory in your hard disk where you want to save your Ruby scripts. Create a new file with name hello.rb. Note that .rb is the file extension of Ruby scripts. Write the following line of code in the file with the help of a plain text editor or a code editor.

puts "Hello humans, this is my first ruby program."

Running Your Program

Now, we need to run it. The installation processes that we followed also added the Ruby executable in our system. Now, the ruby command is available in our command line. So, let's run the ruby script (or program is you like) with the help of following command through command line. But, make sure that your command line's current working directory points to the directory where you have saved the hello.rb file.

ruby hello.rb

Output:

puts "Hello humans, this is my first ruby program."

If you are more familiar with print than puts from your previous language background, if you have any, then you can achieve the above output with the following line of code. Remember, there are some difference in puts and prints in Ruby.

print "Hello humans, this is my first ruby program."

So, now you are capable of talking to the machine with the help of Ruby code.

Make Your Program Interact With User Provided Input

Our hello world program has no interactivity—it is totally hard coded. A program that cannot take input from users is quite crippled. It needs some way to take input from users or other sources. The most primitive way of taking input is from the command line. At the request of a program, the command line waits for the user to type something. When the user hits enter, the command line gives control back to the the program.

In Ruby we can easily take input through the command line with the help of the gets method. On invocation of the gets method, the command line waits for the user to type something and press enter. After that this method returns the input as a string.

Let's write a program that will take the user's name as input and thank them for providing the name. Before the gets method is invoked, we need to show some text on the command line to tell the user what the program is expecting.

puts "Input your name please: "
inp = gets
puts "Thanks " + inp + " for providing your name. We will send this name to aliens soon ;)"

Outputs:

Thanks SabujXi for providing your name. We will send this name to aliens soon ;)

We have got our output from the user with and unexpected newline after the name. When you press enter that is considered as a newline and appended at the end of the input. So, we need to clean that. The strip method on string objects can help us in this regard. strip removes any white space character from the beginning and from the end of strings. Newline character is also a white space character and thus will be removed.

puts "Input your name please: "
inp = gets.strip
puts "Thanks " + inp + " for providing your name. We will send this name to aliens soon ;)"

Output:

Thanks SabujXi for providing your name. We will send this name to aliens soon ;)

Conclusion

Writing programs with Ruby is really fun, easy and need less keystrokes than other languages. In other languages method calling need an opening and closing parenthesis, but in Ruby that is optional as seen in puts, gets, strip methods. Continue your journey with Ruby and I am going to come back with more and more tutorials, articles and guides for you.

By Md. Sabuj Sarker | 11/6/2017 | General

{{CommentsModel.TotalCount}} Comments

Your Comment

{{CommentsModel.Message}}

Recent Stories

Top DiscoverSDK Experts

User photo
3355
Ashton Torrence
Web and Windows developer
GUI | Web and 11 more
View Profile
User photo
3220
Mendy Bennett
Experienced with Ad network & Ad servers.
Mobile | Ad Networks and 1 more
View Profile
User photo
3060
Karen Fitzgerald
7 years in Cross-Platform development.
Mobile | Cross Platform Frameworks
View Profile
Show All
X

Compare Products

Select up to three two products to compare by clicking on the compare icon () of each product.

{{compareToolModel.Error}}

Now comparing:

{{product.ProductName | createSubstring:25}} X
Compare Now