Ruby Syntax

Ruby Syntax

Udayan Shakya
Udayan Shakya

|

2 min read

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 "Hello"
puts "World"

Output

Hello
World

However, you can use semicolons to write multiple statements on the same line:

puts "Hello"; puts "World"

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

# This prints "Hello World!" to the screen
puts "Hello World!"

For multiline comments, use =begin and =end:

=begin
Ruby code that prints
"Hello" and "World" on separate lines
=end

puts "Hello"
puts "World"
Guidelines:
  • =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

puts "Hello World!"

However, you can still use parentheses if you want to:

puts("Hello World!")
Best Practice:
  • 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

# Create a method
def greet(name)
    puts "Hello, #{name}!"
    puts "How are you?"
end

# Call the method
greet "Robert"

Output

Hello, Robert!
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

puts "Main Program"

# Ruby runs this code first
BEGIN {
    puts "Loading..."
    puts "Initializing Ruby Program"
    puts "--------------------------"
}

Output

Loading...
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

puts "Main Program"

# 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

Loading...
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.

Udayan Shakya
Udayan Shakya

Udayan is a physics graduate and works as a content writer at Programiz. When he's not busy with programming, he passes his time consuming the books he possesses in his small library.