← Back to Ruby Operators

Parentheses

Parentheses (()) are used for method calls and to group expressions for precedence.

Calling a method

> puts("Hello, world!")
Hello, world!

In a lot of situations in Ruby, we omit the parentheses when calling a method.

> puts "Hello, world!"
Hello, world!

Grouping expressions

Parentheses can also be used to group expressions for precedence.

> 3 * 4 + 5
=> 17
> 3 * (4 + 5)
=> 27

Range Literals

Range literals can be defined with or without surrounding parentheses. It is unclear to me if the range syntax is distinct from parentheses expression grouping. Regardless, here are some examples to soak up.

def class_check(thing)
  puts thing.class
end

> 1..2
=> 1..2
> class_check 1..2
Range
=> nil
> class_check(1..2)
Range
=> nil
> class_check((1..2))
Range
=> nil
> (1..2).to_a
=> [1, 2]
> 1..2.to_a
(irb):51:in `<main>': undefined method `to_a' for 2:Integer (NoMethodError)

References