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
######
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
Custom Domain by Bitly.
···
On Tue, Jul 26, 2016 at 8:36 AM, Leam Hall <leamhall@gmail.com> wrote:
case my_string.class
when String
#when 'String' # This also fails
#when "String" # This also fails
--
Dave Aronson, consulting software developer of Codosaur.us,
PullRequestRoulette.com, Blog.Codosaur.us, and Dare2XL.com.
Quoting Leam Hall (leamhall@gmail.com):
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
···
Subject: Using object's class in case statement?
Date: Tue 26 Jul 16 08:36:47AM -0400
--
* Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe
* di parlare tanto di amore e di rettitudine? (Chuang-Tzu)
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
···
On Tuesday 26 July 2016 08:36:47 Leam Hall wrote:
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-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
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
non.dmitriy@gmail.com
26 июля 2016 г., в 15:36, Leam Hall <leamhall@gmail.com> написал(а):
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-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Stefano, that worked, thanks! I had not thought of using fewer words.
Leam
···
On 07/26/16 08:51, Stefano Crocco wrote:
On Tuesday 26 July 2016 08:36:47 Leam Hall wrote:
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
BTW, everybody, the URL I posted (http://bit.ly/RubyGotchas plus a
slash and then a period to end the sentence) works if you take both
the slash and the period, or leave them both off. I put on the slash
because many people's email programs will interpret the period as part
of the URL. Apparently some of your use one of the smarter ones, and
were having problems because it took only the slash....
···
--
Dave Aronson, consulting software developer of Codosaur.us,
PullRequestRoulette.com, Blog.Codosaur.us, and Dare2XL.com.
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.
···
On Tue, Jul 26, 2016 at 7:58 AM, Dave Aronson < ruby-talk.list.2.TRex@codosaur.us> wrote:
See the slide titled "===
!= ==!" (currently #10) in my presentation deck at
Custom Domain by Bitly.
--
Eric Christopherson
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
non.dmitriy@gmail.com
26 июля 2016 г., в 15:58, Dave Aronson <ruby-talk.list.2.TRex@codosaur.us> написал(а):
On Tue, Jul 26, 2016 at 8:36 AM, Leam Hall <leamhall@gmail.com> wrote:
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
Custom Domain by Bitly.
--
Dave Aronson, consulting software developer of Codosaur.us,
PullRequestRoulette.com, Blog.Codosaur.us, and Dare2XL.com.
Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Just, that it's not a problem but a good feature. 
Cheers
robert
···
On Tue, Jul 26, 2016 at 2:51 PM, Stefano Crocco <stefano.crocco@alice.it> wrote:
The problem is that a case expression doesn't use operator == but operator ===
--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/
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
···
On 27/07/2016, Eric Christopherson <echristopherson@gmail.com> wrote:
On Tue, Jul 26, 2016 at 7:58 AM, Dave Aronson < > ruby-talk.list.2.TRex@codosaur.us> wrote:
See the slide titled "===
!= ==!" (currently #10) in my presentation deck at
Custom Domain by Bitly.
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.
--
Matthew Kerwin
http://matthew.kerwin.net.au/
Parse it as '"===" != "=="!'.
···
On 27/07/2016, Eric Christopherson <echristopherson@gmail.com> wrote:
On Tue, Jul 26, 2016 at 7:58 AM, Dave Aronson < > ruby-talk.list.2.TRex@codosaur.us> wrote:
See the slide titled "===
!= ==!" (currently #10) in my presentation deck at
Custom Domain by Bitly.
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.
--
Dave Aronson, consulting software developer of Codosaur.us,
PullRequestRoulette.com, Blog.Codosaur.us, and Dare2XL.com.