Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html Ruby:`gsub';:can';t将nil转换为字符串(TypeError)_Html_Ruby_Parsing_Gsub - Fatal编程技术网

Html Ruby:`gsub';:can';t将nil转换为字符串(TypeError)

Html Ruby:`gsub';:can';t将nil转换为字符串(TypeError),html,ruby,parsing,gsub,Html,Ruby,Parsing,Gsub,上面的函数让我感到很舒服。首先,它从文本文件中读入一些字符串。然后,它读入一个文本文件,该文件是HTML表的一部分的模板。接下来,它用字符串的内容替换模板文件中的某些关键字,最后,它将所有内容推送到一个新的文本文件(page_export.html) 这里的问题是,文本文件中导入的某些字段是空白的,或者至少,我认为这就是问题所在。无论哪种方式,我都会遇到以下错误: def export_no_sdesc(item_no = " ", make = " ", model = " ", list_p

上面的函数让我感到很舒服。首先,它从文本文件中读入一些字符串。然后,它读入一个文本文件,该文件是HTML表的一部分的模板。接下来,它用字符串的内容替换模板文件中的某些关键字,最后,它将所有内容推送到一个新的文本文件(page_export.html)

这里的问题是,文本文件中导入的某些字段是空白的,或者至少,我认为这就是问题所在。无论哪种方式,我都会遇到以下错误:

def export_no_sdesc(item_no = " ", make = " ", model = " ", list_price = " ", long_desc = " ", global_image_path = " ")
final_image_path = global_image_path + item_no + ".jpg"
final_thumbs_path = global_image_path + "thumbs/" + item_no + ".jpg"
Dir.glob("body.tmp") do |filename|
    body = file_as_string(filename)
    body = body.gsub("item_no", item_no).gsub("image_path", final_image_path).gsub("image_thumb", final_thumbs_path)
    body = body.gsub("part_make", make).gsub("part_model", model).gsub("long_desc", long_desc).gsub("list_price", list_price)
    File.open('page_export.html', 'a') do |x|
        x.puts body
        x.close
    end
  end
end
为了解决这个问题,我不仅为每个字符串声明了一个空白作为默认参数,而且在脚本的另一部分中,我循环遍历每个字符串——如果它是空的,我会附加一个空白。还是不走运

我有一个与上面的函数几乎相同的函数,但是它在一组稍有不同的数据上运行-一个没有任何空字符串的数据集-并且它工作得很好。我还测试了附加空格的代码,它也可以正常工作


那么,我做错了什么呢?

很简单,您的函数参数之一是
nil
。如果传入
nil
,则是否提供了默认的空字符串并不重要

我们无法从提供的代码中判断哪个参数是nil,所以请全部检查它们,并假设抛出错误,开始逐个检查它们。将以下内容添加到函数顶部:

john@starfire:~/code/ruby/idealm_db_parser$ ruby html_export.rb
html_export.rb:34:in `gsub': can't convert nil into String (TypeError)
from html_export.rb:34:in `export_no_sdesc'
from html_export.rb:31:in `glob'
from html_export.rb:31:in `export_no_sdesc'
from html_export.rb:82
from html_export.rb:63:in `each'
from html_export.rb:63
from html_export.rb:56:in `glob'
from html_export.rb:56

更新

默认参数不会阻止您传入
nil
。它们只有在你不提供任何东西的情况下才会生效

在这里:

改变

def test(x = 3)
  puts x
end

test()    # writes '3'
test(nil) # writes 'nil'
进入


如果它给你零,那么你在body.tmp文件中有一些问题。

我想我对“零”的概念还不太了解。。。nil是它自己的数据类型吗?如中所示,如果某个对象为nil,我不能向其追加空格,因为空格的类型为string?
nil
的类型为
NilClass
,您不能向其追加字符串。你认为你在这个函数之外做的任何检查显然都不起作用。唯一可能导致错误的方法是如果
gsub
的一个参数为nil。它们不会为nil,因为在方法中它们会获得默认值,不是吗?@m4risU默认值不是这样工作的。它们可以是零,几乎可以肯定是零。看看我的更新。哦,哇。我不知道,非常感谢。但是,有没有办法用空字符串替换nil值?如果没有数据,我只需要该字段为空。感谢meagar提供的显示修复:)是的,对不起,我仍在试图将我的头绕到Markdown…嗯,这就是“body.tmp”的内容:
item\u no Make:part\u Make
Model:part\u Model

long\u desc list\u price
我几乎可以肯定这是我试图通过的参数之一,但我会测试它。谢谢。试过你说的,得到这个<代码>john@starfire:~/code/ruby/idealm\u db\u parser$ruby html\u export.rb html\u export.rb:34:inthrow:uncaught throw
(namererror)item\u no Make:part\u Make
Model:part\u Model
long\u desc list\u price'
def test(x = 3)
  puts x
end

test()    # writes '3'
test(nil) # writes 'nil'
    body = file_as_string(filename)
    throw body = file_as_string(filename)