Ruby’s syntax is designed to be simple and expressive. Let’s see how it reads and executes your program.
1. The End of a Statement
In Ruby, a new line usually signals the end of a statement — no semicolons required.
Example
puts "World"
Output
World
However, you can use semicolons to write multiple statements on the same line:
This program gives the same output as above.
Note: Semicolons are uncommon in idiomatic Ruby.
2. Comments Start with Hashes (#)
Ruby uses the hash (#) symbol for single-line comments, similar to Python.
Example
puts "Hello World!"
For multiline comments, use =begin and =end:
Ruby code that prints
"Hello" and "World" on separate lines
=end
puts "Hello"
puts "World"
- =begin and =end must be at the beginning of a line (they shouldn't have any leading spaces).
- The comment content must appear on separate lines between them.
3. Naming Convention
Follow these conventions when naming things:
- Variables and methods: Start with a lowercase letter or underscore (e.g., name, my_value).
- Class names: Start with an uppercase letter (e.g., Person, Calculator).
- Case-sensitive: For instance, the variable value and Value are different.
4. Parentheses in Method Calls
Unlike many other languages, Ruby doesn’t require parentheses when calling methods.
Example
However, you can still use parentheses if you want to:
- Use parentheses when passing multiple arguments for improved readability.
- Avoid them for single arguments (unless clarity is needed).
5. Blocks and Scopes
Ruby uses the end keyword to close code blocks. Indentation is optional but strongly recommended for readability.
Example
def greet(name)
puts "Hello, #{name}!"
puts "How are you?"
end
# Call the method
greet "Robert"
Output
How are you?
Here,
- The code inside greet is indented to show scope.
- end marks the end of the method block.
Note: Curly braces {} are used in Ruby for single-line blocks (like in iterators), and they're required for the code inside BEGIN and END.
6. The BEGIN Statement
The BEGIN statement declares code that will be called before the program runs.
Example
# Ruby runs this code first
BEGIN {
puts "Loading..."
puts "Initializing Ruby Program"
puts "--------------------------"
}
Output
Initializing Ruby Program
--------------------------
Main Program
Here, even though the BEGIN block appears after puts "Main Program", Ruby runs it before any other code. That's just how BEGIN works.
Note: The code inside BEGIN and END must be wrapped in curly braces { ... }, unlike most Ruby blocks that use do...end.
7. The END Statement
The END statement declares code that will be called at the end of the program.
Example
# Ruby runs this code last
END {
puts "--------------------------"
puts "Hasta la vista..."
puts "Program Terminated!"
}
# Ruby runs this code first
BEGIN {
puts "Loading..."
puts "Initializing Ruby Program"
puts "--------------------------"
}
Output
Initializing Ruby Program
--------------------------
Main Program
--------------------------
Hasta la vista...
Program Terminated!
Note: Don't confuse the END statement with the end keyword; end is used to close blocks like def, if, classs, etc.
What's Next?
You're now familiar with the basics of Ruby syntax, and how it distinguishes itself from other programming languages.
To continue on your Ruby journey, click this link to learn how comments work.
Subscribe to Programiz Blog!
Be the first to receive the latest tutorial from Programiz by signing up to our email subscription. Also more bonus like inside looks on the latest feature and many more.