From: Sean O'Halpin Date: 2006-06-17T12:26:10+09:00 Subject: Re: API style preference? a.b=c or a(:b=>c) On 6/16/06, Kirk Haines wrote: > > zoom.Foo = :bar > > zoom(:Foo => :bar) > > Preference? > I prefer the zoom(:Foo => :bar) interface. The reason I prefer it is because you can more easily define your constants in a config file (e.g. YAML) and because it lets you define multiple constants at the same time. BTW, I don't think you ~can~ freeze nil. At least, it seems to have no effect. Here's a quickie implementation (with deep_freeze paranoia): class Object def deep_freeze if self.respond_to? :each self.each do |i| i.deep_freeze end end self.freeze end end class String def deep_freeze freeze end end class Object def constant(hash) target = self.kind_of?(Module) ? self : self.class hash.each do |key, value| target.const_set(key, value.deep_freeze) unless target.const_defined? key end end end constant(:DocRootNames => [nil,'docs','doc','htdocs','htdoc']) constant(:DocRootNames => ['something', 'else']) constant :DocRootNames => ['again'], :Other => 42, :AndAgain => (1..42) p DocRootNames p Other p AndAgain module Foo constant :Bar => :Baz end module Foo constant :Bar => :Boo end p Foo::Bar class Moo constant :Cow => 42 end class Moo constant :Cow => 24 end p Moo::Cow __END__ [nil, "docs", "doc", "htdocs", "htdoc"] 42 1..42 :Baz 42 (Apologies for the appalling names! :) Regards, Sean