卡在我的CLI中出现错误[]:Array的未定义方法“标题”ArrayNoMethodError

问题描述

让我以说我对Ruby真的很新作为开头。我正在尝试创建一个CLI,到目前为止,这是我得到的:

class Topspotifysongs::CLI

def start
  Topspotifysongs::Scraper.new
  get_song
end
def get_song
  input=gets.strip.to_i
  song = Topspotifysongs::Songs.get_song_details(input)
  puts "........................................................................"
  show_song_details(song)
  puts "........................................................................"
  puts "Would you like to show another song? (y/n)"
  repeat_or_done
end

def show_song_details(song)
    Topspotifysongs::Songs.get_song_details(song)
    puts "----------- #{song.title} by #{song.artist} -----------"
    puts ""
    puts "Ranking:           #{song.rank}"
    puts ""
    puts "Streams:           #{song.streams}"
    puts ""
    puts "Link to song:      #{song.spotify_url}"
    puts ""
    puts "---------------------------------------------------------------------"
end

这是我的歌曲文件:

class Topspotifysongs::Songs

  attr_accessor :title,:artist,:rank,:streams,:spotify_url

  @@all = []

  def initialize(title=nil,artist=nil,rank=nil,streams=nil,spotify_url=nil)
    @title = title
    @artist = artist
    @rank = rank
    @streams = streams
    @spotify_url = spotify_url
    @@all << self
  end
  
  def self.all
    @@all
  end

  def self.get_song_details(song)
    doc = Nokogiri::HTML(open("https://spotifycharts.com/regional"))
    doc.css("idk").collect do |x|
      title = x.css("td.chart-table-track.strong").text
      artist = x.css("td.chart-table-track.span").text
      rank = x.css("td.chart-table-position").text
      streams = x.css("td.chart-table-streams").text
      spotify_url = x.css("td.a.href").value
    end
  end
end

我确定还需要消除其他错误,但是这个错误困扰了我好几个小时,我的脑子才开始炸:

/home/z****/****/topspotifysongs/lib/topspotifysongs/cli.rb:25:in `show_song_details': undefined method `title' for []:Array (NoMethodError)``` 

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...