0

What are the "under the hood" differences? What are the practical differences?

Is there any difference from a users perspective?

I know you could use def to define a method, but could you define a message?

Adam Zerner
  • 17,797
  • 15
  • 90
  • 156
  • What do you mean by "define a message"? Is there any difference from a developer's perspective? No. – Dave Newton Apr 11 '16 at 15:44
  • @DaveNewton I'm not sure what I mean by "define a message". – Adam Zerner Apr 11 '16 at 15:46
  • 1
    So, the practical differences are zero: it's conceptual in nature, e.g., http://stackoverflow.com/q/3562272/438992 – Dave Newton Apr 11 '16 at 15:50
  • Good to know. Thanks @DaveNewton. – Adam Zerner Apr 11 '16 at 15:51
  • (Note that I'm specifically not addressing anything around interprocess communication, just what Ruby calls "methods", callable by `send`ing etc. – Dave Newton Apr 11 '16 at 15:53
  • In Ruby methods and messages are the same thing, both technically and practically. When you define a method you could also say you've defined a message, although that terminology sounds strange to most Rubyists' ears. – Jordan Running Apr 11 '16 at 16:22
  • 1
    Many years ago I learned it as methods are the concrete implementation of object behaviors, messages are the act of requesting such behaviors. I can send a message `bibbitybobbityboo` to any Ruby object by saying `my_obj.bibbitybobbityboo`. If `my_obj` has such a method, it will invoke it. Otherwise, `my_obj` will respond by default with `NoMethodError: undefined method 'bibbitybobbityboo' for my_obj:My_Class`. – pjs Apr 11 '16 at 17:00

1 Answers1

2

When you send a message to an object, the object will (usually) respond by executing a method with the same name as that message.

You cannot define messages. You just send them.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • You say "usually". What else might it do? Can you give an example? – Adam Zerner Apr 12 '16 at 01:38
  • How can you create an objection that responds to messages in such a way that doesn't call the associated function? – Adam Zerner Apr 12 '16 at 01:39
  • If there is no corresponding method, a `method_missing` message will be sent instead. If a method is aliased, the name of the method may not be the same as the message. – Jörg W Mittag Apr 12 '16 at 11:31