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

**URL:** <https://rubytalk.org/t/flatten-rb-check-array-type-and-proxy-class/41628>\
**Category:** ruby-talk\
**Created:** [24 October 2007 05:17 UTC](https://rubytalk.org/t/flatten-rb-check-array-type-and-proxy-class/41628 "2007-10-24T05:17:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tom\_wang](https://avatars.discourse-cdn.com/v4/letter/t/f4b2a3/32.png) [@tom\_wang](https://rubytalk.org/u/tom_wang)\
**Post date:** [24 October 2007 05:17 UTC](https://rubytalk.org/t/flatten-rb-check-array-type-and-proxy-class/41628/1 "2007-10-24T05:17:53Z")

</div>

Hi,

I created a proxy class like this one:

class SavingProxy  
&nbsp;&nbsp;attr\_reader :context  
&nbsp;&nbsp;delegate :class, :to =\> :context  
&nbsp;&nbsp;delegate :is\_a?, :to =\> :context  
&nbsp;&nbsp;delegate :inspect, :to =\> :context  
&nbsp;&nbsp;delegate :type, :to =\> :context  
&nbsp;&nbsp;  
&nbsp;&nbsp;def initialize(context)  
&nbsp;&nbsp;&nbsp;&nbsp;@context = context  
&nbsp;&nbsp;&nbsp;&nbsp;@messages = []  
&nbsp;&nbsp;end

&nbsp;&nbsp;def send\_if\_possible(sym, \*args)  
&nbsp;&nbsp;&nbsp;&nbsp;new\_context = @context.send(sym, \*args) if @context.respond\_to? sym  
&nbsp;&nbsp;&nbsp;&nbsp;@context = new\_context if @context.class == new\_context.class  
&nbsp;&nbsp;&nbsp;&nbsp;new\_context  
&nbsp;&nbsp;end  
&nbsp;&nbsp;  
&nbsp;&nbsp;private  
&nbsp;&nbsp;&nbsp;&nbsp;def method\_missing(method, \*args, &block)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Sending method #{method} to #{@context.name rescue nil}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_context = @context.send(method, \*args, &block)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@context = new\_context if @context.class == new\_context.class  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_context  
&nbsp;&nbsp;&nbsp;&nbsp;ensure  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@messages \<\< [method, args, block, Time.now - start]  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;  
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

> **···**
>
> ---------------------------------  
> Ne gardez plus qu'une seule adresse mail ! Copiez vos mails vers Yahoo! Mail

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [25 October 2007 19:12 UTC](https://rubytalk.org/t/flatten-rb-check-array-type-and-proxy-class/41628/2 "2007-10-25T19:12:03Z")

</div>

Hi,

> **···**
>
> In message "Re: flatten, rb\_check\_array\_type and proxy class" on Wed, 24 Oct 2007 14:17:53 +0900, tom wang \<tomawng@yahoo.fr\> 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?
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matz.
