Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.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 控制台显示正确的值,但查看';不对_Ruby_Ruby On Rails 5_Ruby On Rails 5.1 - Fatal编程技术网

Ruby 控制台显示正确的值,但查看';不对

Ruby 控制台显示正确的值,但查看';不对,ruby,ruby-on-rails-5,ruby-on-rails-5.1,Ruby,Ruby On Rails 5,Ruby On Rails 5.1,我用HTTPart gem获得一个json对象。我需要迭代,转换为整数,然后执行x: data.first["x"] = ["0", "0", "2", "5" ] 我需要将值转换为int: arr = data.pluck(:x).map(&:to_i) # How to remove all zeros in one go? 我需要删除所有0: arr.delete(0)thenarr.count{e|e

我用HTTPart gem获得一个json对象。我需要迭代,转换为整数,然后执行x:

data.first["x"] = ["0", "0", "2", "5" ]
我需要将值转换为int:

arr = data.pluck(:x).map(&:to_i) # How to remove all zeros in one go?
我需要删除所有
0

arr.delete(0)
then
arr.count{e|e<3}

当我在
arr.count{| e | e<3}
上做一个puts时,我得到
2
,但我的视图显示
0
。嗯

@result = arr.count{|e| e < 3} # Shows 2 in console but 0 in views. What's going on?
@result=arr.count{| e | e<3}#在控制台中显示2,但在视图中显示0。发生什么事?
编辑代码视图:

class ClientController < ApplicationController
 def show
  serpurl = params[:serpurl] # https://serpbook.com/serp/api/?viewkey=2222&auth=2222
  @data = HTTParty.get(serpurl)
  arr = @data.pluck(:x).map(&:to_i).reject(&:zero?)
  @res = arr.count{|e| e < 3}
  puts arr # Shows empty array
 end
end
class ClientController

如果我在rails控制台中复制并粘贴上述内容,我会看到预期值。

通过在rails中添加
拒绝(&:zero?
),由于某种原因使数组在rails中为空,但在控制台中并非如此。在控制器中粘贴
数据
res
值。数据显示数组。res是0。在控制台中时,显示1。我希望看到1。这一定是数据问题。检查每个步骤,如果
数据
匹配,则检查
arr是否匹配。为它添加一个
ruby调试
,实际上发现问题非常简单。请关注此方法
pull(:x)
和数组维度