Skip to content

Instantly share code, notes, and snippets.

@hopsoft
Created January 13, 2011 00:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hopsoft/777156 to your computer and use it in GitHub Desktop.
Save hopsoft/777156 to your computer and use it in GitHub Desktop.
Dynamic Proxy - Illustrates how to wrap and then forward method calls to another object
class Proxy
def initialize(object)
@object = object
end
# forward all calls to the wrapped object
def method_missing(method_name, *args)
@object.send(method_name, *args)
rescue
puts "#{method_name} is not supported by the wrapped object!"
end
end
# usage
Proxy.new("this is a string").length # => 16
Proxy.new([1, 2, 3]).length # => 3
Proxy.new({:a => 1, :b => 2}).length # => 2
Proxy.new(true).length # => length is not supported by the wrapped object!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment