Case and classes


(Matthias Veit) #1

Hi,

today I wrote a method in my youthful levity and was wondering
about raised exceptions. The method was similar to:

def do_class_sensitive(classhandle)
case classhandle
when Array
#do somethiong…
when String
#do somethiong…
when Fixnum
#do somethiong…
else
raise "what to do with #{classhandle.name}??"
end
end

The aim was to write a class sensitive switch - but this does not
work. I could use the classname (the string) to make the switch work,
but this was not the intention. I could imagine, that inheritance is
the academic problem of the case-switch: falls a subclass of Array into
the Array-clause or into the else-clause? (if the specific subclass is
not listed in the switch).
So I want to ask: what is the expected behaviour of a class sensitive switch??

Best regards,
Matthias

···

Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


(Ned Konz) #2

case switches use ===, so:

$ ri Module.===

···

On Monday 17 June 2002 12:10 pm, Matthias Veit wrote:

So I want to ask: what is the expected behaviour of a class
sensitive switch??


Module#===
mod === anObject -> 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.


Ned Konz
http://bike-nomad.com
GPG key ID: BEEA7EFE