First Ruby Program — Complete Beginner Guide
Learning Objectives
- Create and run your first Ruby file
- Explain what puts does and how output works
- Read your program line by line with confidence
- Add comments to your code
- Run Ruby code directly from the terminal
Writing Hello World
The traditional first program prints a greeting to the screen. Create a file named hello.rb and type the following line exactly:
puts "Hello, World!"
Save the file, then run it from your terminal:
ruby hello.rb
Your terminal displays:
Hello, World!
Congratulations. You have written and executed a Ruby program on your own computer, which is a meaningful first milestone in any coding journey.
Understanding puts
puts is a built-in method that prints data to standard output, followed by a newline character. Think of it as Ruby's way of speaking to you through the terminal window.
Anatomy of the line
| Part | Meaning |
|---|---|
| puts | The method that displays output |
| "Hello, World!" | A string value passed to the method |
| Quotes | Mark the beginning and end of the text |
| Newline | Automatically added by puts after printing |
Strings can use double quotes or single quotes. Double quotes allow special behaviors such as interpolation, which you will explore in a later lesson on string methods.
Running Code Two Ways
From a file
Running ruby hello.rb reads the file and executes every line from top to bottom. This is how you will run most real programs and exercises throughout these tutorials.
From the interactive prompt
Ruby also ships with IRB, an interactive shell for trying code instantly. Type irb in your terminal, then enter:
puts "Hello from IRB"
IRB prints the result immediately and waits for your next command. Exit by typing exit. Many beginners use IRB to test small ideas quickly before writing full files.
Adding More Output
Expand your program to explore how sequences of lines behave when executed in order:
puts "Line one"
puts "Line two"
puts 21 * 2
Running the file prints three separate lines. Notice that puts accepts both strings and numbers, converting values as needed for display without extra work from you.
Printing without a newline
If you want output pieces on the same line, use print instead:
print "Hello, "
print "World!"
This prints both phrases on one line because print does not append a newline automatically. Understanding the difference between puts and print prevents many early formatting surprises.
Adding Comments
Comments are notes written for human readers. Ruby ignores them entirely when running your code, so they never affect program behavior.
# This line is a comment
puts "This line runs"
# puts "This line is skipped"
Single-line comments begin with the hash symbol. Use comments to explain why you wrote something, especially when logic becomes more complex in later lessons and future you needs a reminder.
Common Beginner Mistakes
Forgetting quotes
puts Hello, World!
Without quotes, Ruby tries to interpret Hello as a method or variable name and raises an error. Always wrap text in quotes when you intend to print it literally rather than evaluate it.
Mixing file extensions
Save Ruby files with the .rb extension. Saving as .txt and running ruby hello.txt still works if the content is valid Ruby, but a consistent .rb extension keeps projects organized and readable.
Running in the wrong directory
If Ruby reports that the file does not exist, confirm your terminal is in the same folder as your file. Use pwd to print your current directory on most systems, then change directories if needed before running again.
Exploring with Curiosity
Try these small experiments in your first file to build intuition about evaluation:
puts "I am learning Ruby"
puts "My favorite food is pizza"
puts 10 + 5
puts "10 + 5"
The last two lines demonstrate an important difference: one calculates a sum, while the other prints the characters of the expression as written. Experiments like this build understanding faster than memorizing rules alone.
Key Takeaways
💡
- puts prints a value to the terminal and adds a newline
- Ruby files typically use the .rb extension and run with the ruby command
- IRB lets you experiment interactively without creating files
- Comments start with the hash symbol and are ignored by Ruby
- Small experiments help you understand how Ruby evaluates expressions
Practice Questions
- What does the puts method do when it finishes printing?
- How do you start Ruby's interactive shell?
- What symbol introduces a single-line comment in Ruby?
- Why does puts Hello cause an error for beginners?
- Write a program that prints your city, your age, and the result of 7 times 6.
Extended Learning: Beyond the Basics
Key Concepts Summary
This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.
Prerequisite Knowledge
Before studying this topic in depth, ensure you understand:
- Basic definitions and terminology
- Core formulas and their derivations
- Common applications in real-world scenarios
- Typical pitfalls and misconceptions
Step-by-Step Walkthrough
Let us work through a complete example to solidify understanding:
Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.
Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.
Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.
Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.
Common Mistakes to Avoid
- Misidentifying the problem type — Always start by categorizing the problem
- Incorrect formula application — Verify the formula applies to your specific case
- Unit errors — Always check units are consistent throughout
- Sign errors — Pay careful attention to positive and negative values
- Rounding too early — Keep full precision until the final answer
- Forgetting constraints — Check that solutions satisfy all given conditions
Real-World Applications
Understanding this concept is essential for:
- Advanced problem solving in examinations
- Building intuition for more complex topics
- Developing analytical thinking skills
- Applying mathematical reasoning to practical situations
Practice Exercises
- Basic: Solve 5 problems of increasing difficulty using the core concept.
- Intermediate: Apply the concept to word problems with multiple steps.
- Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
- Challenge: Create your own problem that requires this concept plus additional knowledge.
Interview and Exam Tips
- Common question patterns: Identify which exam or interview formats test this topic
- Time management: Practice completing problems within time constraints
- Common traps: Know the typical mistakes examiners set as distractors
- Verification strategy: Develop a quick method to check your answer
Related Topics
- Previous Topic — Build on the foundation laid here
- Next Topic — See how this concept extends to more advanced material
- Practice Problems — Additional exercises with solutions
- Video Explanation — Visual walkthrough of key concepts
Quick Reference Card
| Concept | Formula/Rule | Key Insight | Common Use |
|---|---|---|---|
| Core Definition | See main text | Understand the intuition | Foundation for all applications |
| Key Formula | See main text | Memorize the derivation | Direct problem solving |
| Special Case | See main text | Recognize when it applies | Simplifies complex problems |
| Edge Case | See main text | Know the limitations | Avoids errors |
Further Reading
For students who want to go deeper:
- Standard textbooks on the subject
- Research papers applying this concept
- Online courses with video explanations
- Practice problem collections with detailed solutions