Welcome to your first Ruby program! In this section, we will guide you through writing, running, and understanding a simple Ruby program. By the end of this lesson, you will have a basic understanding of how to create and execute Ruby code.

Writing Your First Ruby Program

Step 1: Create a New File

  1. Open your text editor or IDE (Integrated Development Environment).
  2. Create a new file and name it hello_world.rb. The .rb extension indicates that this is a Ruby file.

Step 2: Write the Code

In your hello_world.rb file, type the following code:

# This is a comment
puts 'Hello, world!'

Explanation

  • # This is a comment: This line is a comment. Comments in Ruby start with a # and are ignored by the Ruby interpreter. They are useful for adding notes or explanations within your code.
  • puts 'Hello, world!': This line is a Ruby method that prints the string 'Hello, world!' to the console. puts stands for "put string" and is used to output text.

Step 3: Save the File

Save your hello_world.rb file.

Running Your Ruby Program

Step 1: Open the Terminal

  1. Open your terminal or command prompt.

  2. Navigate to the directory where you saved your hello_world.rb file. You can use the cd command to change directories. For example:

    cd path/to/your/directory
    

Step 2: Execute the Program

Run your Ruby program by typing the following command in the terminal:

ruby hello_world.rb

Output

You should see the following output in your terminal:

Hello, world!

Congratulations! You have successfully written and executed your first Ruby program.

Practical Exercises

Exercise 1: Modify the Program

Modify the hello_world.rb program to print your name instead of "Hello, world!".

Solution

# This is a comment
puts 'Hello, [Your Name]!'

Replace [Your Name] with your actual name. Save the file and run it again using the ruby hello_world.rb command.

Exercise 2: Add More Output

Add another puts statement to print a second line of text.

Solution

# This is a comment
puts 'Hello, world!'
puts 'Welcome to Ruby programming!'

Save the file and run it again. You should see:

Hello, world!
Welcome to Ruby programming!

Common Mistakes and Tips

  • Syntax Errors: Ensure that you use single or double quotes around strings. Missing quotes will result in a syntax error.
  • File Naming: Make sure your file has the .rb extension. Without it, the Ruby interpreter may not recognize the file as a Ruby script.
  • Terminal Navigation: Be careful with the cd command to navigate to the correct directory where your Ruby file is saved.

Summary

In this lesson, you learned how to:

  • Create a new Ruby file.
  • Write a simple Ruby program using the puts method.
  • Run your Ruby program from the terminal.
  • Modify and extend your program with additional output.

With these basics in hand, you are now ready to dive deeper into Ruby's syntax and structure in the next lesson. Keep practicing, and soon you'll be writing more complex Ruby programs with ease!

© Copyright 2024. All rights reserved