0

我得到一个带有 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 控制台,我会看到预期值。

4

0 回答 0