On Tue, Jul 14, 2009 at 8:46 AM, David A. Black<[email protected]> wrote:
>
> I think you're overthinking it. There's no inherent connection between
> constants and class methods.
>
> module MyMathModule
> PI = 3.14159265358979
> end
>
> puts "PI is #{MyMathModule::PI}"
>
> I've created a module and a constant inside that module. The constant
> is resolved using the :: operator, and no methods are involved.
>
> If you define an instance method, you can use the class's constants:
>
> class Rainbow
> COLORS = %w{ red orange yellow green blue indigo violet }
>
> def show_me_the_colors
> puts "My colors are: #{COLORS.join(", ")}"
> end
> end
>
> r = Rainbow.new
> r.show_me_the_colors
>
> => My colors are: red, orange, yellow, green, blue, indigo, violet
I was going to say that defining a class constant like this,
initialized by an expression evaluated when the class definition is
executed is normally the way to go.
But then I went back an looked at the post at the beginning of this
thread, and looked at the code, which when rewritten to look something
like this:
def self.get_payment_types
payment_types_all = find(:all, :select => "display_name,
stored_name" , :order => :display_name)
payment_types = payment_types_all.map {|item| [item.display_name,
item.stored_name]}
end
# must be defined after the method. Can't be defined in a method
PAYMENT_TYPES = find(:all, :select => "display_name, stored_name" ,
:order => :display_name)..map {|item| [item.display_name,
item.stored_name]}
And note that since we are really getting the payment types from the
database, a constant might not be the right path.
If the payment types can change over time, then it's not a constant.
If it is a constant maybe the database isn't the best place to save it.
--
Rick DeNatale
Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---