0

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:

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).

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"

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
3
  • 2
    what does the call to parse(file_name) look like? Commented Jul 17, 2015 at 21:30
  • What Ruby version were you migrating from? Commented Jul 17, 2015 at 21:58
  • migrating from version 2.1.5 Commented Jul 17, 2015 at 21:59

4 Answers 4

1

I'm guessing you omitted the end to your function, but if you don't have it, you need it.

Sign up to request clarification or add additional context in comments.

3 Comments

sorry it was a snippet! the file itself is longer. I forgot to add that I'm calling this ruby file into my rails test which looks like this
@KennyEllis Add this code to your question (i.e. edit the question to contain it) rather than putting it into a comment. It will be much easier to read there.
thanks for the insight! That makes a lot more sense!
1

This error indicates that the argument passed to parse as file_name is an array instead of a string.

However, if that's the case, it fails the same on e.g. 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.

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. 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.)

This way, an Error will hit the test runner, which will usually display it including message and stack trace.

2 Comments

Unfortunately after commenting out puts ERROR and rescue the same error is present.
You must also remove the first parser.parse(filename). (The one outside the test 'parse' doblock.)
0

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 Comments

It didn't have to do with copy and pasting or the quotation marks
Turns out it was an problem with windows running ruby 2.2.2
0

So apparently this is an issue with upgrading to ruby 2.2.2 on windows. After getting past this error, I only encountered more errors.. nokogiri..etc

I have recently got a mac and the errors went away.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.