Forum: Ruby how can I switch over an array ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Tsunami S. (Guest)
on 2009-01-28 01:33
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 ?
matt neuburg (Guest)
on 2009-01-28 02:51
(Received via mailing list)
Tsunami S. <removed_email_address@domain.invalid> 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.
David A. Black (Guest)
on 2009-01-28 03:48
(Received via mailing list)
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 (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!
Robert K. (Guest)
on 2009-01-28 08:55
(Received via mailing list)
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
Tsunami S. (Guest)
on 2009-01-28 12:15
The one with

c = @map[char]

didn't even cross my mind . Thanks !
Brian C. (Guest)
on 2009-01-28 12:56
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
Stefan R. (Guest)
on 2009-01-28 18:16
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
unknown (Guest)
on 2009-01-28 20:40
(Received via mailing list)
On Jan 27, 5:31 pm, Tsunami S. <removed_email_address@domain.invalid> 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
This topic is locked and can not be replied to.