From: Stefan Rusterholz Date: 2009-01-29T01:13:42+09:00 Subject: Re: how can I switch over an array ? Tsunami Scripter wrote: > I have the following code snippet : > > def call(char,value) > case char > when " ","#","!","@","$","%","^","&","*","(",")","{","}"," > [","]","\'","\"","<",">",",",".","?",";","\n","\r" > @map[char].call(value) > when "digit" > @map[char].call(value) > when "char" > @map[char].call(value) > else > puts "don't know what to do with #{char} and #{value}" > end > end > > can the first when be written in a simpler way ? Enjoy: @map = Hash.new { |map, char| proc { |value| puts "don't know what to do with #{char} and #{value}" } }.merge(your_original_map) @map[char][value] Regards Stefan -- Posted via http://www.ruby-forum.com/.