The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Small Update for the method_missing() Post

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Guy Naor

Posts: 104
Nickname: familyguy
Registered: Mar, 2006

Guy Naor is one of the founders of famundo.com and a long time developer
Small Update for the method_missing() Post Posted: May 13, 2006 3:52 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Guy Naor.
Original Post: Small Update for the method_missing() Post
Feed Title: Famundo - The Dev Blog
Feed URL: http://devblog.famundo.com/xml/rss/feed.xml
Feed Description: A blog describing the development and related technologies involved in creating famundo.com - a family management sytem written using Ruby On Rails and postgres
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Guy Naor
Latest Posts From Famundo - The Dev Blog

Advertisement

A few days ago I posted a ruby trick with method_missing() to make a child object appear as part of the parent object. Well, I found out I can improve it a bit, to make sure we first check the method_missing() of the parent class, and only then the one of the child class.

The correct code is:

def method_missing(method_id, *args, &block)
  begin
    super
  rescue NoMethodError => e
    raise if !family_setting
    begin
      family_setting.send method_id, *args 
    rescue
      raise e
    end
  end
end

The reason we need this change is to prevent us from calling method_missing() based methods in the child before we try the parent. A good example in rails would be all the find_by_xxx functions which will go to the child instead of the parent.

Read: Small Update for the method_missing() Post

Topic: Cool Things With Ruby Previous Topic   Next Topic Topic: Ruby Method Size

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use