Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.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/3/clojure/3.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 Rails视图输出不同于Puma web服务器日志_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails Rails视图输出不同于Puma web服务器日志

Ruby on rails Rails视图输出不同于Puma web服务器日志,ruby-on-rails,ruby,Ruby On Rails,Ruby,我在rails config/initializer中有一个名为abc.rb的ruby脚本 我试图在rails控制器中访问该脚本的类 class UrlsController < ApplicationController def index @url_to_check = Abc.new("http://ecodehut.com/linux") end end 但我的预期结果是: {"it’s"=>1, "like"=>1, "asking"=>1

我在rails config/initializer中有一个名为abc.rb的ruby脚本

我试图在rails控制器中访问该脚本的类

class UrlsController < ApplicationController

  def index
    @url_to_check = Abc.new("http://ecodehut.com/linux")
  end

end
但我的预期结果是:

{"it’s"=>1, "like"=>1, "asking"=>1, "“should"=>1, "i"=>5, "go"=>3, "to"=>14, "school?”"=>1, "and"=>8, "“would"=>1, "get"=>1, "a"=>3, "job"=>1, "if"=>2, "school?”.list"=>1, "of"=>1, "advantages"=>1, "is"=>5, "never"=>1, "ending"=>1, "elaborating"=>1, "each"=>1,  "throw"=>1, "your"=>2, "mouse"=>1, "away"=>1, "this"=>3, "time."=>1, "keyboard"=>1, "all"=>1, "need"=>1, "mozart."=>1, "navigate..."=>1, "before"=>2, "we"=>1, "begin"=>1, "with"=>3, "configuration"=>1,  "here"=>1, "download"=>1, "install"=>2, "system."=>1, "after"=>1, "come"=>1, "back"=>1, "continue."=>1}
我不明白为什么rails会打印散列中的所有内容,而不是像frequency_count方法中提到的值大于5的键值


注意:当使用ruby Abc.rb在终端中运行时,Abc.rb脚本效果良好,请更改此脚本并重试

def frequency_count
    @word_array = @doc.css("p").text.split(" ")
    @occurance = Hash.new(0)
    @word_array.each {|x| @occurance[x.downcase] += 1 }
    @occurance.select {|x, y| y> 5}
end
原因是您的方法的最后一行是

@occurance.each {|x,y|
                if y > 5
                    puts "#{x} : #{y} times"
                end
            }
只需迭代@occurance变量中的所有键和值,但不基于计数进行排序。因此,取而代之的是视图中的所有键值,它们的计数大于5

当您执行@occurance.select{| x,y | y>5}时,它会过滤您的散列并为您提供所需的计数大于5的值。现在您可以循环它并打印内部视图

希望你能理解

更新

在index.html.erb中,将此代码放在希望打印哈希详细信息的位置

<% @occurance.each do |key, value| %>
    <span><%= "#{key}: #{value}" %></span><br />
<% end %>

这是因为您的abc.rb脚本返回的“@occurrence”哈希值与给定值相同。您正在查看的所需输出可能会打印在控制台日志上。因为您只是打印它,而不是存储在任何变量中。查看您的服务器日志到cinfirmyes…正如我提到的,我可以在控制台日志中看到正确的输出我也尝试将其保存到一个变量中,但没有输出请尝试我的答案,它在某种程度上工作@Meimoit,但格式化了输出哈希…当我尝试使用puts或if and else时,它只打印空的{}好的..它只打印y>5,但仍然包含在散列括号{to=>14,and=>8,you=>9,the=>6}中。而且它仍然不是我期望的格式。我试着做到的是:到:14倍和:8倍你:9倍于:6倍,正如你在我的问题结束时所看到的那样,你难道不能做一个循环,并将细节从散列打印到你想要的格式吗@如果我给你看结果,你会感到惊讶吗。正如你所说,我使用了。选择并保存在一个变量中,比如@shortlist=@occurance.select{x,y{y>5},然后我做了这个@shortlist.each{x,y}放入{x}:{y}次,结果是{to=>14,并且=>8,你=>9,这个=>6}。它与的上一个输出相同。选择。但是,我可以在web服务器控制台中看到预期的结果,但在视图中无法实现相同的结果。我不知道发生了什么。我在这个类中有11个方法,除了这个方法,其他的都是金色的。这是唯一的迭代方法。其余的方法输出是相当理想的,但我只有这个问题。请尝试更新的答案。我对Haml synatax不太熟悉@美墨
@occurance.each {|x,y|
                if y > 5
                    puts "#{x} : #{y} times"
                end
            }
<% @occurance.each do |key, value| %>
    <span><%= "#{key}: #{value}" %></span><br />
<% end %>