On Tue, 14 Jul 2009, Rick DeNatale wrote: > > 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:
I was addressing myself at this point to the general matter of constant scope, visibility, and syntax, which I think was an impediment to analyzing the underlying problem. David -- David A. Black / Ruby Power and Light, LLC Ruby/Rails consulting & training: http://www.rubypal.com Now available: The Well-Grounded Rubyist (http://manning.com/black2) Training! Intro to Ruby, with Black & Kastner, September 14-17 (More info: http://rubyurl.com/vmzN) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---

