【问题标题】:Why do I get "Undefined method `take' for Array" in Ruby 1.8.7?为什么我在 Ruby 1.8.7 中得到“数组的未定义方法 `take'”?
【发布时间】:2013-04-10 22:03:54
【问题描述】:

我不确定为什么会出现此错误,因为它在 Ruby 1.8.7 中似乎是一个数组。

下面是相关代码:

rows.each_with_index do |row, index|
    if (index == 0) then
        log "Found the following slow statements (took over #{slow_query_threshold} seconds):"
    end
    row_data = row.strip().split('|')
    connections = row_data[1].split(', ')
    n_more = '... and #{connections.length - 3} more' if connections.length > 3
    log %{[#{row_data[0]}] on #{connections.take(3).join(', ')} #{n_more}}
end

这给了我这个错误:

2013-04-10T05:36:45.04Z : Found the following slow statements (took over 5.0 seconds):
/myscript.rb:111: undefined method `take' for ["App15(28.39sec)"]:Array (NoMethodError)
     from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `each_with_index'

【问题讨论】:

  • Ruby 1.8.7 支持take: RUBY_VERSION => "1.8.7" %w[a b c d].take(3) => ["a", "b", "c"]
  • 似乎 ruby​​ 版本可能早于 1.8.7。

标签: ruby ruby-1.8


【解决方案1】:

您必须在 1.8.6 上运行。升级,或require 'backports/1.8.7/enumerable/take'

【讨论】:

  • 在 1.8.5 上运行,因为 #!在我的脚本中指向与 which ruby 不同的版本。
猜你喜欢
  • 1970-01-01
  • 2015-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-24
  • 1970-01-01
  • 2014-06-05
  • 2019-10-31
相关资源
最近更新 更多