我正在尝试创建一个类Song,它接受两个输入,即Song和Artist,并创建数组,即[song,artist]。当我运行该代码时,我的对象是数组的断言失败。如何正确编写一个带有两个输入并创建数组对象的initialize方法?
我的代码:
class Song
def initialize(song, artist)
@piece = [song, artist]
end
end
hello = Song.new("hello", "goodbye")
def assert
raise "Assertion failed!" unless yield
end
assert { hello.kind_of?(Array) }
hello
是一个Song
对象,而不是数组对象。你的意思是hello.piece
?
class Song
attr_reader :piece # <---------
def initialize(song, artist)
@piece = [song, artist]
end
end
hello = Song.new("hello", "goodbye")
def assert
raise "Assertion failed!" unless yield
end
assert { hello.piece.kind_of?(Array) } # <------
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句