Re: Find the fully qualified name of a class from a string
- From: "Kristoffer Lundén" <kristoffer.lunden@xxxxxxxxx>
- Date: Sat, 31 Mar 2007 09:46:01 +0900
On 3/31/07, Nasir Khan <rubylearner@xxxxxxxxx> wrote:
Anyway, there could be several uses for such a thing. Simplest use case
would be a RAILS like (but not RAILS) situation where a controller is
deployed on a running server which (controller) optionally comes as a string
enveloped in a protocol message, this string is evaled and the controller is
instatiated but the system also needs to maintain meta information including
the name of the controller just deployed, which defaults to the fully
qualified name of the class.
My use case is similar.
Here's one way to capture that info as it's evaled. Not thread-safe
as-is. Not even sure if it's useful at all, but I was a bit curious.
There's more ways to do this, of course, especially depending on when
you want to capture this info.
str = <<EOF
module A
class B
def b
puts "hello"
end
end
end
EOF
set_trace_func proc {|event, file, line, id, binding, classname|
return unless event == 'class'
name = eval('self.class == Class && Module.nesting[0]', binding)
if name
# Do something with name
end
}
eval(str)
set_trace_func(nil)
--
Kristoffer Lundén
✉ kristoffer.lunden@xxxxxxxxx
✉ kristoffer.lunden@xxxxxxxxxxxx
http://www.gamemaker.nu/
☎ 0704 48 98 77
- Follow-Ups:
- Re: Find the fully qualified name of a class from a string
- From: Rick DeNatale
- Re: Find the fully qualified name of a class from a string
- References:
- Re: Find the fully qualified name of a class from a string
- From: ara . t . howard
- Re: Find the fully qualified name of a class from a string
- From: Rick DeNatale
- Re: Find the fully qualified name of a class from a string
- Prev by Date: Re: Web-based personal development co. looking for programmers
- Next by Date: Re: New presentation on Ruby
- Previous by thread: Re: Find the fully qualified name of a class from a string
- Next by thread: Re: Find the fully qualified name of a class from a string
- Index(es):
Relevant Pages
|