[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

case statement

Shai Rosenfeld

8/8/2007 1:41:00 PM

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
--
Posted via http://www.ruby-....

21 Answers

Alex Young

8/8/2007 1:46:00 PM

0

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

--
Alex

Shai Rosenfeld

8/8/2007 2:05:00 PM

0

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?)
--
Posted via http://www.ruby-....

Kaldrenon

8/8/2007 2:12:00 PM

0

On Aug 8, 10:05 am, Shai Rosenfeld <shaigui...@gmail.com> wrote:
> 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?)
> --
> Posted viahttp://www.ruby-....

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

Jano Svitok

8/8/2007 2:20:00 PM

0

On 8/8/07, Shai Rosenfeld <shaiguitar@gmail.com> wrote:
> 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 :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.

Alex Young

8/8/2007 2:23:00 PM

0

Shai Rosenfeld wrote:
> 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
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"

--
Alex

Brad Phelan

8/8/2007 2:24:00 PM

0

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

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

--
Brad Phelan
http://xt...

Shai Rosenfeld

8/8/2007 2:25:00 PM

0

Kaldrenon wrote:
> On Aug 8, 10:05 am, Shai Rosenfeld <shaigui...@gmail.com> wrote:
>> Array#include is EXACTLY what i need, but syntaxtetically (if u get the
>> --
>> Posted viahttp://www.ruby-....
>
> 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....

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.


--
Posted via http://www.ruby-....

David A. Black

8/8/2007 2:28:00 PM

0

Brad Phelan

8/8/2007 2:29:00 PM

0

Brad Phelan wrote:
> Shai Rosenfeld wrote:
>> 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?)
>
> 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
>
> --
> Brad Phelan
> http://xt...

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.

--
Brad Phelan
http://xt...

David A. Black

8/8/2007 2:50:00 PM

0