Buzz Hill wrote: > To Everyone, > > Thanks for your help. > > When you are confused about something, it is sometimes hard to distill > all your thoughts down to the essence of the issue and ask the right > question. But I am trying to define a CONSTANT using the result of an > instance method
No you're not. The constant you're trying to define is clearly a class property. If you think about it, this is as it should be. The constant is going to have the same value for every instance of the class. In other words: a = PaymentType.new b = PaymentType.new a and b are now separate instances of PaymentType -- but surely you never want a.PAYMENT_TYPES to be different from b.PAYMENT_TYPES ? > and Ruby just doesn't allow that and I don't understand > why. It does allow it, but a "constant" that's different for different instances is not really a constant and should probably not be represented as such. OTOH, if the constant is *not* different for different instances, then it should be a class property, not an instance property. Either way, you are asking for something that appears to have no utility whatsoever. Why bother? > > Why does it have to be a class method? Is it because a constant is > "class-only". It can't be accessed by an instance. You could define an > instance method that would return the same information as the constant > but there is no syntax to access the constant directly from an instance > object. Sure there is: self.class::CONSTANT. > > So to define a constant, I can use a literal or a class method but not > an instance method. As I explained above, you can use an instance method, but there is no practical value to doing so. > And it can only be accessed via Class::Constant. > > Buzz Best, -- Marnen Laibow-Koser http://www.marnen.org [email protected] -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---

