中級 › attr_writer だけでは値を読み出せない(attr_accessor との違い) › パターン4
attr_writer だけでは値を読み出せない(attr_accessor との違い)
attr_writerは代入用のメソッド(name=)だけを定義し、読み出し用のメソッド(name)は定義しません。
1 class Profile 2 attr_________ :bio ^ 3 4 def initialize(bio) 5 self.bio = bio 6 end 7 end 8 9 item = Profile.new("hi") 10 puts item.bio
main.rb:10:in `<main>': undefined method `bio' for an instance of Profile (NoMethodError)
空欄を埋めて実行を通す
attr_writerだけのクラスでは、初期化した値を後から読み出す手段がありません。
次の問題