From: "Brian Schröder" Date: 2005-10-26T01:09:20+09:00 Subject: Re: Comparing Classes with Case ? On 25/10/05, Christophe Grandsire wrote: > Selon Warren Seltzer : > > > The following failure surprised me: > > > > Version 1: > > > > irb(main):068:0> "hi".class == "hi".class > > => true > > irb(main):069:0> "hi".class === "hi".class > > => false > > > > > > Version 2: > > > > irb(main):055:0> case "whatever".class > > irb(main):056:1> when String > > irb(main):057:1> p "it is a string" > > irb(main):058:1> else > > irb(main):059:1* p "it is not a string" > > irb(main):060:1> end > > "it is not a string" > > > > What it amounts to is you can't use case/when to test for class. This is a > > HUGE LOSE. > > The pickaxe book .chm says YOU CAN do it. I'm using ruby 1.8.2 > > > > Lose the ".class". === automatically looks for the class of the left argument. > In your case example, just write: > case "whatever" > when String > p "it is a string" > else > p "it is not a string" > end > > It's work like a charm. Basically "a === SomeClass" is the same as "a.class == > SomeClass". > -- > Christophe Grandsire. > > http://rainbow.conlang.free.fr > > It takes a straight mind to create a twisted conlang. > Just throwing in that it would not be a "BIG LOSS", not even a _big loss_ or a big loss, because you nearly never should test for the class of an object. bschroed@black:~/svn/projekte/ruby-things$ cat undef.rb class UppercaseString def initialize(string) @string = string.upcase end def gsub(*args, &block) self.class.new(@string.gsub(*args, &block)) end def to_s @string.dup end end a = "quack like a duck" b = UppercaseString.new("walk like a duck") puts a.gsub(/duck/i, 'cow') puts b.gsub(/duck/i, 'cow') class <