# "nested" methods

**URL:** <https://www.ruby-forum.com/t/nested-methods/144250>\
**Category:** Ruby\
**Created:** [July 27, 2008, 4:02pm UTC](https://www.ruby-forum.com/t/nested-methods/144250 "2008-07-27T16:02:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![stefan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@stefan](https://www.ruby-forum.com/u/stefan)\
**Post date:** [July 27, 2008, 4:02pm UTC](https://www.ruby-forum.com/t/nested-methods/144250/1 "2008-07-27T16:02:51Z")

</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  
def items  
def []= value  
-code-  
end  
end  
end

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

cheers,  
buhrmi

---

<div class="post-metadata">

**Author:** ![stefan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@stefan](https://www.ruby-forum.com/u/stefan)\
**Post date:** [July 27, 2008, 4:47pm UTC](https://www.ruby-forum.com/t/nested-methods/144250/2 "2008-07-27T16:47:07Z")

</div>

Stefan B. wrote:

> def []= value  
> -code-  
> end  
> end  
> end
> 
> but this does not work. How can i do that?

class MyArray \< Array  
def []= index, value  
super  
p ‘hi’  
end  
end

class Myclass  
attr\_accessor :items  
end

m = Myclass.new  
m.items = MyArray.new  
m.items[5] = “blah”

---

<div class="post-metadata">

**Author:** ![stefan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@stefan](https://www.ruby-forum.com/u/stefan)\
**Post date:** [July 27, 2008, 4:59pm UTC](https://www.ruby-forum.com/t/nested-methods/144250/3 "2008-07-27T16:59:31Z")

</div>

oh… ok

thanks

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

---

<div class="post-metadata">

**Author:** ![stefan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@stefan](https://www.ruby-forum.com/u/stefan)\
**Post date:** [July 27, 2008, 5:01pm UTC](https://www.ruby-forum.com/t/nested-methods/144250/4 "2008-07-27T17:01:45Z")

</div>

On 27.07.2008 16:02, Stefan B. wrote:

> ```
> end
> 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\>  
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  
def items  
@items ||= []  
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  
class Items  
def []= idx, val  
# whatever  
end  
end

def items  
@items ||= Items.new  
end  
end

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![stefan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@stefan](https://www.ruby-forum.com/u/stefan)\
**Post date:** [July 27, 2008, 5:22pm UTC](https://www.ruby-forum.com/t/nested-methods/144250/5 "2008-07-27T17:22:14Z")

</div>

Stefan B. wrote:

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

There is:  
class Myclass  
def items  
Object.new.instance\_eval do  
def []= index, value  
-code-  
end  
self  
end  
end  
end  
But using an “explicit class” is clearer.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![stefan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@stefan](https://www.ruby-forum.com/u/stefan)\
**Post date:** [July 27, 2008, 5:28pm UTC](https://www.ruby-forum.com/t/nested-methods/144250/6 "2008-07-27T17:28:04Z")

</div>

Thank you very much that helped me alot.
