Flatten, rb_check_array_type and proxy class

Hi,

I created a proxy class like this one:

class SavingProxy
attr_reader :context
delegate :class, :to => :context
delegate :is_a?, :to => :context
delegate :inspect, :to => :context
delegate :type, :to => :context

def initialize(context)
@context = context
@messages = []
end

def send_if_possible(sym, *args)
new_context = @context.send(sym, *args) if @context.respond_to? sym
@context = new_context if @context.class == new_context.class
new_context
end

private
def method_missing(method, *args, &block)
puts “Sending method #{method} to #{@context.name rescue nil}”
new_context = @context.send(method, *args, &block)
@context = new_context if @context.class == new_context.class
new_context
ensure
@messages << [method, args, block, Time.now - start]
end

end

Now my problem is that I would like this proxyclass to behave exactly
like the object it proxies

So for example if I did

[4,5,[1,3], SavingProxy.new([2,3])].flatten

I would like to get this:

[4,5,1,3,2,3]

How could i do that?

I looked a bit and found that in the code for flatten my proxy is not
recognized as a n array by rb_check_array_type.

Is it possible to make rb_check_array_type recognize my proxy as an
array when my proxy proxies an array?

Thanks

Hi,

In message “Re: flatten, rb_check_array_type and proxy class”
on Wed, 24 Oct 2007 14:17:53 +0900, tom wang [email protected]
writes:

|Now my problem is that I would like this proxyclass to behave exactly like the object it proxies
|
|So for example if I did
|
|[4,5,[1,3], SavingProxy.new([2,3])].flatten
|
|I would like to get this:
|
|[4,5,1,3,2,3]
|
|How could i do that?

You have to provide #to_ary method explicitly, since Ruby checks it
via respond_to?

          matz.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs