case with multiple expressions

H

Hal Fulton

Is there anything wrong with this kind of solution?

case [x,y,z]
when [1,2,3] then #...
when [1,2,4] then #...
when [2,2,3] then #...
else ...
end


Just asking...

Hal
 
Y

Yukihiro Matsumoto

Hi,

In message "case with multiple expressions"

|Is there anything wrong with this kind of solution?
|
| case [x,y,z]
| when [1,2,3] then #...
| when [1,2,4] then #...
| when [2,2,3] then #...
| else ...
| end

Hmm, if we define Array#=== as

def ===(other)
self.all?{|item| item === other}
end

You can do

case [x, y]
when [0..50, 150..200]
color = white
when [0..100, 0..50]
color = blue
when [50..100, 50..200]
color = gray
else
color = black
end

Just an idea...

matz.
 
N

nobu.nokada

Hi,

At Thu, 5 Feb 2004 09:31:27 +0900,
Yukihiro Matsumoto wrote in [ruby-talk:91585]:
|Is there anything wrong with this kind of solution?
|
| case [x,y,z]
| when [1,2,3] then #...
| when [1,2,4] then #...
| when [2,2,3] then #...
| else ...
| end

Hmm, if we define Array#=== as

def ===(other)
self.all?{|item| item === other}
end

Shouldn't it be:

index = -1
other.each_with_index do |item, index|
unless self.fetch(index) {return false} === item
return false
end
end
return self.size == index + 1
 
G

gabriele renzi

il Thu, 5 Feb 2004 09:31:27 +0900, (e-mail address removed) (Yukihiro
Matsumoto) ha scritto::
Hi,

In message "case with multiple expressions"

|Is there anything wrong with this kind of solution?
|
| case [x,y,z]
| when [1,2,3] then #...
| when [1,2,4] then #...
| when [2,2,3] then #...
| else ...
| end

Hmm, if we define Array#=== as

def ===(other)
self.all?{|item| item === other}
end

I suppose that should be:

def ===(otr)
self.zip(otr).any? {|i1,i2| i1===i2}
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

Forum statistics

Threads
474,142
Messages
2,570,819
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top