case statement

S

Shai Rosenfeld

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
 
A

Alex Young

Shai said:
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?

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

Shai Rosenfeld

Alex said:
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?)
 
K

Kaldrenon

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?)

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
 
J

Jano Svitok

Alex said:
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?)

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

class Object
alias_method :eek:ld_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.
 
A

Alex Young

Shai said:
Alex said:
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
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"
 
B

Brad Phelan

Shai said:
Alex said:
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?)

Here's is a little trick that works

class Casey
def initialize(o)
@o = o
end
def method_missing(name)
CaseyMethod.new(@o, name)
end
end

class CaseyMethod
def initialize(o, m)
@o = o
@m = m
end
def ==(other)
@o.send(@m, other)
end
end

class Object
def casey
Casey.new(self)
end
end


case [2,3,4].casey.include?
when 1
puts "a"
when 2
puts "b"
else
puts "c"
end
 
S

Shai Rosenfeld

Kaldrenon said:
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.
 
D

dblack

Hi --

Shai said:
Alex said:
Shai Rosenfeld 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
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 :)


David

--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
 
B

Brad Phelan

Brad said:
Shai said:
Alex said:
Shai Rosenfeld 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?)

Here's is a little trick that works

class Casey
def initialize(o)
@o = o
end
def method_missing(name)
CaseyMethod.new(@o, name)
end
end

class CaseyMethod
def initialize(o, m)
@o = o
@m = m
end
def ==(other)
@o.send(@m, other)
end
end

class Object
def casey
Casey.new(self)
end
end


case [2,3,4].casey.include?
when 1
puts "a"
when 2
puts "b"
else
puts "c"
end

The problem with the above is that I don't think it does what you want.
It breaks out after it finds the first match and doesn't try to match
any further. If more than one *when* expression matches only the
first block is executed.
 
D

dblack

Hi --

Alex said:
Shai Rosenfeld 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?)

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

class Object
alias_method :eek:ld_case_equal, :===

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

Let's go back to the "pretty dangerous" thing :) 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

--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
 
R

Rimantas Liubertas

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?

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
 
S

Shai Rosenfeld

Alex said:
Shai said:
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) }
 
B

Brad Phelan

Hi --

Alex Young wrote:
Shai Rosenfeld 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?)

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

class Object
alias_method :eek:ld_case_equal, :===

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

Let's go back to the "pretty dangerous" thing :) 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

You don't need to do the 'dangerous' thing to create the same effect.
You just need to create a proxy object that when === is called on it it
delegates to the original method. See my post further down the list for
full details.

case [2,3,4].casey.include?
when 1
puts "a"
when 2
puts "b"
else
puts "c"
end

Brad
 
S

Sebastian Hungerecker

Shai said:
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
 
S

Shai Rosenfeld

:)

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

thanks for all your help,

shai
 
R

Robert Klemme

2007/8/8 said:
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?

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
 
A

Alex Young

Robert said:
2007/8/8 said:
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?

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

Or the other way around:

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

dblack

Hi --

Robert said:
2007/8/8 said:
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?

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

Or the other way around:

a = [1,2,3,4]
b = 2
result = case b
when *a: "yes!"
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

--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
 

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
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top