每个块的多维数组

时间:2015-12-04 19:16:27

标签: ruby

我试图遍历我的多维数组,称为播放列表,以确定播放列表的总持续时间。但是,我仍然会收到错误'阻止持续时间':未确定的方法' []'为......

错误发生在list.rb的第31行

Song.rb

class Song
  attr_accessor :name, :artist, :duration, :lyrics

  def initialize(name, artist, duration, lyrics)
    @name = name
    @artist = artist
    @duration = duration #duration in seconds
    @lyrics = lyrics
  end

  def play_song
    #`say #{lyrics}`
    puts "#{lyrics}"
  end

  def friendly_duration
    puts "#{(duration / 60).to_i}" + ":" "#{(duration % 60).to_i}"     
  end

end

List.rb

class List
  attr_reader :songs

  def initialize
    @songs = []
  end

  def add_song(song)
    @songs << song
  end

  def play
    songs.each do |song|
      puts song.lyrics
    end
  end

  def shuffle
    songs.shuffle.each do |song|
      puts song.lyrics
    end  
  end

  def duration
    sum = 0
    songs.each do |song|
      sum += song.duration
    end

    puts "#{(sum / 60).to_i}" + ":" "#{(sum % 60).to_i}"
  end

end

runner.rb

require_relative "song"
require_relative "list"

# Create playlist
playlist = List.new

# Create songs and add them to the list
playlist.add_song(Song.new("Levels", "Avicii", 200, "Ooooo sometimes I get a good feeling..."))
playlist.add_song(Song.new("Poison", "Martin Garrix", 248, "Beep boop boop boop beep beep"))
playlist.add_song(Song.new("Animals", "Martin Garrix", 192, "We are f*ckin animals.."))

playlist.songs[0].play_song
playlist.songs[0].friendly_duration

playlist.play
playlist.shuffle
playlist.duration  

3 个答案:

答案 0 :(得分:2)

会做的伎俩

def duration  
  songs.map{|s| s[2]}.reduce(:+)
end

songs.map{|s| s[2}将提供[200, 248, 192],然后当您致电reduce时,它会200 + 248 + 192并返还总和

答案 1 :(得分:0)

@playlist = [
  ["Levels", "Avicii", 200, "Ooooo sometimes I get a good feeling..."],
  ["Poison", "Martin Garrix", 248, "Beep boop boop boop beep beep"],
  ["Animals", "Martin Garrix", 192, "We are f*ckin animals.."]
]

def duration
  sum = 0
  @playlist.each do |song|  # playlist is a local variable and not visible inside a method,
                            # which has its own scope. @playlist however is visible
    sum += song[2]
  end
  "#{(sum / 60).to_i}" + ":" "#{(sum % 60).to_i}" #no puts, because puts returns nil,
                                                  # which is strange for a method called duration
end

puts duration

答案 2 :(得分:0)

@playlist = [
  ["Levels", "Avicii", 200, "Ooooo sometimes I get a good feeling..."],
  ["Poison", "Martin Garrix", 248, "Beep boop boop boop beep beep"],
  ["Animals", "Martin Garrix", 192, "We are well-bahaved animals.."]

@playlist.transpose[2].reduce(:+)
  #=> 640

考虑制作@playlist哈希的元素:

@playlist = [
  { :name=>"Levels", :artist=>"Avicii", :duration=>200,
    :lyrics=>"Ooooo sometimes I get a good feeling..." },
  { :name=>"Poison", :artist=>"Martin Garrix", :duration=>248,
    :lyrics=>"Beep boop boop boop beep beep" },
  { :name=>"Animals", :artist=>"Martin Garrix", :duration=>192,
    :lyrics=>"We are well-bahaved animals.."}
]

@playlist.reduce(0) { |tot,h| tot + h[:duration] }
  #=> 640