# Barenames and case statements

**URL:** <https://www.ruby-forum.com/t/barenames-and-case-statements/164941>\
**Category:** Ruby\
**Created:** [April 7, 2009, 5:16pm UTC](https://www.ruby-forum.com/t/barenames-and-case-statements/164941 "2009-04-07T17:16:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/97f17d/32.png) [@ericw](https://www.ruby-forum.com/u/ericw)\
**Post date:** [April 7, 2009, 5:16pm UTC](https://www.ruby-forum.com/t/barenames-and-case-statements/164941/1 "2009-04-07T17:16:46Z")

</div>

I’ve encountered this in the past and thought I was messing something  
up, but it’s happening still.

Can anyone tell me why this:

puts “ruby-#{RUBY\_VERSION} [#{RUBY\_PLATFORM}]”

[String, Array, Hash].each do |klass|  
case klass  
when String  
puts “String!”  
when Array  
puts “Array!”  
when Hash  
puts “Hash!”  
else  
puts “I don’t know: #{klass}”  
end  
end

Produces this:

ruby-1.8.7 [x86\_64-linux]  
I don’t know: String  
I don’t know: Array  
I don’t know: Hash

ruby-1.9.1 [x86\_64-linux]  
I don’t know: String  
I don’t know: Array  
I don’t know: Hash

Am I missing something here? In order to get the expected result I  
have to do this:

[String, Array, Hash].each do |klass|  
case klass.to\_s  
when “String”  
puts “String!”  
when “Array”  
puts “Array!”  
when “Hash”  
puts “Hash!”  
else  
puts “I don’t know: #{klass}”  
end  
end

This works of course, but why doesn’t the former? I find it hard to  
believe this has been overlooked. What am I missing?

– Eric W.

---

<div class="post-metadata">

**Author:** ![ericw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/97f17d/32.png) [@ericw](https://www.ruby-forum.com/u/ericw)\
**Post date:** [April 7, 2009, 5:22pm UTC](https://www.ruby-forum.com/t/barenames-and-case-statements/164941/2 "2009-04-07T17:22:37Z")

</div>

Because the === operator of the Class class (used in case statements)  
checks for ‘is an instance of’.

So, the following:

> case klass  
> when String  
> puts “String!”  
> end

is to be interpreted as String===klass, that is to,  
klass.is\_a?(String), which is false. But the following is true:

case klass  
when Class  
puts ‘It’s a class of course’  
end

Hope it helps  
blambeau

---

<div class="post-metadata">

**Author:** ![ericw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/97f17d/32.png) [@ericw](https://www.ruby-forum.com/u/ericw)\
**Post date:** [April 7, 2009, 6:05pm UTC](https://www.ruby-forum.com/t/barenames-and-case-statements/164941/3 "2009-04-07T18:05:24Z")

</div>

On 07.04.2009 17:21, LAMBEAU Bernard wrote:

> is to be interpreted as String===klass, that is to,  
> klass.is\_a?(String), which is false. But the following is true:
> 
> case klass  
> when Class  
> puts ‘It’s a class of course’  
> end

A solution would be to use the other form of “case”:

[String, Array, Hash].each do |klass|  
case  
when klass == String  
puts “String!”  
when klass == Array  
puts “Array!”  
when klass == Hash  
puts “Hash!”  
else  
puts “I don’t know: #{klass}”  
end  
end

Or encapsulate tests in a special instance:

IsString = Object.new  
def IsString.===©  
String == c  
end

klass = String

case klass  
when IsString  
puts “Ah!”  
end

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![ericw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/97f17d/32.png) [@ericw](https://www.ruby-forum.com/u/ericw)\
**Post date:** [April 7, 2009, 6:27pm UTC](https://www.ruby-forum.com/t/barenames-and-case-statements/164941/4 "2009-04-07T18:27:06Z")

</div>

On Tue, Apr 7, 2009 at 11:21 AM, LAMBEAU Bernard [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Because the === operator of the Class class (used in case statements)  
> checks for ‘is an instance of’.

I understand. Definitely not what I’d expect. So my .to\_s is the only  
way to reasonably accomplish what I need to do? Overriding these  
things would probably not be a good idea.

> Hope it helps  
> blambeau

Thanks,

– Eric W.

---

<div class="post-metadata">

**Author:** ![ericw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/97f17d/32.png) [@ericw](https://www.ruby-forum.com/u/ericw)\
**Post date:** [April 7, 2009, 6:53pm UTC](https://www.ruby-forum.com/t/barenames-and-case-statements/164941/5 "2009-04-07T18:53:30Z")

</div>

On Wed, Apr 8, 2009 at 12:43 AM, Eric W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I think the .to\_s works just as well, but thanks.

try also,  
case klass.new  
when String  
…

---

<div class="post-metadata">

**Author:** ![ericw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/97f17d/32.png) [@ericw](https://www.ruby-forum.com/u/ericw)\
**Post date:** [April 7, 2009, 6:45pm UTC](https://www.ruby-forum.com/t/barenames-and-case-statements/164941/6 "2009-04-07T18:45:51Z")

</div>

On Tue, Apr 7, 2009 at 12:04 PM, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> A solution would be to use the other form of “case”:  
> Or encapsulate tests in a special instance:

I think the .to\_s works just as well, but thanks.

> Kind regards
> 
> ```
> robert
> 
> ```

– Eric W.

---

<div class="post-metadata">

**Author:** ![ericw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/97f17d/32.png) [@ericw](https://www.ruby-forum.com/u/ericw)\
**Post date:** [April 7, 2009, 8:06pm UTC](https://www.ruby-forum.com/t/barenames-and-case-statements/164941/7 "2009-04-07T20:06:49Z")

</div>

class Object  
def singleton\_class  
class \<\< self  
self  
end  
end  
end

[String, Array, Hash].each do |klass|  
case klass  
when String.singleton\_class  
puts “String!”  
when Array.singleton\_class  
puts “Array!”  
when Hash.singleton\_class  
puts “Hash!”  
else  
puts “I don’t know: #{klass}”  
end  
end

Hrm it would be nice to have some kind of voting system for feature  
requests  
[http://redmine.ruby-lang.org/issues/show/1082](http://redmine.ruby-lang.org/issues/show/1082)
