# Can you do this ? ruby aptitute

**URL:** <https://www.ruby-forum.com/t/can-you-do-this-ruby-aptitute/127449>\
**Category:** Ruby\
**Created:** [January 29, 2008, 10:47am UTC](https://www.ruby-forum.com/t/can-you-do-this-ruby-aptitute/127449 "2008-01-29T10:47:22Z")\
**Posts on this page:** 4\
**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:** [January 29, 2008, 10:47am UTC](https://www.ruby-forum.com/t/can-you-do-this-ruby-aptitute/127449/1 "2008-01-29T10:47:22Z")

</div>

# in source1.rb

class C1  
def met  
@bb  
puts @bb  
end  
end

# in source2.rb

require ‘source1.rb’  
class C2  
def met  
@aa=10  
C1.new.met  
end  
end

#-------------

see above source code

how to pass @aa(in C2) value to @bb(in C1) ?  
@aa is a dynamic value

not allowed conditions are

1. class variable ,global variable are NOT allowed
2. passing arguments is not allowed( C1.new.met(@aa) is not allow)
3. there is no inheritance relationship between class C1,C2
4. should not modify class C2 and source2.rb

allowed conditions are

1. instance variable ,local variable are allowed
2. iterators ,callback are allowed
3. allowed to modify class C1 and source1.rb (if needed)
4. others are allowed

i got this aptitude ruby question from my friend(he got it from an  
interview)

can any one solve this?

---

<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:** [January 29, 2008, 11:43am UTC](https://www.ruby-forum.com/t/can-you-do-this-ruby-aptitute/127449/2 "2008-01-29T11:43:10Z")

</div>

On 29 janv. 08, at 10:47, Pokkai D. wrote:

> how to pass @aa(in C2) value to @bb(in C1) ?  
> @aa is a dynamic value

I first tried with various uses of Binding but couldn’t get it to work.

This super-ugly hack works though;

# in source1.rb

class C2; end  
class C1  
def met  
ObjectSpace.each\_object(C2) { |o| @bb = o.instance\_variable\_get  
(:@aa) }  
puts @bb  
end  
end

---

<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:** [January 29, 2008, 1:12pm UTC](https://www.ruby-forum.com/t/can-you-do-this-ruby-aptitute/127449/3 "2008-01-29T13:12:44Z")

</div>

On Jan 29, 2008 10:47 AM, Pokkai D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class C2
> 
> 1. instance variable ,local variable are allowed
> 2. iterators ,callback are allowed
> 3. allowed to modify class C1 and source1.rb (if needed)
> 4. others are allowed
> 
> i got this aptitude ruby question from my friend(he got it from an  
> interview)

You can open class C2 (or Object), intercept the creation of C2#met  
using method\_added, and alias C2#met to your own method, thus allowing  
passing of the relevant C2 object using thread variables.

You can use set\_trace\_func to grab the data at the point of call.

You can use ObjectSpace to get at the object (as Luc H.)  
mentions, though it’s hard to know you’ve got the right C2 object.

You can tell the interviewer that you dislike the question; it’s just  
whether you’ve memorized Ruby internals _for doing ugly hacks_, and  
that the only sane thing to do is to add a parameter to the call, or,  
if this is an external library, either mail the author of the library  
and explain your situation and why you need the parameter, or take the  
library “into custody” and maintain it yourself including changes (in  
your own version control repository).

Eivind.

---

<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:** [January 29, 2008, 1:18pm UTC](https://www.ruby-forum.com/t/can-you-do-this-ruby-aptitute/127449/4 "2008-01-29T13:18:07Z")

</div>

Luc H. wrote:

> On 29 janv. 08, at 10:47, Pokkai D. wrote:
> 
> > how to pass @aa(in C2) value to @bb(in C1) ?  
> > @aa is a dynamic value
> 
> I first tried with various uses of Binding but couldn’t get it to work.
> 
> This super-ugly hack works though;
> 
> # in source1.rb
> 
> class C2; end  
> class C1  
> def met  
> ObjectSpace.each\_object(C2) { |o| @bb = o.instance\_variable\_get  
> (:@aa) }  
> puts @bb  
> end  
> end

nice answer

i tried many times but i can’t

i got some confusing about this oops concept.

is there anyway to get the “current parent object instance variable” in  
“child object instance variable” ?

for example in above problem like

class C2  
def initialize(aa)  
@aa=aa  
end  
def met  
C1.new.met  
end  
end  
obj1=C2.new(1)  
obj2=C2.new(2)  
obj3=C2.new(3)  
obj1.met --\> 1  
obj2.met --\> 1  
obj3.met --\> 1

but the answer should be like  
obj1.met —\> 1  
obj2.met —\> 2  
obj3.met —\> 3

## do you have any idea to get like this answer ?

i have one more doublt

if there is a object “parent\_obj”.It may be any type of Class ( not C2  
only ).  
now assign parent\_obj.aa=10  
inside “parent\_obj” have a “child\_obj” ( like this parent\_obj.child\_obj  
)

now how to assign (aa=10) into parent\_obj.child\_obj.bb

this can be done as you told . but that is for some particular class  
only  
for example like C2 ( ObjectSpace.each\_object(C2) )

thank you
