How can I switch over an array?

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 ?

Tsunami S. [email protected] wrote:

   @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 ?

In days gone by, you might have written this sort of thing:

when *" #!@$%“.split(”") – extend string as desired

But I don’t know if that will work any more (Ruby 1.9)…? m.

On 28.01.2009 00:31, Tsunami S. wrote:

   @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 ?

PATTERN = [
" “,”#","!","@","$","%","^","&","*","(",")","{","}",",",
“digit”, “char”
].freeze

def call(char, value)
if PATTERN.include? char
@map[char].call(value)
else
puts “don’t know what to do with #{char} and #{value}”
end
end

Or, even more efficient

PATTERN2 = Regexp.new("\A#{Regexp.union(PATTERN)}\z")

def call(char, value)
if PATTERN2 =~ char
@map[char].call(value)
else
puts “don’t know what to do with #{char} and #{value}”
end
end

Or, even better (i.e. if @map is filled properly)

def call(char, value)
c = @map[char]
if c
c.call(value)
else
puts “don’t know what to do with #{char} and #{value}”
end
end

Cheers

robert

Hi –

On Wed, 28 Jan 2009, matt neuburg wrote:

   @map[char].call(value)

when *" #!@$%“.split(”") – extend string as desired

But I don’t know if that will work any more (Ruby 1.9)…? m.

Yes, it will.

David


David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (The Well-Grounded Rubyist)

http://www.wishsight.com => Independent, social wishlist management!

The one with

c = @map[char]

didn’t even cross my mind . Thanks !

Tsunami S. 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 ?

SPECIAL = /\A[#{Regexp.escape(" #!@$%^&*(){}[]’"<>,.?;\r\n")}]\z/
def call(char,value)
case char
when SPECIAL
… etc

Tsunami S.er 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

On Jan 27, 5:31 pm, Tsunami S. [email protected] wrote:

   @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 ?

def call char, value
if @map.include? char
@map[char].call(value)
else
puts “don’t know what to do with #{char} and #{value}”
end
end