can you do this ? ruby aptitute

Discussion in 'Ruby' started by Pokkai Dokkai, Jan 29, 2008.

  1. # in source1.rb
    class C1
    def met
    puts @bb

    # in source2.rb
    require 'source1.rb'
    class C2
    def met


    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( 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

    can any one solve this?
    Pokkai Dokkai, Jan 29, 2008
    1. Advertisements

  2. Pokkai Dokkai

    Luc Heinrich Guest

    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
    Luc Heinrich, Jan 29, 2008
    1. Advertisements

  3. 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 Eklund, Jan 29, 2008

  4. 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)
    def met
    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

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

    thank you
    Pokkai Dokkai, Jan 29, 2008
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.
Similar Threads
There are no similar threads yet.