Ruby Logo

Your First Ruby Program

Write and run your first Ruby program with Hello World examples.

Home Ruby Your First Ruby Program

Your First Ruby Program

Learn to write, save, and run your first Ruby program. Master the basics of Ruby output methods and file execution.

Hello, World!

Learning Objective: Write and run your first Ruby program that outputs "Hello, World!"

Step 1: Create Your First Ruby File

Create a new file called hello.rb
# Create the file
touch hello.rb

# Or use your favorite text editor
code hello.rb # VS Code
vim hello.rb # Vim
nano hello.rb # Nano
Add this code to hello.rb
puts "Hello, World!"

Step 2: Run Your Program

# Run the Ruby file
ruby hello.rb

# Expected output:
Hello, World!

Ruby Output Methods

Ruby provides several methods for displaying output. Each has its own characteristics and use cases.

puts Most Common Output Method

puts (put string) outputs text and automatically adds a newline at the end. It's the most commonly used output method.

Example Code
puts "Hello, World!"
puts "Welcome to Ruby!"
puts 42
puts [1, 2, 3]
Output
Hello, World!
Welcome to Ruby!
42
1
2
3

print Output Without Newline

print outputs text without adding a newline at the end. Useful when you want to build output on the same line.

Example Code
print "Hello, "
print "World!"
print "\n" # Add newline manually
Output
Hello, World!

p Debugging Output

p is great for debugging. It calls inspect on the object, showing its internal representation.

Example Code
p "Hello, World!"
p [1, 2, 3]
p { name: "Ruby", version: "3.2" }
Output
"Hello, World!"
[1, 2, 3]
{:name=>"Ruby", :version=>"3.2"}

File Extensions & Naming Conventions

Ruby File Convention: Ruby files should end with .rb extension

Ruby Naming Conventions

File Names
  • snake_case: hello_world.rb
  • descriptive: user_authentication.rb
  • lowercase: calculator.rb
Examples
  • my_first_program.rb
  • temperature_converter.rb
  • student_grade_calculator.rb

Practice Examples

Example 1: Personal Greeting

Create greeting.rb
puts "What's your name?"
name = gets.chomp
puts "Hello, #{name}! Welcome to Ruby programming!"
Run and Test
# Run the program
ruby greeting.rb

# Expected interaction:
What's your name?
Alice
Hello, Alice! Welcome to Ruby programming!

Example 2: Simple Calculator

Create calculator.rb
puts "Enter first number:"
num1 = gets.chomp.to_i
puts "Enter second number:"
num2 = gets.chomp.to_i
sum = num1 + num2
puts "The sum of #{num1} and #{num2} is #{sum}"

Best Practices

Ruby Programming Best Practices

  • Use descriptive file names: Choose names that clearly indicate what the program does
  • Add comments: Use # to explain complex logic
  • Use puts for user output: It's the most readable and commonly used method
  • Use p for debugging: Great for inspecting variables and data structures
  • Keep it simple: Start with basic programs and gradually add complexity

Common Mistakes to Avoid

  • Forgetting .rb extension: Always save Ruby files with .rb extension
  • Case sensitivity: Ruby is case-sensitive - puts is different from Puts
  • Missing quotes: String literals must be enclosed in quotes
  • Wrong file path: Make sure you're in the correct directory when running Ruby files

What's Next?

Continue Your Ruby Journey

  1. Learn IRB: Start the Interactive Ruby shell with irb
  2. Explore Variables: Learn about different types of variables in Ruby
  3. Study Data Types: Understand strings, numbers, arrays, and hashes
  4. Practice More: Create simple programs to reinforce your learning
  5. Read Documentation: Learn how to find help and documentation

Quick Navigation

Related Topics

Video Tutorial

Watch and learn your first ruby program

Pro Tip: After reading through the content above, watch this video to reinforce your understanding and see the concepts in action!