[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

how can I switch over an array ?

Tsunami Script

1/27/2009 11:31:00 PM

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 ?
--
Posted via http://www.ruby-....

7 Answers

matt

1/28/2009 12:47:00 AM

0

Tsunami Script <lyon2003@lycos.com> 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 ?

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.

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Leopard - http://www.takecontrolbooks.com/leopard-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...

David A. Black

1/28/2009 1:46:00 AM

0

Hi --

On Wed, 28 Jan 2009, matt neuburg wrote:

> Tsunami Script <lyon2003@lycos.com> 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 ?
>
> 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.r...
Coming in 2009: The Well-Grounded Rubyist (http://manning....)

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

Robert Klemme

1/28/2009 6:51:00 AM

0

On 28.01.2009 00:31, Tsunami Script 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 ?

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

--
remember.guy do |as, often| as.you_can - without end

Tsunami Script

1/28/2009 10:13:00 AM

0

The one with

c = @map[char]

didn't even cross my mind . Thanks !
--
Posted via http://www.ruby-....

Brian Candler

1/28/2009 10:55:00 AM

0

Tsunami Script 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

--
Posted via http://www.ruby-....

Stefan Rusterholz

1/28/2009 4:14:00 PM

0

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

William James

1/28/2009 6:36:00 PM

0

On Jan 27, 5:31 pm, Tsunami Script <lyon2...@lycos.com> 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 ?

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