Peña, Botp
7/28/2006 9:39:00 AM
fr Sy:
# I want to simplify this hash creation:
# g = { 1 => "test", 2 => "test", }
# ...
# however, I don't like the way my code looks.. I want to be able to
# specify the duplication within the hash generation.
not sure if this simple idea will help, but,
irb(main):055:0> class Hash
irb(main):056:1> def init2 range
irb(main):057:2> range.each{|c| self[c]=self.default}
irb(main):058:2> end
irb(main):059:1> end
=> nil
irb(main):060:0> h=Hash.new
=> {}
irb(main):065:0> h.default = "test"
=> "test"
irb(main):066:0> h.init2 1..8
=> 1..8
irb(main):067:0> h
=> {5=>"test", 6=>"test", 1=>"test", 7=>"test", 2=>"test", 8=>"test", 3=>"test", 4=>"test"}
irb(main):068:0> h.default = "testing again"
=> "testing again"
irb(main):069:0> h.init2 6..10
=> 6..10
irb(main):070:0> h
=> {5=>"test", 6=>"testing again", 1=>"test", 7=>"testing again", 2=>"test", 8=>"testing again", 3=>"test", 9=>"testing again", 4=>"test", 10=>"testing again"}
irb(main):071:0>
kind regards -botp