I have a custom class Region
, which I instantiated as following : node = Region.new
I wonder what is the differences, and why the following conditions act like that :
puts node.class === Region # => false
puts node.class == Region # => true
puts node.is_a?(Region) # => true
puts node.class.to_s === 'Region' # => true
What is the best way to test (ideally, in a case
condition) what is the class of my node (as it could be classes like Country
or Site
as well...)
Thank you for your time :)
CodePudding user response:
In a case
block you can do:
case node
when Region
# node is an instance of Region
else
# node is not an instance of Region
end
Which would internally check the condition in this order: Region === node
. For details about the ===
method read this answer.