Created
August 31, 2012 08:29
-
-
Save alisdair/3550245 to your computer and use it in GitHub Desktop.
Invisible proxy example for Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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