Rubyのメソッド可視性(public, protected, private)

2021年4月27日火曜日

Ruby

t f B! P L


概要

Rubyにおけるpublic、protected、privateの振る舞いについて

public

  • クラス定義内におけるメソッドのデフォルト設定
class  C
  def  public_method
    p  'publicだよ'
  end
end

c = C.new
c.public_method
=> "publicだよ"
  • どのインスタンスからも実行できる。
class  C
  public
  def  public_method
    p  'publicだよ'
  end
end

c = C.new
c.public_method
=> "publicだよ"

other_c = C.new
c.public_method
=> "publicだよ"

protected

  • クラスのメソッド定義内で呼び出し可能
class  C
  def  call_protected_method
    protected_method
  end

  protected
  def  protected_method
    p  'protectedだよ'
  end
end

c = C.new
c.protected_method
=> NoMethodError  # クラスの外からは呼べない

c.call_protected_method
=> "protectedだよ"  # クラス内から呼び出し可能
  • 自分自身、またはサブクラスのインスタンスから実行できる。
class  Parent
  def  call_protected_method(receiver = self)
    receiver.protected_method
  end

  protected
  def  protected_method
    p  "#{self.class}から呼ばれたprotected_methodだよ"
  end
end

class  Child < Parent; end

parent = Parent.new
parent.call_protected_method
=> "Parentから呼ばれたprotected_methodだよ"

child = Child.new
child.call_protected_method
=> "Childから呼ばれたprotected_methodだよ"

private

  • クラス定義外(トップレベル)におけるメソッドのデフォルト設定
def  private_method
  p  'privateだよ'
end

private_method
=> "privateだよ"
  • レシーバつきで呼び出せない
class  C
  def  call_private_method
    private_method
  end

  private
  def  private_method
    p  'privateだよ'
  end
end

c = C.new
c.private_method
=> NoMethodError  # レシーバがついているから呼べない

c.call_private_method
=> "privateだよ"
  • initialize、initialize_copyは可視性を変更しても常にprivate
class  C
  public
  def  initialize
  end
end

c = C.new
c.initialize
=> NoMethodError  # privateメソッドのためNG

共通

  • サブクラスからレシーバなしで呼び出せる
class Parent
  public
  def public_method
    'publicだよ'
  end

  protected
  def protected_method
    'protectedだよ'
  end

  private
  def private_method
    'privateだよ'
  end
end

class Child < Parent
  def call_public_method
    public_method
  end

  def call_protected_method
    protected_method
  end

  def call_private_method
    private_method
  end
end

child = Child.new

child.call_public_method
=> "publicだよ"

child.call_protected_method
=> "protectedだよ"

child.call_private_method
=> "privateだよ"

自己紹介

Webエンジニアをやっています。日々思ったことや、読書レビュー、IT系の記事などを書き連ねています

広告

[書籍] 世界一楽しい決算書の読み方感想まとめ

  こういう人におすすめ 決算書を読もうとして挫折した人 企業分析したい投資家 会社で経営企画担当、管理職などのポジションの人 概要 著者はTwitterで会計クイズを行なっている 「大手町のランダムウォーカー」さん 。 「日本人全員が財務諸表を読める世界を創る」 を合言葉にして...

QooQ