Ruby is arguably the most meta-programming friendly language of the current dynamic languages since it has no compile time at all, and most constructs in Ruby are available at runtime.
Simply, there is no distinction between the code you are writing and the code that your computer runs, and you can change the code that your computer is running at any time you want.
Is it dangerous?
Does this prevent me doing it?
I don’t think it is any different than
AOP –regarding the complications–, it is also dangerous but we see it as a saviour most of the time. I think
declarative transaction management is one of the best things happened in enterprise programming. Thanks to
I am not saying that just for meta-programming, it is the best fit for everything. There are times/projects that you should not use a dynamic language and should use a statically typed language like
Just a couple of minutes ago, I created a controller in my
RoR app to override a method in
Net::Http to enable mocking. When enabled the controller replaces a method with another one that can read everything from file system instead of making HTTP calls. The best thing is, it just took me 10 minutes to figure it out and code it.
No need for point-cut expressions, just open
module Net class HTTP # do what you want to do it and close end end
No need for around advice, just unbind the method you want to wrap and bind it after wrapping.
module Net class HTTP # unbind the method real_get_response = self.method(:get_response) # redefine the wrapped method with the same name define_method(:get_response) do # do what you want to do before # bind and call the method real_get_response.bind(self.class).call # do what you want to do after end end end