我正在使用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)
然后arr.count{|e| e < 3}
当我点击arr.count{|e| e < 3}
时,我得到2
,但我的观看次数显示为0
。咦?
@result = arr.count{|e| e < 3} # Shows 2 in console but 0 in views. What's going on?
编辑代码视图:
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
如果我将以上内容复制并粘贴到rails控制台中,我会看到预期值。