posts

Oct 15, 2017

Ruby .() notation

Estimated Reading Time: 1 minutes (180 words)

In Ruby, .() is a syntatic sugar for call method.

class Person
  def call
    "Hello World"
  end
end

Person.new.()
#=> Hello World

In this example, we declare a Person class and call method in the class. With this, we can later execute Person.new.() to call the call method.

Note that, we need to initialize the Person object first by calling new, since the call is a instance method.

If we wanted to just call by Person.(), we can do it this way:

class Person
  def self.call
    "Hello World"
  end
end

Person.()
#=> "Hello World"

Some programming languages such as Python, Swift and Scala initalize object by Person(). In Ruby, we can’t override the () operator to achieve the same effect (see this StackOverflow question). However, we can use .() to achieve a similar result:

class Person
  def initialize(name, age)
    @name = name
    @age = age
  end

  def self.call(name:, age:)
    self.new(name, age)
  end
end

Person.(name: "Peter", age: 12)
#=> #<Person:0x00007f991b80b8d8 @name="Peter", @age=12>

Another way is to use metaprogramming in Ruby. Take a look at this StackOverflow answer to find out how.