Meta-Programming in Ruby – So Easy, So Elegant

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?

hell yes.

Does this prevent me doing it?

hell no.

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 AOP and Spring.

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 Java instead.

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 Net::HTTP class.

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

Read More Post

STAY CONNECTED

Most Popular

Why Serviced Apartments Are the Perfect Choice for Long-Term Stays in Singapore – Everything You Need to Know
17 Dec

Why Serviced Apartments Are the Perfect Choice for Long-Term Stays in Singapore – Everything You Need to Know

If you’re heading to Singapore for a few weeks or many months, maybe for work or study or just to

Manifesting Your Dream Career: Finding Your Passion And Purpose
01 Aug

Manifesting Your Dream Career: Finding Your Passion And Purpose

Are you passionate about finding your dream career? Manifesting can help you align your career with your passions and purpose.

Revolutionizing Your Ride: Expert Auto Service At Carolina Auto Service
27 Feb

Revolutionizing Your Ride: Expert Auto Service At Carolina Auto Service

Finding a reliable auto service provider is paramount in the bustling world of vehicle maintenance and repair. That's where Carolina

Floratam: The Robust Grass Solution From Council Growers SOD
25 Jan

Floratam: The Robust Grass Solution From Council Growers SOD

Floratam grass, a popular St. Augustine variety, has become a go-to choice for homeowners and landscapers seeking a lush, durable

Carolina Mobile Autoservice: Your Go-To For Expert Brake Service Repair
25 Jan

Carolina Mobile Autoservice: Your Go-To For Expert Brake Service Repair

For vehicle owners, the braking system is among the most critical components for safety and performance. Carolina Mobile Autoservice, a

Safeguarding Your Property: Expert Water Damage Services By MBC Capital
24 Jan

Safeguarding Your Property: Expert Water Damage Services By MBC Capital

Water damage can significantly impact homes and businesses, often leaving a trail of destruction and disruption in its wake. Understanding