how can I switch over an array ?

T

Tsunami Script

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

matt neuburg

Tsunami Script said:
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 ?

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.
 
D

David A. Black

Hi --

Tsunami Script said:
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 ?

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.

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 (http://manning.com/black2)

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

Robert Klemme

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 ?

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
 
B

Brian Candler

Tsunami said:
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
 
S

Stefan Rusterholz

Tsunami said:
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
 
W

w_a_x_man

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 ?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top