5/10日目

全5部のうちの第2部読み終わり。クラスとモジュールの部分が前半の重要ポイントかなーと言う感じ。この辺は少し手を動かしたりした。

#!/usr/bin/ruby

class AccTest
  def initialize(myname = 'instance name')
    @name = myname
    puts "\ninitialized by #{@name}"
  end

  public
  def pub
    puts "This is public method of #{@name}"
    # self.priv
    priv
    self.protec
  end

  private
  def priv
    puts "This is private method of #{@name}"
  end

  protected
  def protec
    puts "This is protected method of #{@name}"
  end
end

class AccTestTest < AccTest
  def pub_chld
    puts "This is public method of #{@name}"
    priv
    self.protec
  end
end

acc = AccTest.new
acc.pub
ruby = AccTest.new("ruby")
ruby.pub
acctesttest = AccTestTest.new("child")
acctesttest.pub_chld

#
# initialized by instance name
# This is public method of instance name
# This is private method of instance name
# This is protected method of instance name
#
# initialized by ruby
# This is public method of ruby
# This is private method of ruby
# This is protected method of ruby
#
# initialized by child
# This is public method of child
# This is private method of child
# This is protected method of child

いまいちわかってない。