Why does Ruby call the `call` method when I don't supply the method name?

Proc#call:

Invokes the block, setting the block’s parameters to the values in params using something close to method calling semantics. Generates a warning if multiple values are passed to a proc that expects just one (previously this silently converted the parameters to an array). Note that prc.() invokes prc.call() with the parameters given. It’s a syntax sugar to hide “call”.

I did some research and found method #() is a syntactic sugar of the method #call..Look at the error as below :

module Foo
  def self.bar
    12
  end
end
Foo.()
#undefined method `call' for Foo:Module (NoMethodError)

As OP defined the #call method in module Foo class,Foo#call is called in an attempt of Foo.().

Here is some more examples :

"ab".method(:size).() # => 2
"ab".method(:size).call # => 2
"ab".() # undefined method `call' for "ab":String (NoMethodError)

See here what Matz said So compromise with object.() syntax introduced in 1.9...

Tags:

Ruby