From: David Alan Black Date: 2001-04-28T04:48:38+09:00 Subject: [ruby-talk:14348] Re: case and to_s Hello -- On Sat, 28 Apr 2001, Jim Freeze wrote: > Hi: > > I am trying to add a clase to a list depending upon the > type of the class. Since there are several types of > classes, I am using a case statement. But, the way I > am using the case statement causes the << to call > #to_s, which I don't want it to do. > Here is the error followed by the code. > Can someone please explain this behavior to me? > Tnanks > > ./dev.rb > ./dev.rb:21:in `===': failed to convert Class into String (TypeError) > from ./dev.rb:21:in `add_device' > from ./dev.rb:32 > from ./dev.rb:32:in `each' > from ./dev.rb:32 > I don't think it's a matter of #to_s getting called -- but read on. [...] > case d.type > when /DeviceA/ > @devlist << d > end This is in effect performing the test: /DeviceA/ === d.type but Regexp#=== (case equality method for Regexp) expects a String on the other end of the line, not a Class. You could force a regexp-to-string test: case d.type.to_s (etc.) Or you could do: case d when DeviceA (etc.) because then your test is: DeviceA === d and Module#===, to paraphrase the pickaxe (p. 345), will return true if d is an instance of DeviceA or one of DeviceA's descendants. (The descendants part may not be desireable, but you can always use the string-based test above instead.) David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav