0

I'm going through the Learn Ruby the Hard Way - ex40

Currently, the code works fine. That is not my problem. My problem is every time I add a new song. A) I need to create an instance variable inside the initialize method. B) Then, I have to give it an attr_reader.

What I know if I can A) not have to keep creating new instance variable, but simply variables inside the Song class. B) Not have to create an attr_reader for each variable.

class Song
  def initialize()
    @jcole_lighter = "Come here, I\'m about to take you higher"
    @hold_on_drake = ["Cause you\'re a good girl and you know it",
                          "You act so different around me",
                          "Cause you\'re a good girl and you know it"]
  end


  def sing_me_a_song()
        for line in initialize
            puts line
        end
    end

  attr_reader :jcole_lighter
  attr_reader :hold_on_drake

end


thing = Song.new
puts thing.jcole_lighter()
puts "-"*10
thing= Song.new
puts thing.hold_on_drake()
jmoon90
  • 339
  • 4
  • 17

1 Answers1

1

Check this out for a good explanation of attr_reader, attr_writer, and attr_accessor.

And check this out for learning how to add parameters to the constructor.

You could have :attr_accessor :artists inside Song and in initialize do this:

@artists = Array.new

Then you can have a method add:

def add(artist)
  @artists << artist
end

Just an idea. Always happy to help a Drake fan.

Community
  • 1
  • 1
Vidya
  • 29,932
  • 7
  • 42
  • 70
  • I didn't necessary use the << to artist but used a if-else statement to get lyrics of an artist's if called. – jmoon90 Oct 23 '13 at 11:34