From: Joel VanderWerf Date: 2008-05-24T11:29:37+09:00 Subject: Re: Prevent ruby constant variables from changing? Dave Bass wrote: > Pe単a, Botp wrote: >> i too had this problem, and the closest i can get is to wrap the >> constants in a module and then freeze the module. > > I was inspired by Perl's use of subroutines as wrappers for constants to > come up with the following technique. It doesn't involve modules or > freezing: > > irb(main):001:0> def self.Const > irb(main):002:1> 1 > irb(main):003:1> end > => nil > irb(main):004:0> self.Const > => 1 > irb(main):005:0> self.Const = 2 > NoMethodError: undefined method `Const=' for main:Object > from (irb):5 > irb(main):006:0> self.Const > => 1 > > Anyone see problems? It at least avoids accidental modification of a > constant, and it's simple. you have to stick to the self.Const syntax though. def self.Const; 1; end Const = 2 p Const -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407