Panasonic Youth rob sanheim writes about software, business, ruby, music, stuff and things



Posted
14 November 2006 @ 2am

Tagged
Rails, Ruby

Discuss

Whats wrong with this method_missing?

A coworker who shall remain nameless had this method missing implementation in a module that got mixed into the app helper. This tricked me up for longer then it should, mostly because I hadn't synced up in awhile and so assumed the problem was in changes I made. I don't know if it would break anything in production, as I never go that far - a whole bunch of failing tests stopped me before I got that far.

module AdRender
# gets included into ApplicationHelper, which gets included into the entire view layer, etc...

  def method_missing(methodname, *args)
    if methodname =~ /^render_/
      methodname[/^render_/] = ''
      render_ad methodname
    end
  end
end

Do you see whats wrong?


4 Comments

Posted by
Stephan
14 November 2006 @ 2am

Well ‘methodname’ is a Symbol, and a Symbol never matches a Regexp.

Depending on what you try to achieve

methodname.to_s =~ /^render/

will do.


Posted by
Stephan
14 November 2006 @ 2am

Oh and BTW, the never-match hide that fact that


methodname[/^render_/]

would raise a NoMethodError.


Posted by
Rob
14 November 2006 @ 3am

So…you brought a good point that I hadn’t even realized. =) The main issue that I saw was that it didn’t call super, but you are right in that it should convert to string to compare to the regex. So now I’m not sure why its working…


Posted by
Brian K
14 November 2006 @ 5am

it’s written in some cracked-out language ;)


Leave a Comment

Friday Night Randomness Five Common Myths about Working From Home