Professional Web Applications Themes

case with multiple expressions - Ruby

Is there anything wrong with this kind of solution? case [x,y,z] when [1,2,3] then #... when [1,2,4] then #... when [2,2,3] then #... else ... end Just asking... Hal...

  1. #1

    Default case with multiple expressions

    Is there anything wrong with this kind of solution?

    case [x,y,z]
    when [1,2,3] then #...
    when [1,2,4] then #...
    when [2,2,3] then #...
    else ...
    end


    Just asking...

    Hal



    Hal Guest

  2. #2

    Default Re: case with multiple expressions

    Hi,

    In message "case with multiple expressions"
    on 04/02/05, Hal Fulton <com> writes:

    |Is there anything wrong with this kind of solution?
    |
    | case [x,y,z]
    | when [1,2,3] then #...
    | when [1,2,4] then #...
    | when [2,2,3] then #...
    | else ...
    | end

    Hmm, if we define Array#=== as

    def ===(other)
    self.all?{|item| item === other}
    end

    You can do

    case [x, y]
    when [0..50, 150..200]
    color = white
    when [0..100, 0..50]
    color = blue
    when [50..100, 50..200]
    color = gray
    else
    color = black
    end

    Just an idea...

    matz.


    Yukihiro Guest

  3. #3

    Default Re: case with multiple expressions

    Hi,

    At Thu, 5 Feb 2004 09:31:27 +0900,
    Yukihiro Matsumoto wrote in [ruby-talk:91585]: 

    Shouldn't it be:

    index = -1
    other.each_with_index do |item, index|
    unless self.fetch(index) {return false} === item
    return false
    end
    end
    return self.size == index + 1

    --
    Nobu Nakada


    nobu.nokada@softhome.net Guest

  4. #4

    Default Re: case with multiple expressions

    il Thu, 5 Feb 2004 09:31:27 +0900, org (Yukihiro
    Matsumoto) ha scritto::
     

    I suppose that should be:

    def ===(otr)
    self.zip(otr).any? {|i1,i2| i1===i2}
    end

    gabriele Guest

Similar Threads

  1. Replies: 112
    Last Post: December 9th, 05:46 PM
  2. Bind Multiple Expressions
    By IowaDave in forum Coldfusion Flash Integration
    Replies: 0
    Last Post: March 11th, 01:42 PM
  3. CFIF inside CFLOOP with Multiple Expressions
    By MWAR Web Admin in forum Macromedia ColdFusion
    Replies: 4
    Last Post: June 9th, 12:36 AM
  4. Handling case of field names in multiple db's
    By Michael Flanagan in forum PHP Development
    Replies: 4
    Last Post: December 9th, 01:52 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139