Reputation: 25
I'm trying to create a class Song that takes in two inputs, song and artist, and creates objects that are arrays i.e. [song, artist]. When I run this code, my assertion that my object is an array fails. How can I correctly write an initialize method that takes in two inputs and creates an array object?
My code:
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) }
Upvotes: 2
Views: 60
Reputation: 15967
your assertion assumes that hello
is an array
which is incorrect. hello
is an instance of the class Song
.
However, if you did added this to the top of your class:
attr_reader :piece
and then did this
assert { hello.piece.kind_of?(Array) }
that would pass.
Upvotes: 1
Reputation: 369164
hello
is a Song
object, not an array object. Do you mean 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) } # <------
Upvotes: 1