1 2 3 4 5
x = 'My String' x.kind_of? String # => true x.class == String # => true
Refactorings
No refactoring yet !
PabloBM
September 10, 2008, September 10, 2008 14:13, permalink
Nyet, they are not identical. As you can read on the documentation at http://www.ruby-doc.org/core/classes/Object.html#M000370 , kind_of?() "[r]eturns true if class is the class of obj, or if class is one of the superclasses of obj or modules included in obj."
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
module M; end class A; end class B < A include M end b = B.new b.kind_of? A # => true b.kind_of? B # => true b.kind_of? M # => true b.class == A # => true b.class == B # => false b.class == M # => false
Carl Porth
September 10, 2008, September 10, 2008 16:59, permalink
I tend to see the use of ===
1 2 3 4 5 6 7 8 9 10 11 12 13
module M; end class A; end class B < A include M end b = B.new A === b # => true B === b # => true M === b # => true
Adam
October 1, 2008, October 01, 2008 05:27, permalink
If you are using Rails (or ActiveSupport on it's own), you might also want to consider the use of acts_like? Example:
1 2 3 4 5 6
>> "My String".acts_like?(:string) => true >> "My String".mb_chars.acts_like?(:string) => true >> "My String".mb_chars.is_a?(String) => false
phylae.myopenid.com
October 1, 2008, October 01, 2008 06:16, permalink
This doesn't work for me. I think acts_like? is only for date/time objects.
1 2 3 4 5 6 7 8
$:~/sc/ror/my_app$ ruby -v ruby 1.8.6 (2007-09-24 patchlevel 111) [i486-linux] $:~/sc/ror/my_app$ rails -v Rails 2.1.1 $:~/sc/ror/my_app$ script/console Loading development environment (Rails 2.1.0) >> "My String".acts_like?(:string) => false
It seems like these two are alway identical. Is that the case? If so, I am wondering if there is a preferred convention or if it is all up to personal preference.