Re: Find the fully qualified name of a class from a string



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


Relevant Pages

  • Re: Do it yourself Webserver (Teil 3)
    ... Klauen wir bei Rails die Idee, "Convention over Convention" zu ... Controller implementiert werden können: ... Das Projekt musste ich mit Struts beginnen. ...
    (de.comp.lang.java)
  • Re: Rails for the Rubyist
    ... but I think you'd enjoy Ruby for Rails, ... "In the description of views and controllers, ... use in the template? ...
    (comp.lang.ruby)
  • how much should I pack in a controller (rails)?
    ... I followed "Agile Web Development with Rails". ... In the depot application there is one controller "admin" for the backend and another "store" for the public users. ... If I create just one controller for all the backend administration, that one controller may turn out cluttered with far to many methods (like list_customers, list_contacts, list_projects,... ...
    (comp.lang.ruby)
  • Re: Find the fully qualified name of a class from a string
    ... > would be a RAILS like situation where a controller is ... > deployed on a running server which (controller) optionally comes as a string ... I also check to see if the trace function is running on the ... def classes_defined_in ...
    (comp.lang.ruby)
  • Re: Newbie: Rails Controller Attribute Scope
    ... I'm having a hard time figguring out what is wrong with my controller. ... This is probably better answered on the Rails list, ... assume by new invocation of a method, you mean new request, correct? ...
    (comp.lang.ruby)