# How to get the name of parent object inside child object?

**URL:** <https://www.ruby-forum.com/t/how-to-get-the-name-of-parent-object-inside-child-object/127772>\
**Category:** Ruby\
**Created:** [February 1, 2008, 6:05am UTC](https://www.ruby-forum.com/t/how-to-get-the-name-of-parent-object-inside-child-object/127772 "2008-02-01T06:05:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [February 1, 2008, 6:05am UTC](https://www.ruby-forum.com/t/how-to-get-the-name-of-parent-object-inside-child-object/127772/1 "2008-02-01T06:05:28Z")

</div>

how to get the name of parent object inside child object without passing  
any

arguments to child object(global variable is not allowed) ?

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [February 1, 2008, 12:04pm UTC](https://www.ruby-forum.com/t/how-to-get-the-name-of-parent-object-inside-child-object/127772/2 "2008-02-01T12:04:24Z")

</div>

i don’t know exactly what you mean, but…  
Given object x try:

puts x.class.superclass

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [February 1, 2008, 3:17pm UTC](https://www.ruby-forum.com/t/how-to-get-the-name-of-parent-object-inside-child-object/127772/3 "2008-02-01T15:17:11Z")

</div>

Raffaele Tesi wrote:

> i don’t know exactly what you mean, but…  
> Given object x try:
> 
> puts x.class.superclass

class Cycle  
@brand=“something”  
def initialize  
end  
end  
class Wheel  
@brand  
def option  
Cycle.new  
end  
end

> w=Wheel.new  
> @brand —\>nil  
> w.option  
> @brand —\>“something”

i need a requirement like above one example.

should not passing any arguments to child object(w.option)  
global variable is not allowed.

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [February 1, 2008, 4:14pm UTC](https://www.ruby-forum.com/t/how-to-get-the-name-of-parent-object-inside-child-object/127772/4 "2008-02-01T16:14:19Z")

</div>

# something like this?

class Cycle  
attr\_accessor :brand  
def initialize  
@brand=“something”  
end  
end  
class Wheel  
def option  
Cycle.new  
end  
end

w=Wheel.new  
puts w.option.brand

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [February 2, 2008, 6:29am UTC](https://www.ruby-forum.com/t/how-to-get-the-name-of-parent-object-inside-child-object/127772/5 "2008-02-02T06:29:26Z")

</div>

thank for the quick reply

sorry i wrote wrong explanation  
this is my correct explanation

class Cycle  
@brand=nil  
def initialize  
end  
end  
class Wheel  
@brand=“something”  
def option  
Cycle.new  
end  
end

> w=Wheel.new  
> w.brand —\>something  
> c=Cycle.new  
> c.brand —\>nil

> w.option.brand —\>something

any idea ?
