Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Ruby
Knowing which subclass called a superclass class method
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="David A. Black, post: 4609711"] Hi -- Do you mean self.name, in both cases? I assume that you've done: class A < S end A.doSomething # or do_something, using Ruby style naming and that you didn't really get magically-inserted spaces :-) The code inside a class definition gets executed when the class definition is read in. Code at the top level of the definition, like your puts statement, gets executed right away. Method definitions get executed in the sense that the methods get defined, but the methods themselves don't get executed until they're called. So your definition of S does two things: * prints out "1" + self.name * defines a method, to be called later If you want to do the puts'ing for a subclass, you could do: class S def self.inherited(c) c.class_eval { puts "1" + self.name } end end (or just puts "1" + c.name) David [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Ruby
Knowing which subclass called a superclass class method
Top