# "nested" methods

**URL:** <https://rubytalk.org/t/nested-methods/47960>\
**Category:** ruby-talk\
**Created:** [27 July 2008 14:02 UTC](https://rubytalk.org/t/nested-methods/47960 "2008-07-27T14:02:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stefan\_Buhr](https://avatars.discourse-cdn.com/v4/letter/s/59ef9b/32.png) [@Stefan\_Buhr](https://rubytalk.org/u/Stefan_Buhr)\
**Post date:** [27 July 2008 14:02 UTC](https://rubytalk.org/t/nested-methods/47960/1 "2008-07-27T14:02:36Z")

</div>

Hi there,

I want to do this:

@myclass.items[5] = an\_item

But the items object of @myclass should have an custom setter. I have  
tried this:  
class Myclass  
&nbsp;&nbsp;&nbsp;&nbsp;def items  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def []= value  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-code-  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
end

but this does not work. How can i do that?

cheers,  
buhrmi

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Stefan\_Buhr](https://avatars.discourse-cdn.com/v4/letter/s/59ef9b/32.png) [@Stefan\_Buhr](https://rubytalk.org/u/Stefan_Buhr)\
**Post date:** [27 July 2008 14:59 UTC](https://rubytalk.org/t/nested-methods/47960/2 "2008-07-27T14:59:14Z")

</div>

oh... ok

thanks

i hoped there would be a way without an new explicit class

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [27 July 2008 14:59 UTC](https://rubytalk.org/t/nested-methods/47960/3 "2008-07-27T14:59:46Z")

</div>

> I want to do this:
> 
> @myclass.items[5] = an\_item
> 
> But the items object of @myclass should have an custom setter. I have  
> tried this:  
> class Myclass  
> &nbsp;&nbsp;&nbsp;&nbsp;def items  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def = value  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-code-  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> end
> 
> but this does not work.

Your nested method is defined for Myclass. Basically something like nested methods does not exist in Ruby. The only difference is the point in time when the nested method will be defined (after the other method has been executed):

$ irb  
irb(main):001:0\> class M  
irb(main):002:1\> def x  
irb(main):003:2\> def y; 9; end  
irb(main):004:2\> end  
irb(main):005:1\> end  
=\> nil  
irb(main):006:0\> m=M.new  
=\> #\<M:0x7ff8a1e0\>  
irb(main):007:0\> m.y  
NoMethodError: undefined method `y' for #\<M:0x7ff8a1e0\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):7  
irb(main):008:0\> m.x  
=\> nil  
irb(main):009:0\> m.y  
=\> 9  
irb(main):010:0\> m=M.new  
=\> #\<M:0x7ff7888c\>  
irb(main):011:0\> m.y  
=\> 9  
irb(main):012:0\>

> How can i do that?

class Myclass  
&nbsp;&nbsp;&nbsp;def items  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@items ||=   
&nbsp;&nbsp;&nbsp;end  
end

Now you have a member of type Array in Myclass and can use its method =.

You can as well do this

class Myclass  
&nbsp;&nbsp;&nbsp;class Items  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def = idx, val  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# whatever  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def items  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@items ||= Items.new  
&nbsp;&nbsp;&nbsp;end  
end

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 27.07.2008 16:02, Stefan Buhr wrote:

---

<div class="post-metadata">

**Author:** ![Sebastian\_Hungereck1](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastian\_Hungereck1](https://rubytalk.org/u/Sebastian_Hungereck1)\
**Post date:** [27 July 2008 15:17 UTC](https://rubytalk.org/t/nested-methods/47960/4 "2008-07-27T15:17:01Z")

</div>

Stefan Buhr wrote:

> i hoped there would be a way without an new explicit class

There is:  
class Myclass  
&nbsp;&nbsp;def items  
&nbsp;&nbsp;&nbsp;&nbsp;Object.new.instance\_eval do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def = index, value  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-code-  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end  
But using an "explicit class" is clearer.

HTH,  
Sebastian

> **···**
>
> --  
> Jabber: sepp2k@jabber.org  
> ICQ: 205544826

---

<div class="post-metadata">

**Author:** ![Stefan\_Buhr](https://avatars.discourse-cdn.com/v4/letter/s/59ef9b/32.png) [@Stefan\_Buhr](https://rubytalk.org/u/Stefan_Buhr)\
**Post date:** [27 July 2008 15:27 UTC](https://rubytalk.org/t/nested-methods/47960/5 "2008-07-27T15:27:48Z")

</div>

Thank you very much that helped me alot.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
