From: "Sean O'Dell" Date: 2003-08-22T07:15:33+09:00 Subject: Re: Wrapping ENV Hal E. Fulton wrote: > I just wrote a little piece of code. Is it useful to anyone but > me? And/or do you see a more elegant way to do it, or any problems > with it? > > I get tired of writing: > > ENV["this"] = x > y = ENV["that"] > > I'd rather write: > > env.this = x > y = env.that > > (Saving me brackets, quotes, and shift key.) > > Here's the code: > > module EnvWrap > ENV.to_hash.each do |name,value| > newname = name.dup.downcase > define_method(newname) { ENV[name] } > define_method(newname+"=") {|val| ENV[name] = val} > end > end > > env = Object.new.extend EnvWrap > > p env.path > env.path = "wocka" > p env.path > > p ENV['PATH'] > > > Cheers, > Hal That's pretty useful! Why not generalize it though: class Record def initialize(source) source.to_hash.each do |name, value| method_name = name.downcase define_method(method_name) { source[name] } define_method(method_name + "=") { |value| source[name] = value } end end end env = Record.new(ENV) p env.path env.path = "wocka" p env.path p ENV['PATH'] Sean O'Dell