Professional Web Applications Themes

Can't case on class? - Ruby

The code below produces the following output: Class is ValidForm::OptionSet. Is it OK? true What about this: false WTF? 1 class ValidForm::Option 2 def field_tohtml(indentLevel=0) 3 puts "Class is #{optionSet.class}." 4 puts "Is it OK? #{optionSet.class==ValidForm::OptionSet}" 5 puts "What about this: #{optionSet==ValidForm::OptionSet}" 6 case optionSet.class 7 when ValidForm::OptionSet 8 #.... 9 10 when ValidForm::CheckboxSet 11 #.... 12 13 when ValidForm::RadioSet 14 #.... 15 16 else 17 puts 'WTF?' 18 end 19 end 20 end Thanks to memmove on #ruby-lang I found that if I change line #6 to: case optionSet #no .class here then it works, but I don't understand ...

  1. #1

    Default Can't case on class?

    The code below produces the following output:

    Class is ValidForm::OptionSet.
    Is it OK? true
    What about this: false
    WTF?


    1 class ValidForm::Option
    2 def field_tohtml(indentLevel=0)
    3 puts "Class is #{optionSet.class}."
    4 puts "Is it OK? #{optionSet.class==ValidForm::OptionSet}"
    5 puts "What about this: #{optionSet==ValidForm::OptionSet}"
    6 case optionSet.class
    7 when ValidForm::OptionSet
    8 #....
    9
    10 when ValidForm::CheckboxSet
    11 #....
    12
    13 when ValidForm::RadioSet
    14 #....
    15
    16 else
    17 puts 'WTF?'
    18 end
    19 end
    20 end


    Thanks to memmove on #ruby-lang I found that if I change line #6 to:

    case optionSet #no .class here

    then it works, but I don't understand why.

    Can someone explain why the former doesn't work?
    If this isn't an official bug, I call it a design flaw :)

    --
    (-, /\ \/ / /\/
    Gavin Guest

  2. #2

    Default Re: Can't case on class?

    Gavin Kistner wrote:
     

    $ ri Module#===
    ------------------------------------------------------------- Module#===
    mod === obj => true or false
    ------------------------------------------------------------------------
    Case Equality---Returns true if anObject is an instance of mod or
    one of mod's descendents. Of limited use for modules, but can be
    used in case statements to classify objects by class.


    /kent
    Kent Guest

  3. #3

    Default Re: Can't case on class?

    Kent S. wrote: [/ref]
     

    Bah, I forgot that case used === and not ==. Thanks, Kent.
    So the questions I have now are:

    a) Why does someClass===someClass return false?

    b) Why does the case fooInstance; when FooClass; work?

    I ask the second because:

    irb(main):001:0> foo = %w{ a b c d }
    => ["a", "b", "c", "d"]
    irb(main):002:0> foo.class
    => Array
    irb(main):003:0> foo.class==Array
    => true
    irb(main):004:0> foo.class===Array
    => false
    irb(main):005:0> foo==Array
    => false
    irb(main):006:0> foo===Array
    => false


    Line 4 confirms what Kent pointed out...but line 6 seems to indicate
    that the case statement switching off of the instance itself (which does
    work) should not work.


    --
    (-, /\ \/ / /\/
    Gavin Guest

  4. #4

    Default Re: Can't case on class?

    Gavin Kistner wrote: 

    Nevermind, I was confusing the appearance of === as a commutative
    operator with the fact that it's actually a method call.

    (foo===Array) != (Array===foo)

    and apparently the latter is the order that is used in a case statement.

    I can see the reason that Module#=== was thus defined (and the power
    that the definition enables)...but the principle of most surprise
    strikes again :)

    --
    (-, /\ \/ / /\/
    Gavin Guest

  5. #5

    Default Re: Can't case on class?


    "Gavin Kistner" <com> schrieb im Newsbeitrag
    news:xkbZb.346875$.. [/ref]
    does 
    >
    > Nevermind, I was confusing the appearance of === as a commutative
    > operator with the fact that it's actually a method call.[/ref]

    Btw: == is a method call, too. You can't define operators independent of
    a class in Ruby. All operators that you can define, have to sit in a
    class. It's just the syntax ("a + b") that mimics independence and
    commutability.
     

    Well, that's just a normal event during learning: you're surprised by
    something you didn't know beforehand, investigated and learned something.
    Nothing that would indicate that POLS is violated.
    :-)

    Kind regards

    robert

    Robert Guest

Similar Threads

  1. #25205 [Com]: Class names case insensitiv
    By reiersol at online dot no in forum PHP Development
    Replies: 0
    Last Post: October 29th, 01:00 PM
  2. #25205 [Opn->Bgs]: Class names case insensitiv
    By sniper@php.net in forum PHP Development
    Replies: 0
    Last Post: August 22nd, 08:35 AM
  3. #25205 [NEW]: Class names case insensitiv
    By thomas dot hebinck at digionline dot de in forum PHP Development
    Replies: 0
    Last Post: August 22nd, 08:24 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