Hello.
How can i know a function's caller method? E.g.
class A
def do_stg(n)
b = B.new
b.do_xyz(n)
end
end
class B
def do_xyz(n)
# -> Here i want to know that
# -> This method has been called
# -> From A#do_stg
n + 1
end
end
Kernel#caller does not help.. i don't care what file or line a function
has been called, and i couldn't find nothing helpful on ruby-doc nor by
searching google
Please keep in mind i'd like to know that do_xyz has been called by
do_stg -> WHICH IS A METHOD OF THE "A" CLASS <-
I've always seen that whenever you need something like this, there's
always a better design choice that you can make. Like what would you
do in languages that didn't have caller etc etc.
Its still a hack, (because ideally you shouldn't be needing this IMO),
but you can make the caller send in an extra argument, putting its
description/ID/Name there.
MT
···
On Wed, Mar 19, 2008 at 8:53 PM, Alberto Santini <santini.alberto@gmail.com> wrote:
Hello.
How can i know a function's caller method? E.g.
class A
def do_stg(n)
b = B.new
b.do_xyz(n)
end
end
class B
def do_xyz(n)
# -> Here i want to know that
# -> This method has been called
# -> From A#do_stg
n + 1
end
end
Kernel#caller does not help.. i don't care what file or line a function
has been called, and i couldn't find nothing helpful on ruby-doc nor by
searching google
Please keep in mind i'd like to know that do_xyz has been called by
do_stg -> WHICH IS A METHOD OF THE "A" CLASS <-
but you can make the caller send in an extra argument, putting its
description/ID/Name there.
Of course i can, i just *don't want* to do this. For example, imagine i
want to access a database table that (by -> convention <-) has the same
name of a class. Imagine i have a DBHelper class that contains a
DBHelper#find method used to find records on the database. if
DBHelper#find is called from a Ball object i want to lookup on the
"ball" table, it it's called from a Cube object i want to search the
"cube" table, etc. This is just the simplest usage that came to my mind,
but i guess there are hundreds. Obviously, i can implement DBHelper#find
in a way like this:
def find(table_name = "", criteria = {})
...
end
and call it like this:
find(self.class.to_s, {:id => 5})
It is just *not what i want to do*.
So, coming back to my question, is there a simple way to get the wanted
class name, without haveing to parse Kernel#caller?
I've only ever used it when I'm working with trace statements when a
debugger isn't available or doing the job I need it to.
-- Ben
···
On Wed, Mar 19, 2008 at 9:10 AM, Alberto Santini <santini.alberto@gmail.com> wrote:
> but you can make the caller send in an extra argument, putting its
> description/ID/Name there.
Of course i can, i just *don't want* to do this. For example, imagine i
want to access a database table that (by -> convention <-) has the same
name of a class. Imagine i have a DBHelper class that contains a
DBHelper#find method used to find records on the database. if
DBHelper#find is called from a Ball object i want to lookup on the
"ball" table, it it's called from a Cube object i want to search the
"cube" table, etc. This is just the simplest usage that came to my mind,
but i guess there are hundreds. Obviously, i can implement DBHelper#find
in a way like this:
def find(table_name = "", criteria = {})
...
end
and call it like this:
find(self.class.to_s, {:id => 5})
It is just *not what i want to do*.
So, coming back to my question, is there a simple way to get the wanted
class name, without haveing to parse Kernel#caller?
but you can make the caller send in an extra argument, putting its
description/ID/Name there.
Of course i can, i just *don't want* to do this. For example, imagine i want to access a database table that (by -> convention <-) has the same name of a class. Imagine i have a DBHelper class that contains a DBHelper#find method used to find records on the database. if DBHelper#find is called from a Ball object i want to lookup on the "ball" table, it it's called from a Cube object i want to search the "cube" table, etc.
Perhaps your DBHelper class would be better designed as a module that is included in the Ball/Cube.etc classes, or extends its instances?
I don't know, but maybe you can implement the methods you *want* as a
module method, and use them in your classes that you need such
functionality built-into, which would ensure they'll be able to know
which class they're under whenever they're called.
like
module M
def foo
puts self.class
end
end
class A
include M
end
class B
include M
end
a= A.new
a.foo => A
b= B.new
b.foo => B
I might be completely off base here, but i'll still *try* to give you
a design based resolution, more often than not, that causes less
problems in the long run.