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

Just asking…

Hal

Hi,

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

In message “case with multiple expressions” on 04/02/05, Hal Fulton hal9000@hypermetrics.com writes:

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

···


Nobu Nakada

I suppose that should be:

def ===(otr)
self.zip(otr).any? {|i1,i2| i1===i2}
end

···

il Thu, 5 Feb 2004 09:31:27 +0900, matz@ruby-lang.org (Yukihiro Matsumoto) ha scritto::

Hi,

In message “case with multiple expressions” > on 04/02/05, Hal Fulton hal9000@hypermetrics.com writes:

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