# How can I switch over an array?

**URL:** <https://www.ruby-forum.com/t/how-can-i-switch-over-an-array/158790>\
**Category:** Ruby\
**Created:** [January 28, 2009, 12:33am UTC](https://www.ruby-forum.com/t/how-can-i-switch-over-an-array/158790 "2009-01-28T00:33:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tsunami](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tsunami](https://www.ruby-forum.com/u/tsunami)\
**Post date:** [January 28, 2009, 12:33am UTC](https://www.ruby-forum.com/t/how-can-i-switch-over-an-array/158790/1 "2009-01-28T00:33:42Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![tsunami](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tsunami](https://www.ruby-forum.com/u/tsunami)\
**Post date:** [January 28, 2009, 1:51am UTC](https://www.ruby-forum.com/t/how-can-i-switch-over-an-array/158790/2 "2009-01-28T01:51:01Z")

</div>

Tsunami S. [removed\_email\_address@domain.invalid](mailto: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.

---

<div class="post-metadata">

**Author:** ![tsunami](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tsunami](https://www.ruby-forum.com/u/tsunami)\
**Post date:** [January 28, 2009, 7:55am UTC](https://www.ruby-forum.com/t/how-can-i-switch-over-an-array/158790/3 "2009-01-28T07:55:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tsunami](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tsunami](https://www.ruby-forum.com/u/tsunami)\
**Post date:** [January 28, 2009, 2:48am UTC](https://www.ruby-forum.com/t/how-can-i-switch-over-an-array/158790/4 "2009-01-28T02:48:57Z")

</div>

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](http://www.rubypal.com)  
Coming in 2009: The Well-Grounded Rubyist ([The Well-Grounded Rubyist](http://manning.com/black2))

[http://www.wishsight.com](http://www.wishsight.com) =\> Independent, social wishlist management!

---

<div class="post-metadata">

**Author:** ![tsunami](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tsunami](https://www.ruby-forum.com/u/tsunami)\
**Post date:** [January 28, 2009, 11:15am UTC](https://www.ruby-forum.com/t/how-can-i-switch-over-an-array/158790/5 "2009-01-28T11:15:42Z")

</div>

The one with

c = @map[char]

didn’t even cross my mind . Thanks !

---

<div class="post-metadata">

**Author:** ![tsunami](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tsunami](https://www.ruby-forum.com/u/tsunami)\
**Post date:** [January 28, 2009, 11:56am UTC](https://www.ruby-forum.com/t/how-can-i-switch-over-an-array/158790/6 "2009-01-28T11:56:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tsunami](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tsunami](https://www.ruby-forum.com/u/tsunami)\
**Post date:** [January 28, 2009, 5:16pm UTC](https://www.ruby-forum.com/t/how-can-i-switch-over-an-array/158790/7 "2009-01-28T17:16:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tsunami](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tsunami](https://www.ruby-forum.com/u/tsunami)\
**Post date:** [January 28, 2009, 7:40pm UTC](https://www.ruby-forum.com/t/how-can-i-switch-over-an-array/158790/8 "2009-01-28T19:40:50Z")

</div>

On Jan 27, 5:31 pm, Tsunami S. [removed\_email\_address@domain.invalid](mailto: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
