Ruby - Using Parentheses Avoid Ambiguity between method and variable

Introduction

Methods may share the same name as local variables.

For example, you might have a variable called name and a method called name.

Parentheses avoid ambiguity:

Demo

greet = "Hello"
name = "Fred"#   www .  j  a  v  a 2s. c  o m

def greet
    return "Good morning"
end

def name
    return "Mary"
end

def sayHi( aName )
    return "Hi, #{aName}"
end

puts( greet )                 #=> Hello
puts greet                    #=> Hello
puts greet()                  #=> good morning
puts( sayHi( name ) )         #=> Hi, Fred
puts( sayHi( name() ) )       #=> Hi, Mary

Result

Related Topic