オブジェクトについて、Objectクラスのメソッドを使って調べる方法をまとめた。
『パーフェクトRuby』とリファレンスマニュアルのclass Objectを参考にした。
1. オブジェクト(レシーバ)のクラスを確認したい
Object#classを使う
'Ruby'.class
# => String
[].class
# => Array
100.class
# => Fixnum
Class.class
# => Module
2. オブジェクトが持つメソッドを確認したい
全ての可視性のメソッドを持つParentとChildクラスを例にする
class Parent
def super_public_method; end
private
def super_private_method; end
protected
def super_protected_method; end
end
class Child < Parent
def public_method; end
private
def private_method; end
protected
def protected_method; end
end
2-1. プライベートメソッド以外のメソッドを確認したい
Object#methodsを使う
child = Child.new
child.methods
# =>[:public_method, :protected_method, :super_public_method, :super_protected_method, ...]
2-2. パブリックメソッドを確認したい
Object#public_methodsを使う
child = Child.new
child.public_methods
# => [:public_method, :super_public_method, :nil?, :===, ...]
# 引数にfalseを指定すると親クラスから継承したメソッドを含めない
child.public_methods(false)
# => [:public_method]
2-3. プライベートメソッドを確認したい
Object#private_methodsを使う
child = Child.new
child.private_methods
# => [:private_method, :super_private_method, :initialize_copy, :initialize_dup...]
# 引数にfalseを指定すると親クラスから継承したメソッドを含めない
child.private_methods(false)
# => [:private_method]
2-4. プロテクテッドメソッドを確認したい
Object#protected_methodsを使う
child = Child.new
child.protected_methods
# => [:protected_method, :super_protected_method]
# 引数にfalseを指定すると親クラスから継承したメソッドを含めない
child.protected_methods(false)
# => [:protected_method]
2-5. 特異メソッドを確認したい
Object#singleton_methodsを使う
child = Child.new
child.singleton_methods
# => []
# オブジェクトに特異メソッドを定義する
def child.singleton_method; end
# => [:singleton_method]
3. オブジェクトに指定したメソッドが定義されているか確認したい
**Object#respond_to?**を使う
class Sample
def one; end
def two; end
private
def three; end
end
sample = Sample.new
sample.respond_to? :one
# => true
sample.respond_to? :two
# => true
sample.respond_to? :three
# => false
# 第2引数にtrueを指定するとprivateメソッドも含めるようになる
sample.respond_to? :three, true
# => true
4. オブジェクトが持つインスタンス変数を確認したい
Object#instance_variablesを使う
class Sample
def initialize
@one = 1
@two = 2
end
end
sample = Sample.new
sample.instance_variables
# => [:@one, :@two]
5. オブジェクトが持つインスタンス変数の値を参照したい
Object#instance_variable_getを使う
class Sample
def initialize
@one = 1
@two = 2
end
end
sample = Sample.new
sample.instance_variable_get :@one
# => 1
sample.instance_variable_get :@two
# => 2
6. オブジェクトに指定したインスタンス変数が定義されているか調べたい
**Object#instance_variable_defined?**を使う
class Sample
def initialize
@one = 1
@two = 2
end
end
sample = Sample.new
sample.instance_variable_defined? :@one
# => true
sample.instance_variable_defined? :@two
# => true
sample.instance_variable_defined? :@three
# => false
7. オブジェクトが指定したクラスの直接のインスタンスであるか調べたい
**Object#instance_of?**を使う
class A
end
class B < A
end
b = B.new
b.instance_of?(B)
# => true
b.instance_of?(A)
# => false
8. オブジェクトが指定したクラスまたはそのサブクラスのインスタンスであるか、あるいは指定したモジュールをインクルードしたクラスまたはそのサブクラスであるかを調べたい
Object#is_a? または Object#kind_of? を使う
module M
end
class A
include M
end
class B < A
end
b = B.new
b.is_a?(B)
# => true
b.is_a?(A)
# => true
b.is_a?(M)
# => true
9. オブジェクトIDを確認したい
Object#object_idを使う
"ruby".object_id # => 70150657363520
[].object_id # => 70150657363400
:ruby.object_id # => 323528