Case statement

dblack@rubypal.com wrote:

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 :old_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 :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

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

···

On Wed, 8 Aug 2007, Jano Svitok wrote:

On 8/8/07, Shai Rosenfeld <shaiguitar@gmail.com> wrote:

:slight_smile:

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

thanks for all your help,

shai

···

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