11 Mar '17, 10am

Always Define respond_to_missing? When Overriding method_missing

method_missing is still considered scary, but here’s something more scary: forgetting to override respond_to? . Whoa, right? Just gave you an anxiety attack there. How about #method , though? Does that still work? Go ahead, try it. Here, I’ll try it with you: require 'ostruct' class Order def user @_user ||= OpenStruct.new(name: 'Mike', age: 28, occupation: 'slacker') end def method_missing(method_name, *arguments, &block) if method_name.to_s =~ /user_(.*)/ user.send($1, *arguments, &block) else super end end def respond_to?(method_name, include_private = false) method_name.to_s.start_with?('user_') || super end end OK now I’ll load that into irb and play with it: ruby-1.9.2-p290> order = Order.new => #<Order:0x00000001e40948> ruby-1.9.2-p290> order.user_name => "Mike" ruby-1.9.2-p290> order.respond_to?(:user_name) => true ruby-1.9.2-p290> order.method(:user_name) NameErro...

Full article: https://robots.thoughtbot.com/always-define-respond-to-mi...

Tweets

Grass Is Always Greener On The Other Side… + CD...

thefinance.sg 12 Mar '17, 4am

Grass Is Always Greener On The Other Side… + CDW Holdings FY16Q4, TTI’s Thoughts March 12, 2017 by The ThumbTack investor ...

via @grist

via @grist

grist.org 12 Mar '17, 10am

This story was originally published by Fusion and is reproduced here as part of the Climate Desk collaboration. It’s easy ...