Skip to content

Instantly share code, notes, and snippets.

@armw4
Last active January 4, 2016 20:29
Show Gist options
  • Save armw4/8674034 to your computer and use it in GitHub Desktop.
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!!
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
@armw4
Copy link
Author

armw4 commented Jan 28, 2014

It would be nice if you could call:

Fixnum.alias_method :blank_man_new, :blank_man

directly without relying on send, but this isn't too bad :)

@armw4
Copy link
Author

armw4 commented Jan 28, 2014

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