can you do this ? ruby aptitute

P

Pokkai Dokkai

# 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?
 
L

Luc Heinrich

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
 
E

Eivind Eklund

# 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)

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 Heinrich)
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.
 
P

Pokkai Dokkai

Luc said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top