Marnen Laibow-Koser wrote:

>> 
>> 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 ?
> 

The syntax of a.PAYMENT_TYPES won't work. I am not sure if you meant it 
to work or you were just using it for the sake of explanation.


>> 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?
> 

Without trying to be arguementative, Ruby won't allow this.

class PaymentType

    def payment_types
      ["Check", "Credit Card", "Purchase Order"]
    end

     PAYMENT_TYPES = payment_types

 end

This will error: NoMethodError

But I get your point, which has enhanced my understanding. Things that 
relate to the class in general, things that shouldn't be redefined, like 
Math:PI are controlled only by class methods. So, in the above example, 
if the payment_types were to change dynamically as the program executed, 
I would not use a constant, I would setup getter and setter methods.

>> 
>> 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.

Again, this syntax won't work, if using self.payment_types

pt = PaymentType.new
pt.PaymentType::PAYMENT_TYPES

Error: NoMethodError: undefined method ‘PaymentType’

> 
>> 
>> 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.

See above.


> 
>> 
> 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to