Skip to content

Instantly share code, notes, and snippets.

@alisdair
Created August 31, 2012 08:29
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alisdair/3550245 to your computer and use it in GitHub Desktop.
Save alisdair/3550245 to your computer and use it in GitHub Desktop.
Invisible proxy example for Ruby
class FlatArray
instance_methods.each do |m|
undef_method(m) unless m =~ /(^__|^nil\?|^send$|^object_id$)/
end
def initialize(array)
@target = array
end
def respond_to?(symbol, include_priv=false)
@target.respond_to?(symbol, include_priv)
end
def <<(object)
if object.is_a? Array
@target += object
else
@target << object
end
end
private
def method_missing(method, *args, &block)
@target.send(method, *args, &block)
end
end
a = [1, 2, 3, 4]
puts "a.class = #{a.class}"
puts "a = #{a.inspect}"
a << [5, 6, 7]
puts "a = #{a.inspect}"
b = FlatArray.new([1, 2, 3, 4])
puts "b.class = #{b.class}"
puts "b = #{b.inspect}"
b << [5, 6, 7]
puts "b = #{b.inspect}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment