2

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) }

2 Answers 2

1

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) } # <------
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this passes! But I'm still not sure if my object is initializing correctly - I feel like this assertion should pass, but maybe I'm formatting it wrong too because it fails: assert {Song.new("hello", "goodbye") == ["hello", "goodbye"]}
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.