Support Online
Skip to main content

Writing Your First Ruby Program: An Interactive Input/Output Application

In this guide, you will write your first interactive program in Ruby that will both output to the screen and receive input from the user. puts ile metin yazdırmayı, gets ile kullanıcıdan veri almayı ve chop ile gereksiz satır sonlarını temizlemeyi öğreneceksiniz.

🧠 What Will You Learn in This Guide?

  • Creating a file in Ruby and running it on the command line
  • Using the puts (output), gets (input) and chop (line break removal) methods
  • Placing variables in text with string interpolation (#{})
  • Fix newline character \n

🔹 Brief Technical Summary

To run a file in Ruby:

ruby dosya_adi.rb
  • puts: Prints text to the screen.
  • gets: Receives input from the keyboard, waits until the ENTER key.
  • chop: Removes the character at the end of the string (usually \n).
  • #{değişken}: Places the variable directly in the text.

🔸 1. Writing the “Hello World” Program

First, let's write our basic Ruby code. Create a new file with the following command in terminal:

nano hello.rb

Then add this code:

puts "Merhaba, Dünya!"

💡 This command is the simplest example of Ruby that prints text to the screen.

Press CTRL + X, then Y and ENTER to save.


🔸 2. Running Ruby Program

Run the file you created in the terminal:

ruby hello.rb

You will see the following result on the screen:

Merhaba, Dünya!

The Ruby interpreter reads the file, calls the puts method, and writes the quoted text to the screen.


🔸 3. Getting Input from the User

Let's request information from the user to make the program more interactive. Create a new file:

nano greeting.rb

Type these codes:

puts "Lütfen adınızı girin."
name = gets
puts "Merhaba, #{name}! Ben Ruby!"

💡 Here:

  • puts displays a message to the user.
  • gets receives data from the user.
  • #{name} inserts the name into the text by interpolating the string.

Save and run the file:

ruby greeting.rb

🔸 4. Clearing Newline Character

When you run the program, the output might look like this:

Lütfen adınızı girin.
Deniz
Merhaba, Deniz
! Ben Ruby!

This is because the gets method also receives the character \n when the user presses ENTER. Add the chop method to remove this excess:

name = gets.chop

💡 chop deletes the character at the end of the string and corrects the output.

Run the program again:

Lütfen adınızı girin.
Deniz
Merhaba, Deniz! Ben Ruby!

💬 Frequently Asked Questions (FAQ)

1️⃣ What is the difference between puts and print?

puts moves to a new line after printing the output, print continues on the same line.

2️⃣ why does gets skip lines?

Because when the user presses ENTER, the character \n is also included in the string.

3️⃣ What can be used instead of gets.chop?

.chomp is available. .chop always deletes the last character, while .chomp only removes \n if it exists.

4️⃣ Why use string interpolation?

Including variables in the text by writing them into #{} is cleaner than concatenating them with the operator +.

5️⃣ Where can I test Ruby programs?

You can set up your own Ruby environment by creating a Virtual Server (V-Server) on GenixNode.


🎯 Result

You have now written your first program in Ruby that can both output and receive data from the user. This foundation is one of the most important steps in your Ruby learning.

💡 Next Step: Personalize the program's response by asking the user for their age or favorite color. To better understand the interactive nature of Ruby, practice by opening a Ruby development environment on GenixNode!