Confusion with method call without `.` operator

15 views
Skip to first unread message

Love U Ruby

unread,
Sep 6, 2013, 4:15:03 PM9/6/13
to rubyonra...@googlegroups.com
I am confused on the second call :-

class Foo
def <<(param)
"hi"
end
end
foo = Foo.new
foo.<<(1) # => "hi"

foo << 1 # => "hi" # I didn't use `.` method,then how method `<<` has
been called?

--
Posted via http://www.ruby-forum.com/.

Robert Walker

unread,
Sep 6, 2013, 7:49:23 PM9/6/13
to rubyonra...@googlegroups.com
Love U Ruby wrote in post #1120864:
> I am confused on the second call :-
>
> class Foo
> def <<(param)
> "hi"
> end
> end
> foo = Foo.new
> foo.<<(1) # => "hi"
>
> foo << 1 # => "hi" # I didn't use `.` method,then how method `<<` has
> been called?

I would image that it works the same as it does for the +, -, *, and /
methods.

Example:
$ irb
irb(main):001:0> 5 + 7
=> 12
irb(main):002:0> 5.+(7)
=> 12
Reply all
Reply to author
Forward
0 new messages