Last active
January 4, 2016 20:29
-
-
Save armw4/8674034 to your computer and use it in GitHub Desktop.
Was curious as to how I could go about implementing this. Alias a method after dynamically defining it. You have to use 'send' here since 'alias_method' is private. Cheers!!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Fixnum.class_eval %Q{ | |
def blank_man | |
"blank man" | |
end | |
} | |
Fixnum.send :alias_method, :blank_man_new, :blank_man | |
puts "they're equal" if 14.blank_man == 14.blank_man_new # => they're equal |
Why would you ever want to do this? I found a reason to do it today. May seem a bit contrived, but you'd be surprised. If your code has equivalent naming conventions, semantics, etc. throughout, then you'll be fine in most cases. But as soon as there's a one off, as was the case today for me, then you'll find yourself in my shoes. Cheers!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It would be nice if you could call:
directly without relying on send, but this isn't too bad :)