【问题标题】:Cannot load such file -- inifile (LoadError) Ruby无法加载这样的文件——inifile (LoadError) Ruby
【发布时间】:2017-06-11 09:11:11
【问题描述】:

我在执行我的 ruby​​ 脚本时遇到了以下问题。有什么想法吗?

D:\my_ruby>ruby format.rb

C:/Ruby24-x64/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    : cannot load such file -- inifile (LoadError)
            from C:/Ruby24-x64/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55
    :in `require'

            from format.rb:2:in `<main>'

Windows 7 64 位

Ruby 版本 - ruby​​ 2.4.1p111(2017-03-22 修订版 58053)[x64-mingw32]

捆绑器版本 1.15.1

宝石-v:2.6.11

我尝试运行bundle update,但仍然是同样的问题。

代码:

require 'csv'
require 'inifile'

def transform_data(data)
  groups = data.delete('groups').map do |k,v|
    values = v.split(',').map(&:strip).map do |v|
      v.gsub(/[\n\r\0\t]/) do |char|
        case char
        when "\0" then '\0'
        when "\n" then '\n'
        when "\r" then '\r'
        when "\t" then '\t'
        end
      end
    end

    [k, values]
  end.to_h

  data.flat_map do |endpoint, attrs|
    attrs.flat_map do |cluster, access_level|
      c = cluster.gsub('@', '')
      groups[c].map do |id|
        {
          cluster: c,
          id: id,
          case: endpoint,
          access_level: access_level
        }
      end
    end
  end
end

records = File.read('input.txt').split(/(#.*\n|\*+\n)/).each_slice(4).flat_map do |r|
  data = Hash.new { |h,k| h[k] = Hash.new }
  IniFile::Parser.new(data, '=', ';#', 'global').parse r[2]

  file_name = r[0][/(?<=\/)[^\/]*(?=-values\.txt)/]

  transform_data(data).map { |d| { file_name: file_name }.merge(d) }
end

CSV.open('output.csv', 'wb') do |csv|
  csv << records.first.keys
  records.each do |hash|
    csv << hash.values
  end
end

【问题讨论】:

  • 可以看看源代码吗?
  • @Paul Coleman:我已经在我的问题上添加了代码。谢谢。
  • 你是如何安装inifile的?
  • 显然gem已经很久(3年)没有更新了,所以我的猜测是它与ruby 2.4不兼容。您可以尝试使用安装较早的 ruby​​ 版本或跳过该 gem。
  • 这是另一个潜在的宝石props。这个我没试过!

标签: ruby


【解决方案1】:

当我从 Ruby 2.0 升级到 Ruby 2.4 时,我也遇到了同样的问题。只需安装inifile gem:gem install inifile。这个问题应该没有了。

【讨论】:

    猜你喜欢
    • 2019-02-02
    • 2015-03-25
    • 1970-01-01
    • 1970-01-01
    • 2017-05-10
    • 1970-01-01
    • 1970-01-01
    • 2023-01-12
    • 2017-12-04
    相关资源
    最近更新 更多