-
-
Save mneedham/573074 to your computer and use it in GitHub Desktop.
| Trying to create a little proxy around a class: | |
| class Bar | |
| def self.foo | |
| "mark" | |
| end | |
| end | |
| It works by opening the class: | |
| class Bar | |
| class << self | |
| alias_method :orig, :foo | |
| def foo | |
| p "i am overriden" | |
| orig | |
| end | |
| end | |
| end | |
| But I want to only create the proxy on a method call so trying to use 'define_method' instead: | |
| Bar.send(:alias_method, :orig, :foo) | |
| But that fails saying: | |
| NameError: undefined method `foo' for class `Bar' | |
| from (irb):11:in `alias_method' | |
| from (irb):11:in `send' | |
| from (irb):11 | |
| from :0 | |
| Must be doing something obvious wrong but not sure what |
foo is not an instance method of Bar so it won't work. You'll need to use instance_eval or class_eval on Bar or the above technique of metaclass (never seen it before, thanks joelash).
Ok I get it now. Shouldn't the metaclass be called the eigen class though?
Yes they are synonyms. In my experience you'll hear people call it either.
OK mark just pointed out a typo in my example and I cannot edit it on an iPhone so I'll fix it when I get to a real computer but the secon to last line should be
Bar.metaclass.send :alias_method, :orig, :foo
joelash: They aren't synonyms, though in ruby they are often used as such. In this case eigenclass would be more accurate iirc, though I used metaclass.
kalwre: you're right I should have been more specific in saying in ruby they're considered synonyms. Thanks for the correction!
I think you need call the send on the metaclass of bar. Something like this works....