简体   繁体   English

Rails-升级到ruby 2.2.2-没有将数组隐式转换为String(TypeError)

[英]Rails- upgrading to ruby 2.2.2 - no implicit conversion of Array into String (TypeError)

very new to ruby and rails.. Ive been working on a project, that simply reads in files and parses them to store into a database. 我一直在研究一个项目,该项目只是读取文件并将其解析以存储到数据库中。 Project was running fine, however running the code after an update to ruby 2.2.2 , I received an error that wasn't previously there: 项目运行正常,但是在更新ruby 2.2.2之后运行代码,我收到了以前没有的错误:

in `foreach': no implicit conversion of Array into String (TypeError)

Here is the snippet of the foreach thats causing an error: (let me know if more code is necessary). 这是导致错误的foreach的代码段:(让我知道是否需要更多代码)。

def parse(file_name)
  File.foreach(file_name).with_index do |line, line_num|
    puts "\nLine #{line_num}: #{line}"

Does anyone know whats going on? 有人知道发生了什么吗? Thank you 谢谢

EDIT: Sorry it was a snippet! 编辑:抱歉,这是一个片段! Im calling this ruby code into my rails Test called "parse_log_file_test" 我将此红宝石代码称为“ parse_log_file_test”,称为“ parse_log_file_test”

require File.dirname(__FILE__) + '/../test_helper'

class ParseLogFileTest < ActiveSupport::TestCase

  filename = Array.new

  Dir.glob('database/oag-logs/*.log').each do |log_file|

    filename.push(log_file)

  end

  parser = ParseLogFile.new

  parser.parse(filename)

  test 'parse' do
    parser = ParseLogFile.new

    filename.each do |log_file|

      begin

        parser.parse(log_file)
      rescue

        puts"ERROR: Unable to parse line #{log_file}"

      end
    end
    assert true
  end
end

我猜想您省略了函数的end ,但是如果您没有它,那就需要它。

This error indicates that the argument passed to parse as file_name is an array instead of a string. 此错误表明传递给file_name进行parse的参数是数组而不是字符串。

However, if that's the case, it fails the same on eg Ruby 1.8.4: 但是,在这种情况下,它在Ruby 1.8.4上失败了:

File.foreach([]).with_index do |line, line_num|
  puts "\nLine #{line_num}: #{line}"
end

Output: 输出:

TypeError: can't convert Array into String
    from (irb):1:in `foreach'
    from (irb):1:in `with_index'
    from (irb):1
    from :0

Thus my guess is that the code that produces the value you pass to parse returned a string in your previous Ruby version and returns an array in 2.2.2. 因此,我的猜测是,生成传递给您的值以进行parse的代码在您先前的Ruby版本中返回了一个字符串,并在2.2.2中返回了一个数组。

In your case, the error is caused by the first invocation of parser.parse(...) , right above the test 'parse' do line, not by the invocation inside the test method. 在您的情况下,该错误是由parser.parse(...)test 'parse' do行上方parser.parse(...)的第一次调用引起的,而不是由test方法内部的调用引起的。 I guess you put that invocation there after the migration, probably to debug a problem. 我猜您是在迁移后将调用放在那儿,可能是为了调试问题。 But you are passing a different argument to the first invocation than to the invocation inside the test method, so it fails for a different reason than the one in the test method. 但是,您传递给第一个调用的参数与传递给测试方法内部的调用的参数不同,因此,失败的原因与测试方法中的调用原因不同。

To see what Error is caused inside your test, simply remove the lines 要查看测试内部导致的错误,只需删除以下行

      rescue

        puts"ERROR: Unable to parse line #{log_file}"

(Keep the end or you'll have to remove the begin , too.) (保留end否则您也必须删除begin 。)

This way, an Error will hit the test runner, which will usually display it including message and stack trace. 这样,错误将命中测试运行器,该运行器通常会显示该错误,包括消息和堆栈跟踪。

Agreed with the poster above that you are most likely missing quotation marks. 同意上面的海报,您很可能会丢失引号。 It should have nothing to do with 2.2.2 though, probably you are copy-pastying your file name differently this time around. 但是,它与2.2.2无关,可能这次您复制文件名的方式有所不同。

So apparently this is an issue with upgrading to ruby 2.2.2 on windows. 因此,显然,这是在Windows上升级到ruby 2.2.2的问题。 After getting past this error, I only encountered more errors.. nokogiri..etc 经过这个错误后,我只遇到了更多的错误.. nokogiri..etc

I have recently got a mac and the errors went away. 我最近有一台Mac,错误消失了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError-在Ruby on Rails数组中没有将String隐式转换为Integer - TypeError - no implicit conversion of String into Integer in array Ruby on Rails Ruby on Rails:TypeError(没有将字符串隐式转换为整数) - Ruby on Rails: TypeError (No implicit conversion of String into Integer) 在Rails 5上的TypeError(没有将nil隐式转换为String)ruby - TypeError (no implicit conversion of nil into String) ruby on rails 5 Ruby on Rails:不会将数组隐式转换为字符串(DEVISE) - Ruby on Rails: no implicit conversion of Array into String (DEVISE) 在Ruby on Rails中上传文件后尝试将文件移动到另一个文件夹时,“ TypeError:没有将数组隐式转换为字符串” - “TypeError: no implicit conversion of Array into String” when try to move file into another folder after upload files in Ruby on Rails TypeError:数组未隐式转换为String - TypeError: no implicit conversion of Array into String TypeError:没有将Symbol隐式转换为Integer-Ruby on Rails - TypeError: no implicit conversion of Symbol into Integer - Ruby on Rails 创建方法栏上的TypeError(没有将数组隐式转换为String)活动记录 - TypeError (no implicit conversion of Array into String) active record on create method rails Ruby on Rails-如何将变量插入数组中的字符串? - Ruby on rails- How can I insert a variable into a string in array? 类型错误:在 ruby​​ 中没有将字符串隐式转换为整数 - TypeError: no implicit conversion of String into Integer in ruby
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM