Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Javaプログラマーが学ぶRuby基礎/継承階層の確認

概要

Rubyの継承階層の確認について説明します。

構文

Rubyの継承階層の確認は以下の構文で利用出来ます。

インスタンス.is_a? クラス名

※is_aはkind_ofでも可

サンプル

class Parent
  attr_accessor:name
  def initialize(name)
    @name=name
  end
end

class Child < Parent
end

child = Child.new("hoge")
puts child.name
puts child.is_a? Parent
puts child.kind_of? Parent
puts child.instance_of? Parent

出力

hoge
true
true
false