# Flatten, rb\_check\_array\_type and proxy class

**URL:** <https://www.ruby-forum.com/t/flatten-rb-check-array-type-and-proxy-class/118145>\
**Category:** Ruby\
**Created:** [October 24, 2007, 7:18am UTC](https://www.ruby-forum.com/t/flatten-rb-check-array-type-and-proxy-class/118145 "2007-10-24T07:18:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tom\_wang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f4b2a3/32.png) [@tom\_wang](https://www.ruby-forum.com/u/tom_wang)\
**Post date:** [October 24, 2007, 7:18am UTC](https://www.ruby-forum.com/t/flatten-rb-check-array-type-and-proxy-class/118145/1 "2007-10-24T07:18:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tom\_wang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f4b2a3/32.png) [@tom\_wang](https://www.ruby-forum.com/u/tom_wang)\
**Post date:** [October 25, 2007, 9:12pm UTC](https://www.ruby-forum.com/t/flatten-rb-check-array-type-and-proxy-class/118145/2 "2007-10-25T21:12:28Z")

</div>

Hi,

In message “Re: flatten, rb\_check\_array\_type and proxy class”  
on Wed, 24 Oct 2007 14:17:53 +0900, tom wang [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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.

```
