▲ | bigtunacan 16 hours ago | |||||||||||||||||||||||||
It’s not just about practicality. Ruby is using message passing, not method calling. This is fundamentally different and a bit foreign to the larger community. Then ruby layers syntactic sugar on top that hides this. Behind the scenes everything is a message passed using __send__ and you can do this directly as well, but you generally don’t. So when you write 5.times { puts "Hello" } It’s sort of expected by the average programmer that you are telling 5 to call the times method and expect it to exist and do what it’s told. In reality you have indirectly sent a message that looks like 5.__send__(:times) { puts "Hello" } What we are really doing is sending a message to 5 (the receiver) and giving it the opportunity to decide how to respond. This is where method_missing comes in to allow responding in a custom fashion regardless if a method was explicitly defined. So you’re not telling 5 to call the method times, rather you are asking, “Hey 5, do you know how to handle the message times?” These are fundamentally different things. This is actually super important and honestly hard to really grok _especially_ in ruby because of the syntactic sugar. I came from a C/C++ background originally, then Java and then moved to Ruby. After a few years I thought I understood this difference, but honestly it wasn’t until I spent a couple years using Objective-C where message passing is happening much more explicitly that I was able to truly understand the difference in a way that it became intuitive. | ||||||||||||||||||||||||||
▲ | anamexis 10 hours ago | parent [-] | |||||||||||||||||||||||||
I’m a rubyist, but how is message passing fundamentally different from method calling? I get that method_missing adds a twist, but your comment doesn’t explain what the fundamental difference is. Especially in the context of Fixnum#times. How does message passing vs method calling matter there? #times is just a method on Fixnum. | ||||||||||||||||||||||||||
|