Ruby:为什么我的数组中所有字符串的末尾都有一个“nil”?
因此,我用 Ruby 编写了一些代码,将文本文件拆分为单独的行,然后根据分隔符对这些行进行分组。然后,此输出被写入一个数组,该数组被传递给一个方法,该方法将 HTML 输出到文本文件中。当我尝试在不同的方法中使用 gsub 将 HTML 文本文件中的占位符替换为 record
数组中的值时,我开始遇到问题 - Ruby 不断告诉我我正在传递 nil 值。在尝试调试程序的该部分几个小时后,我决定看看其他地方,我想我找到了一些东西。下面发布了该程序的修改版本。
这是输入文本文件的示例:
26188
WHL
1
Delco
B-7101
A-63
208-220/440
3
285 w/o pallet
1495.00
C:/img_converted/26188B.jpg
EDM Machine Part 2 of 3
AC Motor, 3/4 Hp, Frame 182, 1160 RPM
|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|
这是我一直在测试的代码片段:
# function to import file as a string
def file_as_string(filename)
data = ''
f = File.open(filename, "r")
f.each_line do |line|
data += line
end
return data
end
Dir.glob("single_listing.jma") do |filename|
content = file_as_string(filename)
content = content.gsub(/\t/, "\n")
database_array = Array.new
database_array = content.split("|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|")
for i in database_array do
record = Array.new
record = i.split("\n")
puts record[0]
puts record[0].class end
end
运行该代码时,我得到以下输出:
john@starfire:~/code/ruby/idealm_db_parser$ ruby putsarray.rb
26188
String
nil
NilClass
... 这意味着 record 中的每个数组位置
显然具有 String
类型的数据 和 类型为 nil
的数据。这是为什么?
So I've written some code in Ruby to split a text file up into individual lines, then to group those lines based on a delimiter character. This output is then written to an array, which is passed to a method, which spits out HTML into a text file. I started running into problems when I tried to use gsub in different methods to replace placeholders in a HTML text file with values from the record
array - Ruby kept telling me that I was passing in nil values. After trying to debug that part of the program for several hours, I decided to look elsewhere, and I think I'm on to something. A modified version of the program is posted below.
Here is a sample of the input text file:
26188
WHL
1
Delco
B-7101
A-63
208-220/440
3
285 w/o pallet
1495.00
C:/img_converted/26188B.jpg
EDM Machine Part 2 of 3
AC Motor, 3/4 Hp, Frame 182, 1160 RPM
|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|
Here is a snippet of the code that I've been testing with:
# function to import file as a string
def file_as_string(filename)
data = ''
f = File.open(filename, "r")
f.each_line do |line|
data += line
end
return data
end
Dir.glob("single_listing.jma") do |filename|
content = file_as_string(filename)
content = content.gsub(/\t/, "\n")
database_array = Array.new
database_array = content.split("|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|")
for i in database_array do
record = Array.new
record = i.split("\n")
puts record[0]
puts record[0].class end
end
When that code is run, I get this output:
john@starfire:~/code/ruby/idealm_db_parser$ ruby putsarray.rb
26188
String
nil
NilClass
... which means that each array position in record
apparently has data of type String
and of type nil
. why is this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
database_array
的维度比您想象的要多。您的节结束标记
|--|--|...|--| 后面有一个换行符。因此,
file_as_string
返回类似这样的内容:然后在节末尾的
split()
变成类似这样的内容:然后您再次拆分每个内容,但是
" \n".split("\n")
给出一个空数组,其第一个元素返回为nil
。Your
database_array
has more dimensions than you think.Your end-of-stanza marker,
|--|--|...|--|
has a newline after it. So,file_as_string
returns something like this:and is then
split()
on end-of-stanza into something like this:You then split each again, but
"\n".split("\n")
gives an empty array, the first element of which comes back asnil
.