0 Replies - 12396 Views - Last Post: 12 July 2011 - 09:41 AM Rate Topic: -----

#1 modi123_1   User is offline

  • Suitor #2
  • member icon

Reputation: 14092
  • View blog
  • Posts: 56,460
  • Joined: 12-June 08

[link] Ruby for Newbies: Missing Methods

Posted 12 July 2011 - 09:41 AM



Letís say your working with a Ruby object. And letís also say that you arenít entirely familiar with this object. And letís also say that you call a method that doesnít exist on the object.

Ruby has an awesome way of allowing us to rescue ourselves from this. Check this out:

We can create a method called method_missing in our class. If the object weíre calling the method on doesnít have the method (and doesnít inherit the method from another class or module), Ruby will give us one more chance to do something useful: if the class has a method_missing method, weíll hand the information about the method cal to method_missing and let it sort the mess out.

Well, thatís great; weíre no longer getting an error message.

But stop and think about this for a second. First of all: no, weíre not getting an error message any more, but we arenít getting something useful. Itís hard to say what useful would be in this case, because out method name doesnít suggest anything. Second of all, this is pretty powerful, because it allows you to basically pass any method to an object and get an intelligent result.

Is This A Good Question/Topic? 1
  • +

Page 1 of 1