【问题标题】:Cannot load data file in Sinatra无法在 Sinatra 中加载数据文件
【发布时间】:2013-12-11 03:39:45
【问题描述】:

我创建了以下解析器:

require "./artist"
require "./song"
require "./genre"
require "debugger"

class Parser
  attr_accessor :artists, :genres, :song
  attr_reader :mp3


  REGEX = /(?<artist>.*)\s\-\s(?<song>.*)\s\[(?<genre>.*)\]/


  def initialize(directory="data")
    debugger
    @mp3 = Dir.entries(directory).select {|f| !File.directory? f}
    debugger
  end

  def parse
    @mp3.map do |file|
      match = REGEX.match(file)

      artist = Artist.find_by_name(match[:artist]) || Artist.new.tap {|artist| artist.name = match[:artist]}

      song = Song.new
      song.name = match[:song]
      song.genre = Genre.find_by_name(match[:genre]) || Genre.new.tap {|genre| genre.name = match[:genre]}

      #debugger
      artist.add_song(song)
    end
  end

end

a = Parser.new.parse

我尝试通过调用它所在目录 lib 中的 parser.rb 来运行它。我收到以下错误消息:

Parser.rb:47:in `open': No such file or directory - data (Errno::ENOENT)
    from parser.rb:47:in `entries'
    from parser.rb:47:in `initialize'
    from parser.rb:68:in `new'
    from parser.rb:68:in `<main>'

这是我的文件结构: 谁能告诉我为什么它无法识别我的数据目录?我已经凝视了一段时间,无法弄清楚。它就像 10 分钟前一样工作,我不记得我改变了什么让一切都搞砸了。

感谢您的反馈!谢谢

【问题讨论】:

    标签: ruby file require


    【解决方案1】:

    您应该能够从lib 上方的目录中运行您的示例,例如ruby -I/lib lib/parser.rb-I 将设置“包含路径”,这样 ruby​​ 解释器将找到其他所需的 ruby​​ 文件,如 (lib/)song.rb

    【讨论】:

      猜你喜欢
      • 2013-07-04
      • 1970-01-01
      • 1970-01-01
      • 2012-05-28
      • 2012-06-22
      • 1970-01-01
      • 2013-08-11
      • 2012-12-30
      • 2017-03-01
      相关资源
      最近更新 更多