1

I ran into a study drill problem, and I couldn't figure it out. Here's the link to the exercise. https://learnrubythehardway.org/book/ex40.html

Below are my work. On Study Drill 2, I passed in variables and it worked. However, at study drill 3, I broke my code. I realized I wasn't passing in variable, but a hash. And because my class takes in 2 arguments, I couldn't figure out how to pass a dictionary as 2 arguments.

class Song

  def initialize(lyrics, singer)
    @lyrics = lyrics
    @singer = singer
  end

  def sing_along()
    @lyrics.each {|line| puts line}
  end

    def singer_name()
    puts "The song is composed by #{@singer}"
  end

  def line_reader(lineNum)
    line = @lyrics[lineNum-1]
    puts "The lyrics line #{lineNum} is \"#{line}\"."
  end

end

# The lyrics are arrays, so they have [] brackets
practiceSing = Song.new(["This is line 1",
              "This is line 2",
              "This is line 3"],"PracticeBand")

practiceSing.sing_along()
practiceSing.singer_name()
practiceSing.line_reader(3)

puts "." * 20
puts "\n"

# Variable for passing. Working on dictionary to pass the singer value.
lovingThis = {["Don't know if I'm right",
              "but let's see if this works",
              "I hope it does"] => 'TestingBand'}

# Everything after this line is somewhat bugged
# Because I was using a variable as an argument
# I couldn't figure out how to use dictionary or function to work with 
this

practiceVariable = Song.new(lovingThis,lovingThis)

practiceVariable.sing_along()
practiceVariable.singer_name()
practiceVariable.line_reader(3)

Here's the Output. What it should do is return the singer/band, and return requested lyrics line.

I'm new to coding, please advise how to pass hashes into classes? How to pass lovingThis hash into Song.new() and read as 2 arguments?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Joey
  • 13
  • 5
  • Your lyrics ivar expected to be an array? If so, you could create hash variable where the key is a 'singer_name' and a value is an array of the lyric lines. When passing to class use: Song.new(some_hash['singer_name'], some_hash.keys.first). – Anton Apr 23 '17 at 19:44

1 Answers1

1

you can pass hash to constructor of class in the same way as we pass any other variable, But for that you need to change your constructor definition to take variable number of arguments i.e def initialize(*args)

class Song
  def initialize(*args)
    if args[0].instance_of? Hash
      @lyrics = args[0].keys.first
      @singer = args[0].values.first
    else
      @lyrics = args[0]
      @singer = args[1]
    end
  end

  def sing_along()
    @lyrics.each {|line| puts line}
  end

  def singer_name()
    puts "The song is composed by #{@singer}"
  end

  def line_reader(lineNum)
    line = @lyrics[lineNum-1]
    puts "The lyrics line #{lineNum} is \"#{line}\"."
  end
end

# The lyrics are arrays, so they have [] brackets
practiceSing = Song.new(["This is line 1",
              "This is line 2",
              "This is line 3"],"PracticeBand")

practiceSing.sing_along()
practiceSing.singer_name()
practiceSing.line_reader(3)

puts "." * 20
puts "\n"

# Variable for passing. Working on dictionary to pass the singer value.
lovingThis = {["Don't know if I'm right",
              "but let's see if this works",
              "I hope it does"] => 'TestingBand'}

practiceVariable = Song.new(lovingThis)

practiceVariable.sing_along()
practiceVariable.singer_name()
practiceVariable.line_reader(3)
aqfaridi
  • 719
  • 7
  • 11
  • Thank you so much, I never thought about changing the number of accepted arguments. I've been trying to get 2 arguments by changing hash, or creating functions that returns 2 arguments. This works perfectly! – Joey Apr 23 '17 at 20:30