case statement

Discussion in 'Ruby' started by Shai Rosenfeld, Aug 8, 2007.

  1. 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-forum.com/.
     
    Shai Rosenfeld, Aug 8, 2007
    #1
    1. Advertising

  2. Shai Rosenfeld

    Alex Young Guest

    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
     
    Alex Young, Aug 8, 2007
    #2
    1. Advertising

  3. 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-forum.com/.
     
    Shai Rosenfeld, Aug 8, 2007
    #3
  4. Shai Rosenfeld

    Kaldrenon Guest

    On Aug 8, 10:05 am, 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?)
    > --
    > 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
     
    Kaldrenon, Aug 8, 2007
    #4
  5. Shai Rosenfeld

    Jano Svitok Guest

    On 8/8/07, 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?)


    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.
     
    Jano Svitok, Aug 8, 2007
    #5
  6. Shai Rosenfeld

    Alex Young Guest

    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
     
    Alex Young, Aug 8, 2007
    #6
  7. Shai Rosenfeld

    Brad Phelan Guest

    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://xtargets.com
     
    Brad Phelan, Aug 8, 2007
    #7
  8. Kaldrenon wrote:
    > On Aug 8, 10:05 am, Shai Rosenfeld <> 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.


    --
    Posted via http://www.ruby-forum.com/.
     
    Shai Rosenfeld, Aug 8, 2007
    #8
  9. Shai Rosenfeld

    Guest

    Hi --

    On Wed, 8 Aug 2007, Alex Young 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

    > 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)
     
    , Aug 8, 2007
    #9
  10. Shai Rosenfeld

    Brad Phelan Guest

    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://xtargets.com


    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://xtargets.com
     
    Brad Phelan, Aug 8, 2007
    #10
  11. Shai Rosenfeld

    Guest

    Hi --

    On Wed, 8 Aug 2007, Jano Svitok wrote:

    > On 8/8/07, 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?)

    >
    > 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)
     
    , Aug 8, 2007
    #11
  12. > 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
    --
    http://rimantas.com/
     
    Rimantas Liubertas, Aug 8, 2007
    #12
  13. Alex Young wrote:
    > Shai Rosenfeld 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) }

    --
    Posted via http://www.ruby-forum.com/.
     
    Shai Rosenfeld, Aug 8, 2007
    #13
  14. Shai Rosenfeld

    Brad Phelan Guest

    wrote:
    > Hi --
    >
    > On Wed, 8 Aug 2007, Jano Svitok wrote:
    >
    >> On 8/8/07, 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?)

    >>
    >> 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
     
    Brad Phelan, Aug 8, 2007
    #14
  15. Shai Rosenfeld 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
    --
    NP: Explosions in the Sky - First Breath After Coma
    Jabber:
    ICQ: 205544826
     
    Sebastian Hungerecker, Aug 8, 2007
    #15
  16. Shai Rosenfeld

    Guest

    , Aug 8, 2007
    #16
  17. Shai Rosenfeld, Aug 9, 2007
    #17
  18. 2007/8/8, 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?


    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
     
    Robert Klemme, Aug 9, 2007
    #18
  19. Shai Rosenfeld

    Alex Young Guest

    Robert Klemme wrote:
    > 2007/8/8, 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?

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

    --
    Alex
     
    Alex Young, Aug 9, 2007
    #19
  20. Shai Rosenfeld

    Guest

    Hi --

    On Thu, 9 Aug 2007, Alex Young wrote:

    > Robert Klemme wrote:
    >> 2007/8/8, 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?

    >>
    >> 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)
     
    , Aug 9, 2007
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Franks
    Replies:
    2
    Views:
    1,257
    Steve Franks
    Jun 10, 2004
  2. Tee
    Replies:
    3
    Views:
    7,819
    Herfried K. Wagner [MVP]
    Jun 23, 2004
  3. Janice

    lower case to upper case

    Janice, Dec 10, 2004, in forum: C Programming
    Replies:
    17
    Views:
    1,182
    Richard Bos
    Dec 14, 2004
  4. Replies:
    1
    Views:
    2,485
    Mark P
    Apr 6, 2007
  5. darrel

    'case' vs. 'case is'

    darrel, Aug 14, 2008, in forum: ASP .Net
    Replies:
    3
    Views:
    275
    Rory Becker
    Aug 14, 2008
Loading...

Share This Page