From: Ilias Lazaridis Date: 2005-03-26T10:29:49+09:00 Subject: Re: [EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby [from an answer which showed up as a seperate thread] James Edward Gray II wrote: > Begin forwarded message: > >>> # reflection >>> class Talker >>> # simple >>> def sayYourClassName >>> puts self.class.name # the 'self' is optional here >>> end >> >> omitting "self" (puts class.name) leads to an error > > I checked this one myself, because it surprised me when you said it. > You're right of course. I'm assuming it's because class is a method > name and a Ruby keyword. ok >>> # advanced >>> # caller returns a stack (array) of strings of the form # >>> file:linenumber in `method' >>> # so we extract the most recent one and parse the method name out >>> # code from PLEAC >>> def thisMethodName >>> caller[0] =~ /in `([^']+)'/ ? $1 : '(anonymous)'; >>> end >> >> I understand the concept. >> >> is there possibly a more direct solution available, with cleaner code >> and a stable/higher execution speed? > > Have you measured it and proven it too slow? Remember, premature > optimization is the root of all evil in programming. ;) > > I'm not sure what you consider "clean", but getting rid of the ternary > operator may make it a little more readable: > > if caller[0] =~ /in `([^']+)'/ then $1 else '(anonymous)' end I meant something direct like: caller.active_method >>> # expert >>> def sayYourClassDefinition >>> puts "Class:" >>> sayYourClassName >> >> puts "Class #{self.class.name}" >> Class Talker >> >> but >> >> puts "Class #{sayYourClassName}" >> Talker Class >> puts "Class " + sayYourClassName.to_s >> Talker Class >> >> why? > > In the first example, you're asking Ruby for the class name, which you > add to a string that gets printed by the local call to puts. In the > other two, you're calling a method that prints the class name > immediately. Of course [I've overseen the print statements]. > Then the local puts prints "Class " and the return value > from the method call, which isn't meaningful in this context. ok >>> # %{} is another way to write a string literal >> >> #{} - inside strings >> %{} - outside of strings > > No, these are not equivalent. #{...} is for interpolating Ruby code > inside a string. %{...} defined a double quoted string, without the > quotes: > > %{This is a string. I can use "quotes" in here. And #{"interpolate"} > values.} ok, now I understand. %{} => string #{} => string interpolation of code >>> # (looks neat for multiline strings) [...] >>> } >> >> Can I get somehow a more precise reflection of the class definition >> (output similar to the original class-def, excluding code)? > > I don't believe so, no. Remember that a Ruby class can be reopened > and definitions added to it. That means a class could be built up > from many places. > > Ruby does have a means to get it to store the source code it reads, > but I don't believe that's what you were asking for. you're right. - I will try to work this out myself. Where can I find the following information?: * An UML diagramm (or another visual representation) of the ruby class-model. * A deep (but compact) description of the reflection/introspection api's. > James Edward Gray II .. -- http://lazaridis.com