First the definition: Proxies are objects that masquerade as some other object that’s contained within them, effectively intercepting all messages to the contained object. Proxies are used in multiple places like debugging, tracing, intercepted delegation, benchmarking, etc. But those have already been solved. This post is not about that.
class Proxy instance_methods.each do |m| undef_method m unless m.to_str =~ /^__(send|id)__$/ end def initialize obj @obj = obj end def method_missing id, *opts @obj.__send__ id, *opts end end
That’s the simplest you are going to get. First the Proxy class undef’s all the instance methods that it inherits from Object. Then we override method_missing to intercept the method that’s being invoked on us, along with the arguments and dispatch them to the contained object. How does it work?
proxy1 = Proxy.new(42) p proxy1/42 # => 1 proxy2 = Proxy.new("hello") p proxy2[1,3] # => "ell"
So far so good and it works as expected. We can make the Proxy object behave like an Fixnum or a String What if we try the following?
proxy = Proxy.new("hello") proxy.each { |ch| p ch } proxy.rb:11:in `each': no block given (LocalJumpError) from proxy.rb:11:in `__send__' from proxy.rb:11:in `method_missing'
Oops, what happened there? Ruby passes in the block of code to method_missing and we are not sending this into the proxied object’s each method. When it invokes yield, this causes an exception to be thrown since it doesn’t have the block. Hmm…Turns out there’s a pretty clean way to solve this. We modify method_missing as follows:
def method_missing id, *opts, &block return @obj.__send__ id, *opts if block.nil? @obj.__send__(id, *opts) { |*args| block.call *args } end
I didn’t realize this before, but there are two things going on here. The first one is the * argument is not greedy and preserves the code block that you can trap as a separate argument. The second one is to have * arguments in proc’s. Together they give us what we need. We save the proc, if any, sent to the method_missing and then pass that along with the __send__. Closures take care of the rest and with the wildcarded arguments, we don’t have to worry about the number of arguments that yield is going to be invoked with.

Pingback: names of famous dancers hip hop
Pingback: guaranteed rankings
Pingback: best supplements for muscle gain
Pingback: Dallas Video Production
Pingback: Kia Picanto
Pingback: Dallas Wedding Photographers
Pingback: Flying Cars
Pingback: bodybuilding supplement
Pingback: webcam chat
Pingback: scary
Pingback: Phil Cannella
Pingback: Phil Cannella
Pingback: Mayan Palace Acapulco
Pingback: internet marketing forum
Pingback: movie start times
Pingback: Bajaj Allianz Life Insurance
Pingback: flights to pakistan
Pingback: Lowcost Cruises Deals
Pingback: noclegi zakopane
Pingback: magic flight launch box
Pingback: Bryce Preas
Pingback: HIFI
Pingback: Epson 3010 review
Pingback: izvestni
Pingback: Low Cost Payday Loans
Pingback: watch free movies online
Pingback: fast weight loss tips
Pingback: help me find a job
Pingback: Herbal Incense Blends
Pingback: Legal Herbal Potpourri
Pingback: target sms coupons
Pingback: how to start a cleaning business
Pingback: Spice Herbal Blend
Pingback: Smoke Herbal Blend
Pingback: Bangkok flowers
Pingback: now habit
Pingback: free ipad
Pingback: ipad sales tool
Pingback: Atlanta Garage Door Service
Pingback: legal protection insurance
Pingback: Altamonte Springs chiropractor
Pingback: Webwinkel starten
Pingback: Mobile Money Bandit scam
Pingback: PPCGB
Pingback: Mats
Pingback: Blitzeranwalt
Pingback: followers
Pingback: olcso mobiltelefonok
Pingback: webcam chat
Pingback: eternity rings
Pingback: engagement rings in san diego
Pingback: residential heating and air salt lake
Pingback: modern furniture austin texas
Pingback: dunkin doughnuts
Pingback: hide my ass vpn
Pingback: black hairstyles
Pingback: ingilizce ogreniyorum
Pingback: Holiday Homes
Pingback: price of scrap gold
Pingback: Jake Reeds
Pingback: buy neopoints
Pingback: hardship letter
Pingback: MEDITATION AND KARMA
Pingback: suplimente
Pingback: investment property
Pingback: Jesse V.
Pingback: Organo Gold
Pingback: Join project payday
Pingback: bingo cards for sale
Pingback: web hosting reviews
Pingback: designer engagement rings
Pingback: spamfilter
Pingback: Escort girl Paris
Pingback: Kleintierzubeher
Pingback: syntha six protein powder
Pingback: send flowerrs to sri lanka
Pingback: tanie noclegi zakopane
Pingback: Meet and Greet Parking
Pingback: security guard training
Pingback: Solar Powered Air Conditioner
Pingback: LUXURY HOMES IN CALIFORNIA
Pingback: organic skincare
Pingback: Manchester bootcamp
Pingback: hybrid car
Pingback: gynexin
Pingback: jiu jitsu tampa
Pingback: buy neopoints
Pingback: Pacquiao vs Mayweather
Pingback: proothish istoselidon
Pingback: PowerPoint Course
Pingback: icf scaffolding
Pingback: motivation for weight loss
Pingback: ROV Training
Pingback: how to scrunch hair overnight
Pingback: vw t5 transporter
Pingback: Wholesale Clothing
Pingback: electricians
Pingback: website builder
Pingback: backlinks
Pingback: Scientific Calculator
Pingback: NYC Hotels
Pingback: connections
Pingback: normal cholesterol levels
Pingback: Laser Hair Removal
Pingback: bartschneider test
Pingback: Scott Tucker Payday Loans
Pingback: Scott Tucker Payday Loans
Pingback: led light manufacturers
Pingback: Water Softener
Pingback: electrical installations
Pingback: Husband Pillow
Pingback: physical therapy
Pingback: Halloween costumes
Pingback: iphone 4 cases
Pingback: Dumpsters Atlanta
Pingback: Used Cars Chesterfield
Pingback: holiday home insurance
Pingback: medical job search
Pingback: how to do magic
Pingback: accounting coop
Pingback: Scott Tucker Racing
Pingback: Scott Tucker Racing
Pingback: Scott Tucker Racing
Pingback: Scott Tucker CBS
Pingback: Scott Tucker Leawood
Pingback: passive income
Pingback: SEO Link Monster bonus
Pingback: BPO Software
Pingback: cleaning services Cardiff
Pingback: Scott Tucker Payday Loans
Pingback: Scott Tucker Payday Loans
Pingback: leather wallets for men
Pingback: gynexin
Pingback: SEO Link Monster
Pingback: car hire direct
Pingback: corporate video production nyc
Pingback: Justin Bieber Baby
Pingback: cellulite treatment
Pingback: Scott Tucker Racing
Pingback: football training
Pingback: BPO Companies
Pingback: broker price opinion
Pingback: mopar engines
Pingback: spinach health benefits
Pingback: motorcycle jackets