Instantly share code, notes, and snippets.

Embed
What would you like to do?
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