Discussion:
Using object's class in case statement?
Leam Hall
2016-07-26 12:36:47 UTC
Permalink
I'm trying to use an object's class in a case statement. The tests work
in an "if" but I haven't figured out the case yet.

Suggestions?

Leam

######

my_string = "Hello there!"
puts "Found a String in if!" if my_string.class == String # This works

# This just gives "Not sure"
case my_string.class
when String
#when 'String' # This also fails
#when "String" # This also fails
puts "Found a string in case!"
else
puts "Not sure."
end

######

Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Carlo E. Prelz
2016-07-26 12:41:56 UTC
Permalink
Subject: Using object's class in case statement?
Date: Tue 26 Jul 16 08:36:47AM -0400
I'm trying to use an object's class in a case statement. The tests work in
an "if" but I haven't figured out the case yet.
Suggestions?
This bit me long ago. I cannot remember the whys and wherefores, but
the case statement uses a special matching mechanism, so that, in the
case of objects, it matches the object's class just by passing the
object, not the class, in the case statement.

What I mean is that, instead of writing
case my_string.class
you write

case my_string

and it works.

Carlo
--
* Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - ***@fluido.as che bisogno ci sarebbe
* di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Дмитрий Нон
2016-07-26 12:42:09 UTC
Permalink
Hi, Leam!
You should learn more about case statement.

Case doesn't use == operator. It uses ===.
In "Ruby Programming Language" === operator is called "case operator".

Check this:

```
Integer == Integer
==> true
Integer === Integer
==> false
Integer === 1
==> true

```

Class' case operator checks if argument is this class.
So:
`(Integer === 1) == 1.is_a?(Integer)`


--------------------------------------
Dmitriy Non
I'm trying to use an object's class in a case statement. The tests work in an "if" but I haven't figured out the case yet.
Suggestions?
Leam
######
my_string = "Hello there!"
puts "Found a String in if!" if my_string.class == String # This works
# This just gives "Not sure"
case my_string.class
when String
#when 'String' # This also fails
#when "String" # This also fails
puts "Found a string in case!"
else
puts "Not sure."
end
######
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mai
Stefano Crocco
2016-07-26 12:51:26 UTC
Permalink
Post by Leam Hall
I'm trying to use an object's class in a case statement. The tests work
in an "if" but I haven't figured out the case yet.
Suggestions?
Leam
######
my_string = "Hello there!"
puts "Found a String in if!" if my_string.class == String # This works
# This just gives "Not sure"
case my_string.class
when String
#when 'String' # This also fails
#when "String" # This also fails
puts "Found a string in case!"
else
puts "Not sure."
end
######
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
The problem is that a case expression doesn't use operator == but operator ===
(more precisely, it calls the === operator on the value of the when expression
(in your case String) and given as argument the value of the case expression
(in your case my_string.class)). This can be defined by each class and, in
case of class Module, and the derived class Class:

"Returns true if obj is an instance of mod or one of mod's descendants. Of
limited use for modules, but can be used in case statements to classify
objects by class." (from the RI documentation for Module#===).

What does this mean in your case? It means that your code calls
String === String. But this is false, because String is not an instance of
String.

What you need is to remove the .class from the case expression:

case my_string
when String
#...

In this case String === my_string returns true because my_string is an
instance of the String class.

I hope this helps

Stefano


Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Leam Hall
2016-07-26 13:07:44 UTC
Permalink
Post by Leam Hall
case my_string
when String
#...
In this case String === my_string returns true because my_string is an
instance of the String class.
I hope this helps
Stefano
Stefano, that worked, thanks! I had not thought of using fewer words.

Leam

Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Nguyen Phu Hai
2016-07-26 13:48:24 UTC
Permalink
unsubscribe
Robert Klemme
2016-07-28 08:35:53 UTC
Permalink
Post by Stefano Crocco
The problem is that a case expression doesn't use operator == but operator ===
Just, that it's not a problem but a good feature. ;-)

Cheers

robert
--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Дмитрий Нон
2016-07-26 13:17:11 UTC
Permalink
Actually, I hate this statement in Ruby.
But I think this hatred is because I went to Ruby from other languages.

When you learn new language you read about operators.
So problem is that people don't, cuz "oh, c'mon I know this operator, SKIP SKIP SKIP".

If you know how it works it makes code more elegant:

```
case x
when Integer then 'x is an integer!'
when Float then 'x is a float number:('
when String then 'x is a string. I hate strings!'
else fail 'WTF is x?!'
end
```

Say it: "when integer". Isn't that nice?

But I hate this operator anyways... :)

--------------------------------------
Dmitriy Non
Post by Leam Hall
case my_string.class
when String
#when 'String' # This also fails
#when "String" # This also fails
As others have said, case statements use ===. This is a weird
beastie, which I think should have been called "describes?" or perhaps
overloaded "includes?", since it is NOT commutative as one would
expect with something that looks like equality. It basically means,
does the thing on the left "describe" (as in classes) or "include" (if
used with a range) the thing on the right. See the slide titled "===
!= ==!" (currently #10) in my presentation deck at
http://bit.ly/RubyGotchas/.
--
Dave Aronson, consulting software developer of Codosaur.us,
PullRequestRoulette.com, Blog.Codosaur.us, and Dare2XL.com.
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lan
Eric Christopherson
2016-07-26 14:19:30 UTC
Permalink
On Tue, Jul 26, 2016 at 7:58 AM, Dave Aronson <
See the slide titled "===
!= ==!" (currently #10) in my presentation deck at
http://bit.ly/RubyGotchas/.
To me, that slide's name raises the question: what is ==! ? I assume it's
an operator in some language, but I don't know which one.
--
Eric Christopherson
Matthew Kerwin
2016-07-26 21:42:44 UTC
Permalink
Post by Eric Christopherson
On Tue, Jul 26, 2016 at 7:58 AM, Dave Aronson <
See the slide titled "===
!= ==!" (currently #10) in my presentation deck at
http://bit.ly/RubyGotchas/.
To me, that slide's name raises the question: what is ==! ? I assume it's
an operator in some language, but I don't know which one.
Triple-equals does not equal double-equals <emphatic>

You could define a ==! method on an object, but you'd probably need to
use a . to call it. I believe the parser reads 'a ==! b' as 'a == !b'

Cheers
--
Matthew Kerwin
http://matthew.kerwin.net.au/

Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Eric Christopherson
2016-07-27 02:39:02 UTC
Permalink
Post by Eric Christopherson
On Tue, Jul 26, 2016 at 7:58 AM, Dave Aronson <
See the slide titled "===
!= ==!" (currently #10) in my presentation deck at
http://bit.ly/RubyGotchas/.
To me, that slide's name raises the question: what is ==! ? I assume it's
an operator in some language, but I don't know which one.
Parse it as '"===" != "=="!'.
Facepalm. :)
--
Eric Christopherson

Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Aaron Lewing
2016-09-16 17:46:12 UTC
Permalink
--
Aaron Lewing
301-524-0920
Meinrad Recheis
2018-01-23 08:52:05 UTC
Permalink
unsubscribe
陈后细
2018-02-05 13:54:41 UTC
Permalink
unsubscribe
Joan Currie
2018-06-13 13:58:58 UTC
Permalink
Joan Currie
2018-06-13 16:23:38 UTC
Permalink
Nick Duncan
2018-10-09 15:31:43 UTC
Permalink
Unsubscribe
Post by Leam Hall
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
--
Nick Duncan UNC Chapel Hill | Class of 2015 Sociology B.A. History and
Biology Minor 7045024810 ***@gmail.com
Ahnee Chu
2018-06-14 19:24:13 UTC
Permalink
unsubscribe
andrew mcelroy
2018-06-16 02:38:37 UTC
Permalink
unsubscribe
Loading...