2

I'm so lost. I know how to use caller to get the caller method, but what do you use the get the caller class?

For example:

class Testing
  def return_caller_class
    return caller_class
  end
end

class Parent

  attr_accessor :test_me

  def initialize      
    self.test_me =  Testing.new
  end

end

class Child < Parent
end

class GrandChild < Child
end

test_Parent = Parent.new
test_Child = Child.new
test_GrandChild = GrandChild.new

puts test_Parent.test_me.return_caller_class     => Parent
puts test_Child.test_me.return_caller_class      => Child
puts test_GrandChild.test_me.return_caller_class => GrandChild

Thank you!!!

Edit:

I've tried to do the following

class Testing
  def return_caller_class
    return caller[0][/`.*'/][1..-2]
  end
end

And the output is:

{"
"=>Parent}    
{"
"=>Child}
{"
"=>GrandChild}

To explain better about my question.

I would the output to display this instead

Parent
Child
GrandChild
Kara
  • 6,115
  • 16
  • 50
  • 57
user3163916
  • 318
  • 4
  • 16

2 Answers2

2

I'm a bit out of my depth with this question, but I think you have made a few mistakes unrelated to the problem of getting the caller's class name. If I can help you with those things, at least you might be a step closer (if a solution is even possible)!

Firstly, it seems to me that you're calling return_caller_class from the main program object, not from one of those three objects you created. You have an object of class Testing inside an object of class Parent, but the method call is outside of both.

Secondly, the only reason you seem to be getting anything close to what you want (when you get output like "=>Parent} has nothing to do with the return_caller_class method. It seems as though you inadvertently created little hashes in the last three lines of your program (when you added => Parent, etc), which are being output with puts. (Confirmed here: Has #puts created a new hash?) If these are meant to be comments, they need a # before them.

PS. I found a link to this gem on another thread: https://github.com/asher-/sender. Might be worth checking out.

Community
  • 1
  • 1
Kal
  • 2,098
  • 24
  • 23
  • Thanks for pointing out where I got my assumption wrong. And I'll check out that link. – user3163916 Jan 15 '14 at 21:36
  • I tried return method(__callee__).owner and it returns 'Testing;, I'll check more info. from that link. I might find how to return the other class name. – user3163916 Jan 15 '14 at 21:41
  • I'll continue my research tomorrow. I got something in the way this afternoon that I had to pause my research. – user3163916 Jan 16 '14 at 05:56
  • Thanks the gem works nicely, but I'd prefer not relying on the gem and sadly after reading the C library, I couldn't translate it into ruby, oh well. – user3163916 Jan 16 '14 at 21:06
  • Oh well, put it down to a learning experience, and hopefully you can find an alternative and more idiomatic Ruby way of accomplishing what you need to. – Kal Jan 17 '14 at 02:15
  • the git project whose thread is linked to in this post is no longer maintained. – La-comadreja Jun 16 '15 at 19:09
0
class Testing
  def return_caller_class
    self.class.name
  end
end

class ChildOne < Testing
end

class ChildTwo < Testing
end

result:
------------------------------------------------
>ChildOne.new.return_caller_class
 => "ChildOne"
>ChildTwo.new.return_caller_class
 => "ChildTwo"
>Testing.new.return_caller_class
 => "Testing"
  • 3
    Please give a brief explanation as to why this code works, big code dumps without explanation can be very confusing – Jeeter Jul 22 '16 at 21:58
  • 3
    This is neither big code nor confusing. It is self explanatory. –  Dec 18 '16 at 23:38