Get the caller of a class

Discussion in 'Ruby' started by Mario Ruiz, Aug 1, 2008.

  1. Mario Ruiz

    Mario Ruiz Guest

    I have something like:

    Class MyClass
    def doit

    caller= How can I do it

    puts 'I was called from: ' + caller.class
    puts 'the value of the @variab is: ' + caller.variab.to_s()
    end
    end

    Class MyCaller
    attr_reader: :variab
    attr_writer: :variab

    def callit
    @variab="example"
    result=MyClass.doit
    end
    end


    Is there any way to do it??

    Thank you
    --
    Posted via http://www.ruby-forum.com/.
     
    Mario Ruiz, Aug 1, 2008
    #1
    1. Advertising

  2. Mario Ruiz

    Mario Ruiz Guest

    Sorry the code was wrong, it should be:

    class MyClass
    def MyClass.doit

    caller= How can I do it

    puts 'I was called from: ' + caller.class.to_s()
    puts 'the value of the @variab is: ' + caller.variab.to_s()
    end
    end

    class MyCaller
    attr_reader :variab
    attr_writer :variab

    def initialize
    puts 'starts'
    end
    def callit
    @variab="example"
    result=MyClass.doit
    end
    end

    MyCaller.new().callit
    --
    Posted via http://www.ruby-forum.com/.
     
    Mario Ruiz, Aug 1, 2008
    #2
    1. Advertising

  3. 2008/8/1 Mario Ruiz <>:

    There is no such thing as the "caller of a class": a class can be used
    by many clients and only methods are actually "called". "caller" is
    actually already defined. See whether this helps:

    15:19:02 bas$ irb
    Ruby version 1.8.7
    irb(main):001:0> def a(n) b(n) end
    => nil
    irb(main):002:0> def b(n) caller(n) end
    => nil
    irb(main):003:0> 3.times {|i| puts i, a(i), "----"}
    0
    (irb):2:in `b'
    (irb):1:in `a'
    (irb):4:in `irb_binding'
    (irb):4:in `times'
    (irb):4:in `irb_binding'
    /usr/lib/ruby/1.8/irb/workspace.rb:52:in `irb_binding'
    /usr/lib/ruby/1.8/irb/workspace.rb:52
    ----
    1
    (irb):1:in `a'
    (irb):4:in `irb_binding'
    (irb):4:in `times'
    (irb):4:in `irb_binding'
    /usr/lib/ruby/1.8/irb/workspace.rb:52:in `irb_binding'
    /usr/lib/ruby/1.8/irb/workspace.rb:52
    ----
    2
    (irb):4:in `irb_binding'
    (irb):4:in `times'
    (irb):4:in `irb_binding'
    /usr/lib/ruby/1.8/irb/workspace.rb:52:in `irb_binding'
    /usr/lib/ruby/1.8/irb/workspace.rb:52
    ----
    => 3


    Kind regards

    robert


    --
    use.inject do |as, often| as.you_can - without end
     
    Robert Klemme, Aug 1, 2008
    #3
  4. Mario Ruiz

    Guest

    This is profoundly unhelpful, I know: but perhaps you should consider
    redesigning your method so that it doesn't care where it was called
    from? That would be more in line with Object Oriented design.

    You might find that it belongs on another class, for example the
    parent class of all the classes that you are currently considering
    calling it from.

    Or you could leave it where it is and pass some sort of mode string, instead:

    def doit(mode)
    case mode
    when :full then ...
    when :quick then ...
    end
    end


    On 8/1/08, Robert Klemme <> wrote:
    > 2008/8/1 Mario Ruiz <>:
    >
    > There is no such thing as the "caller of a class": a class can be used
    > by many clients and only methods are actually "called". "caller" is
    > actually already defined. See whether this helps:
    >
    > 15:19:02 bas$ irb
    > Ruby version 1.8.7
    > irb(main):001:0> def a(n) b(n) end
    > => nil
    > irb(main):002:0> def b(n) caller(n) end
    > => nil
    > irb(main):003:0> 3.times {|i| puts i, a(i), "----"}
    > 0
    > (irb):2:in `b'
    > (irb):1:in `a'
    > (irb):4:in `irb_binding'
    > (irb):4:in `times'
    > (irb):4:in `irb_binding'
    > /usr/lib/ruby/1.8/irb/workspace.rb:52:in `irb_binding'
    > /usr/lib/ruby/1.8/irb/workspace.rb:52
    > ----
    > 1
    > (irb):1:in `a'
    > (irb):4:in `irb_binding'
    > (irb):4:in `times'
    > (irb):4:in `irb_binding'
    > /usr/lib/ruby/1.8/irb/workspace.rb:52:in `irb_binding'
    > /usr/lib/ruby/1.8/irb/workspace.rb:52
    > ----
    > 2
    > (irb):4:in `irb_binding'
    > (irb):4:in `times'
    > (irb):4:in `irb_binding'
    > /usr/lib/ruby/1.8/irb/workspace.rb:52:in `irb_binding'
    > /usr/lib/ruby/1.8/irb/workspace.rb:52
    > ----
    > => 3
    >
    >
    > Kind regards
    >
    > robert
    >
    >
    > --
    > use.inject do |as, often| as.you_can - without end
    >
    >



    --
    Me, I imagine places that I have never seen / The colored lights in
    fountains, blue and green / And I imagine places that I will never go
    / Behind these clouds that hang here dark and low
    But it's there when I'm holding you / There when I'm sleeping too /
    There when there's nothing left of me / Hanging out behind the
    burned-out factories / Out of reach but leading me / Into the
    beautiful sea
     
    , Aug 1, 2008
    #4
  5. 2008/8/1 <>:
    > This is profoundly unhelpful, I know: but perhaps you should consider
    > redesigning your method so that it doesn't care where it was called
    > from? That would be more in line with Object Oriented design.


    Good point! After rereading the original posting I would suggest the
    same. Just pass on the calling instance or the bit of data that the
    method needs. That's what method arguments are for.

    Kind regards

    robert

    --
    use.inject do |as, often| as.you_can - without end
     
    Robert Klemme, Aug 1, 2008
    #5
  6. Mario Ruiz

    Mario Ruiz Guest

    I know that way.... but I would like to know if it is possible to do it
    without passing the argument to the method. :)
    --
    Posted via http://www.ruby-forum.com/.
     
    Mario Ruiz, Aug 1, 2008
    #6
  7. Mario Ruiz

    botp Guest

    On Fri, Aug 1, 2008 at 10:25 PM, Mario Ruiz <> wrote:
    > I know that way.... but I would like to know if it is possible to do it
    > without passing the argument to the method. :)


    try #caller

    botp@jedi-hopeful:~$ qri caller
    ---------------------------------------------------------- Kernel#caller
    caller(start=1) => array
    ------------------------------------------------------------------------
    Returns the current execution stack---an array containing strings
    in the form ``file:line'' or ``file:line: in `method'''. The
    optional start parameter determines the number of initial stack
    entries to omit from the result.

    def a(skip)
    caller(skip)
    end
    def b(skip)
    a(skip)
    end
    def c(skip)
    b(skip)
    end
    c(0) #=> ["prog:2:in `a'", "prog:5:in `b'", "prog:8:in `c'",
    "prog:10"]
    c(1) #=> ["prog:5:in `b'", "prog:8:in `c'", "prog:11"]
    c(2) #=> ["prog:8:in `c'", "prog:12"]
    c(3) #=> ["prog:13"]

    hth.
    kind regards -botp
     
    botp, Aug 1, 2008
    #7
  8. Mario Ruiz

    Pit Capitain Guest

    Mario, there once was a library called binding_of_caller, which I
    think could do what you wanted, but I think it doesn't work anymore
    with recent Ruby versions.

    Regards,
    Pit
     
    Pit Capitain, Aug 2, 2008
    #8
  9. Mario Ruiz

    Mario Ruiz Guest

    Thanks to everybody.

    I thought it could be an easier way to do it so I'll just pass a 'self'
    parameter.

    Thanks.
    --
    Posted via http://www.ruby-forum.com/.
     
    Mario Ruiz, Aug 5, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SG1udA==?=
    Replies:
    5
    Views:
    2,673
    IcingDeath
    Jun 25, 2006
  2. popov
    Replies:
    2
    Views:
    350
    popov
    Dec 10, 2003
  3. David
    Replies:
    9
    Views:
    3,798
    David
    Sep 14, 2006
  4. Wei Li
    Replies:
    6
    Views:
    823
    Wei Li
    Aug 23, 2004
  5. Mark
    Replies:
    2
    Views:
    407
Loading...

Share This Page