看板 Ruby 關於我們 聯絡資訊
我在about_classes 這關卡住了... 縮網址是 http://goo.gl/txUal # ------------------------------------------------------------------ class Dog7 attr_reader :name def initialize(initial_name) @name = initial_name end def get_self self end def to_s end def inspect "<Dog named '#{name}'>" end end def test_to_s_is_used_in_string_interpolation fido = Dog7.new("Fido") assert_equal ******, "My dog is #{fido}" end 其中******是作答的部份 把實際跑出來的結果 "My dog is #<Dog7:0x9218758>" 貼上 或是輸入"My dog is Fido" 結果都是 Please meditate on the following. 是我哪邊弄錯了嗎? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.32.165.26 ※ 編輯: Angelliya 來自: 114.32.165.26 (10/16 03:54)
godfat:你的 to_s 定義不對,interpolation 是用 to_s 的結果 10/16 15:59
godfat:而你回傳 nil, ruby 只好用 default 的 10/16 16:00