InQusitive InQusitive - 1 month ago 13
Ruby Question

No implicit converson from nil to String

I am getting an error

ruby/2.1.0/open-uri.rb:36:in `open': no implicit conversion of nil into String (TypeError)


here
@filename
,
@directory
and
@xmlFile
all have
String
as class type if I print them.
But somehow still in
eval_script
the above error is thrown. I don't undertstand why?

def execute
...
@result = eval_script(@filename,@xmlFile,@directory)
end

def eval_script filename,xml,directory
proc = Proc.new{}
eval(File.read(filename),proc.binding, filename)
end


Edit:
1) execute method is my rails action controller method.

Script:

# encoding: UTF-8
require 'nokogiri'
require 'open-uri'

doc = Nokogiri::XML(open(ARGV.first))
path = ARGV[1]
print path
File.delete(path + "/testOut.txt") if File.exist?(path + "/testOut.txt")
file = File.open(path + "/testOut.txt", 'w')
doc.css('testcases').each { |node| file.write "#{node['name']}\n" if node.css('results[test="testOut"]').any? }

Answer

Well, there's your problem. Line 4 of your script is

doc =  Nokogiri::XML(open(ARGV.first))

But there are no ARGV elements being passed, so you're trying to open nil

Since you have the binding available, just refer to the variables defined in the eval_script method.

doc =  Nokogiri::XML(open(xml))