Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.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
Ruby on rails 在Ruby 1.8.7中显示错误的拆分函数_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 在Ruby 1.8.7中显示错误的拆分函数

Ruby on rails 在Ruby 1.8.7中显示错误的拆分函数,ruby-on-rails,ruby,Ruby On Rails,Ruby,我有一段代码是用Ruby 1.8.7编写的 变量表情符号有一个由空格分隔的表情符号列表。然而,当我应用split函数时,我得到了一个错误 lines=[] keywords="" emotes="" Dir["/home/pnpninja/Downloads/aclImdb/train/formatted neg/*"].each do |reviewfile| sum_emote = 0 sum_keyword = 0 lines = File.foreach(reviewfile

我有一段代码是用Ruby 1.8.7编写的 变量表情符号有一个由空格分隔的表情符号列表。然而,当我应用split函数时,我得到了一个错误

lines=[]
keywords=""
emotes=""
Dir["/home/pnpninja/Downloads/aclImdb/train/formatted neg/*"].each do |reviewfile|
  sum_emote = 0
  sum_keyword = 0
  lines = File.foreach(reviewfile.to_s).first(2)
  lines[0].gsub!("\n",'')
  keywords = lines[0].split(" ")
  emotes = lines[1].split(" ")
  keywords.each { |keyword| sum_keyword = sum_keyword + keywordhash[keyword] }
  emotes.each { |emote| sum_emote = sum_emote + emotehash[emote] }
  senti=""
  if sum_emote+sum_keyword >= 0
    senti = "Positive"
  else
    senti = "Negative"
  end
  vv = reviewfile.gsub('formatted neg','analysed neg')
  fin = File.open(vv.to_s, 'a')
  fin << "Emote Weight = #{sum_emote}\n"
  fin << "Keyword Weight = #{sum_keyword}\n"
  fin << "Sentiment : #{senti}"
  fin.close
end
排队

emotes = lines[1].split(" ")

每个文件中的第二行可以是空的,也可以不是空的。

错误告诉您不能对
nil
对象调用
split

重写您的代码以确保没有nil对象,或者如果有问题的对象为nil,则确保不执行任何操作

unless lines[1].nil?
 emotes = lines[1].split(" ")
 emotes.each { |emote| sum_emote = sum_emote + emotehash[emote] }
end

它抱怨“私有方法拆分”,因为它正在拾取。是的,在错误的变量上使用了
.nil?
:)很抱歉,但是您自己看到了它
unless lines[1].nil?
 emotes = lines[1].split(" ")
 emotes.each { |emote| sum_emote = sum_emote + emotehash[emote] }
end