20bdcaa65891ace11f18e393c6e1f729

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.

1
2
3
4
5
x = 'My String'

x.kind_of? String # => true

x.class == String # => true

Refactorings

No refactoring yet !

4ebfc8d183472ac3cffff96be3e0ec68

PabloBM

September 10, 2008, September 10, 2008 14:13, permalink

1 rating. Login to rate!

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
Aedd89a10aba3a46576ea4f604146c65

Carl Porth

September 10, 2008, September 10, 2008 16:59, permalink

2 ratings. Login to rate!

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
A8d3f35baafdaea851914b17dae9e1fc

Adam

October 1, 2008, October 01, 2008 05:27, permalink

No rating. Login to rate!

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
20bdcaa65891ace11f18e393c6e1f729

phylae.myopenid.com

October 1, 2008, October 01, 2008 06:16, permalink

No rating. Login to rate!

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
A8d3f35baafdaea851914b17dae9e1fc

Adam

October 1, 2008, October 01, 2008 15:14, permalink

No rating. Login to rate!

I guess I was giving a futuretalk there. It's in edge, or 2.2 when it is released.

Your refactoring





Format Copy from initial code

or Cancel