Case statement

hi guys,

was wondering the syntax of how to do this:

case [1,2,3,4]

when 1: ‘this is what i want’
when 11: ‘not result’
when 15: ‘no good’

end

((i.e, whatever value is included in the array))
…how do i do this?
tia

Shai R. wrote:

end

((i.e, whatever value is included in the array))
…how do i do this?

Does the Array#include? method do what you need? Perhaps a more
fleshed-out example might help?

Alex Y. wrote:

Shai R. wrote:

end

((i.e, whatever value is included in the array))
…how do i do this?

Does the Array#include? method do what you need? Perhaps a more
fleshed-out example might help?

Array#include is EXACTLY what i need, but syntaxtetically (if u get the
drift) i’m not sure how to do it:

case [3, 45, 6, ‘abc’].inlcude?
when 1: ‘no good’
when 3: ‘good!’
when ‘lolo’: ‘no good’
end

(the above doesn’t work. it’s gives a ‘not enough arguments’ error. how
do i do it correctly?)

On 8/8/07, Shai R. [email protected] wrote:

Array#include is EXACTLY what i need, but syntaxtetically (if u get the
drift) i’m not sure how to do it:

case [3, 45, 6, ‘abc’].inlcude?
when 1: ‘no good’
when 3: ‘good!’
when ‘lolo’: ‘no good’
end

(the above doesn’t work. it’s gives a ‘not enough arguments’ error. how
do i do it correctly?)

You could possibly do something like the following, but it’s pretty
dangerous:

class Object
alias_method :old_case_equal, :===

def ===(other)
case other
when Array:
other.include? self
else
old_case_equal(other)
end
end
end

puts case [3, 45, 6, ‘abc’]
when 1: ‘no good’
when 3: ‘good!’
when ‘lolo’: ‘no good’
end

I.e. case calls === on each when condition, so if you define proper
=== that will not break other things, you’re done.

when you run

case a
when b: …
when c: …
end

ruby will evaulate

b === a, i.e. b.===(a)
c === a, i.e. c.===(a)

J.

On Aug 8, 10:05 am, Shai R. [email protected] wrote:

Array#include is EXACTLY what i need, but syntaxtetically (if u get the

Posted viahttp://www.ruby-forum.com/.

include? takes an argument x, and is called on a collection n, so that
if n.include?(x) it returns true.

http://www.whytheluckystiff.net/ruby/pickaxe/html/ref_c_array.html#Array.include_qm

Shai R. wrote:

drift) i’m not sure how to do it:

case [3, 45, 6, ‘abc’].inlcude?
when 1: ‘no good’
when 3: ‘good!’
when ‘lolo’: ‘no good’
end
I’m not sure why you’re using case here. I’d do it like this:

[3, 45, 6, ‘abc’].include?(var) ? “good” : “no good”

Kaldrenon wrote:

On Aug 8, 10:05 am, Shai R. [email protected] wrote:

Array#include is EXACTLY what i need, but syntaxtetically (if u get the

Posted viahttp://www.ruby-forum.com/.

include? takes an argument x, and is called on a collection n, so that
if n.include?(x) it returns true.

http://www.whytheluckystiff.net/ruby/pickaxe/html/ref_c_array.html#Array.include_qm

please read the post before posting yourself.
what i need is the syntax of a case statement doing the following:

case [a, b, c, d]
when x: ‘nothing happens’
when y: ‘nothing happens’
when z: ‘nothing happens’
when a: ‘render this option’
end

can anyone help me with a live code example of how to do this?
many thanks.

Hi –

On Wed, 8 Aug 2007, Alex Y. wrote:

Array#include is EXACTLY what i need, but syntaxtetically (if u get the
drift) i’m not sure how to do it:

case [3, 45, 6, ‘abc’].inlcude?
when 1: ‘no good’
when 3: ‘good!’
when ‘lolo’: ‘no good’
end
I’m not sure why you’re using case here. I’d do it like this:

[3, 45, 6, ‘abc’].include?(var) ? “good” : “no good”

I think the problem is that if it includes both 1 and 3, it’s no good.

You (Shai) could do something like:

module YesNo
def yes_no(a,b)
[*a].all? {|e| include?(a) } and not [*b].any? {|e| include?(e) }
end
end

a = [1,2,3,4,5]
a.extend(YesNo)

p a.yes_no(3,6) # true
p a.yes_no(3,5) # false

(Those two lines are my only tests at the moment so try it out some
more :slight_smile:

David

Hi –

On Wed, 8 Aug 2007, Jano S. wrote:

do i do it correctly?)
else
old_case_equal(other)
end
end
end

Let’s go back to the “pretty dangerous” thing :slight_smile: I think this is
beyond the acceptable danger threshold; you’re actually making it so
that Array#=== won’t work any more, which could really make things
blow up.

David

Alex Y. wrote:

Shai R. wrote:

drift) i’m not sure how to do it:

case [3, 45, 6, ‘abc’].inlcude?
when 1: ‘no good’
when 3: ‘good!’
when ‘lolo’: ‘no good’
end
I’m not sure why you’re using case here. I’d do it like this:

[3, 45, 6, ‘abc’].include?(var) ? “good” : “no good”

…went for this in the end. i suppose the case statement w/ array
include opt didn’t really fit the needs, all in all. thanks all for your
answers though.

David, what is this all? method? is it a Array#method ? i didn’t find it
in the docs…?

 [*a].all? {|e| include?(a) } and not [*b].any? {|e| include?(e) }

Shai R. wrote:

what is this all? method? is it a Array#method ? i didn’t find it
in the docs…?

It’s Enumerable#all?. It returns true if the block evaluates to true for
all
elements (or in case there is no block, if all the elements are true
(i.e.
non-nil/false)

HTH,
Sebastian

end

((i.e, whatever value is included in the array))
…how do i do this?

Strange problem you have here… What if array includes all: 1,11,15?
What the result should be?

choices = {1 => ‘this is what i want’, 11 => ‘not result’, 15 => ‘no good’}
data = [1, 2, 3, 4]

data.inject([]){|result, k| result << choices[k]}.compact

=> [“this is what i want”]

Regards,
Rimantas

Hi –

On Wed, 8 Aug 2007, Shai R. wrote:

David, what is this all? method? is it a Array#method ? i didn’t find it
in the docs…?

[*a].all? {|e| include?(a) } and not [*b].any? {|e| include?(e) }

It’s a method of Enumerable.

David

2007/8/8, Shai R. [email protected]:

end

((i.e, whatever value is included in the array))
…how do i do this?

You can do it with the other form of “case”:

a = [1,2,3,4]
result = case
when a.include?(1): ‘this is what i want’
when a.include?(11): ‘not result’
when a.include?(15): ‘no good’
end

Kind regards

robert

:slight_smile:

neat method (all? | any?) ; wasn’t aware it existed

thanks for all your help,

shai

Robert K. wrote:

when a.include?(11): ‘not result’
when a.include?(15): ‘no good’
end

Or the other way around:

a = [1,2,3,4]
b = 2
result = case b
when *a: “yes!”
else “no!”
end

a = [1,2,3,4]
result = case
when a.include?(1): ‘this is what i want’
when a.include?(11): ‘not result’
when a.include?(15): ‘no good’
end

ahhhhhhhhhhhhhhhh
perfect.

thanks.

Hi –

On Thu, 9 Aug 2007, Shai R. wrote:

a = [1,2,3,4]
result = case
when a.include?(1): ‘this is what i want’
when a.include?(11): ‘not result’
when a.include?(15): ‘no good’
end

ahhhhhhhhhhhhhhhh
perfect.

I’m curious about the fall-through case. If the array does not
include 1, 11, or 15, then result will be nil, which I assume is
equivalent to a “no good” result. That makes me wonder whether a
simple include? would work equally well. Or is there more to the
various error conditions?

David

Hi –

On Thu, 9 Aug 2007, Alex Y. wrote:

when 15: ‘no good’
when a.include?(1): ‘this is what i want’
else “no!”
end

I had the impression that Shai wanted to check both for the inclusion
of one item, and the absence of others. I’m not sure, since the
original example was kind of pseudo-code based on what he wanted and
it wasn’t clear what should happen in a fall-through situation (which
I know case doesn’t do, but I think Shai was angling for that).

David