7

I've seen code like this:

def some_method
  # ...
end.another_method

What does the end.another_method part do?

2 Answers 2

11

I believe that your example is wrong, as what you are doing here is defining a method and calling a method on the result of a method definition (not a method call), which is always (usually?) nil.

There's a similar form which fmendez is referring to, but end is the end of a block, not a method definition in that case.

So, for example:

array.map do |element|
  element * element
end.sum

would, hypothetically, return a sum of squares of elements of given array.

But, if you are doing method chaining like this, it is more common to use bracket style blocks instead of do..end, so the above example would read:

array.map{ |element|
  element * element
}.sum

Blocks in Ruby are method arguments, not unlike any other method arguments (apart from the dedicated syntax), so putting dot after end is not different than putting the dot after ) in

'hello'.concat(' world!').capitalize

Which is also an example of method chaining.

12
  • You're absolutely correct, I was about to say that just now :-)
    – fmendez
    Apr 19, 2013 at 18:06
  • As far as I know, multiline blocks shouldn't be chained with either syntax. Apr 19, 2013 at 18:06
  • Also, I get a warning: void value expression when dereferencing a method definition Apr 19, 2013 at 18:08
  • @JanDvorak You certainly can. You can try this: a = [3,2,1] a.map! do |element| element * element end.sort! p a
    – fmendez
    Apr 19, 2013 at 18:13
  • 1
    I see... I would prefer if they elaborated. I myself prefer as little one-time variables as possible. Each to his own I guess. Anyway, Jörg came, the party's over... ;) Apr 19, 2013 at 18:33
8

In Ruby, the . is the message sending operator. (In other languages, it would be called a method calling operator instead.) So, when you say

foo.bar

it means "evaluate foo and send the message bar to the result of evaluating foo".

In this particular case, you are sending the message another_method to the result of evaluating

def some_method; end

The Ruby Language Specification says that the value of a method definition expression is undefined and should be ignored; and on most Ruby implementations, method definition expressions simply evaluate to nil, which isn't terribly useful.

However, on some implementations, method definition expressions do evaluate to something more useful than nil. On Rubinius, for example, they evaluate to the CompiledMethod object for the method being defined. And CompiledMethod has a rich API, so sending messages to a CompiledMethod object definitely makes sense.

It has also been proposed that method definition expressions should return a Symbol corresponding to the name of the method being defined or a Method object.

Put simply: the dot in this particular case means the exact same thing it always means in Ruby: send a message, call a method, invoke a member function, whatever you want to call it.

1
  • +1 nicely said! maybe you should mention that in general it's probably not a good idea to do this, as it's not portable between different Ruby implementations, as method definition usually returns nil.
    – Tilo
    Apr 19, 2013 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.