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
- Open your text editor or IDE (Integrated Development Environment).
- 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:
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
-
Open your terminal or command prompt.
-
Navigate to the directory where you saved your
hello_world.rb
file. You can use thecd
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:
Output
You should see the following output in your terminal:
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
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
Save the file and run it again. You should see:
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!
Ruby Programming Course
Module 1: Introduction to Ruby
Module 2: Basic Ruby Concepts
Module 3: Working with Collections
Module 4: Object-Oriented Programming in Ruby
- Classes and Objects
- Instance Variables and Methods
- Class Variables and Methods
- Inheritance
- Modules and Mixins
Module 5: Advanced Ruby Concepts
Module 6: Ruby on Rails Introduction
- What is Ruby on Rails?
- Setting Up Rails Environment
- Creating a Simple Rails Application
- MVC Architecture
- Routing
Module 7: Testing in Ruby
- Introduction to Testing
- Unit Testing with Minitest
- Behavior-Driven Development with RSpec
- Mocking and Stubbing