Guest User

Untitled

a guest
Jul 22nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. # Faster method, the regex / gsub overhead is invoked only the first time, then an alias is used. I'm not sure that this wouldn't introduce side-effects however. What happens when the original camel-cased method is changed / removed, but it's alias remains inplace?
  2.  
  3. class Object
  4. alias orig_method_missing method_missing
  5. def method_missing name, *args, &block
  6. if name =~ /\A[a-z\d_]+\z/
  7. camel_name = name.gsub(/_(.)/) { $1.upcase }
  8. if self.respond_to? camel_name
  9. # The 'rescue nil' is because macruby seems to do some odd things with classes sometimes (SBElementArray?):
  10. self.class.send :alias_method, name, camel_name rescue nil
  11. return self.send camel_name, *args, &block
  12. end
  13. end
  14. orig_method_missing
  15. end
  16. end
Add Comment
Please, Sign In to add comment