为什么这个减去两个相同数组的ruby语句会失败.empty?测试?

时间:2017-08-27 18:48:30

标签: arrays ruby is-empty

在此片段中(来自https://github.com/bitfinexcom/bitfinex-api-rb/blob/master/lib/bitfinex/connection.rb的第19行 - 这也是包含check_params方法定义的类的源。)

if (params.keys - allowed_params).empty?

params.keys是一个数组,allowed_params是一个数组。为什么减法不能通过.empty测试?

将其设置为重复此操作:

$gem install bitfinex-rb pry

Ruby源

#!/opt/local/bin/ruby

# Annoying!
# Stuff is installed to $GEMPATH/bitfinex-rb-0.0.11
# But you have to invoke it as below.
require 'pry'
require 'bitfinex'

Bitfinex::Client.configure do |conf|
  conf.secret = ENV["BFX_API_SECRET"]
  conf.api_key = ENV["BFX_API_KEY"]
end

client = Bitfinex::Client.new

history_req = {
      'since' => 1444277602,
      'until' => 0,
      'limit' => 500,
      'wallet' => "exchange"
}

puts
print history_req.keys
puts
puts history_req.keys

client.history(
   currency = "USD",
   history_req
)

脚本输出(来自pry的额外帮助)

  

$ pry wtf.rb {“account”=> {“read”=> true,“write”=> false},   “history”=> {“read”=> true,“write”=> false},“orders”=> {“read”=> true,   “write”=> true},“positions”=> {“read”=> true,“write”=> false},   “funding”=> {“read”=> true,“write”=> true},“wallets”=> {“read”=> true,   “write”=> false},“withdraw”=> {“read”=> false,“write”=> false}}

     

[“自”,“直到”,“限制”,“钱包”]直到限制钱包

     

异常:Bitfinex :: ParamsError:Bitfinex :: ParamsError    - 来自:/opt/local/lib/ruby2.4/gems/2.4.0/gems/bitfinex-rb-0.0.11/lib/bitfinex/connection.rb   @ line 23 @ level:0回溯(22)。

18:     # Make sure parameters are allowed for the HTTP call
19:     def check_params(params, allowed_params)
20:       if (params.keys - allowed_params).empty?
21:         return params
22:       else
23:         raise Bitfinex::ParamsError
24:       end
25:     end
26: 
27:     def rest_connection
28:       @conn ||= new_rest_connection ...exception encountered, going interactive! [71] pry(main)>

github上的Bitfinex gem的来源:

https://github.com/bitfinexcom/bitfinex-api-rb

1 个答案:

答案 0 :(得分:0)

让我们看看docs for the history method

  

参数:

     
      
  • currency string)(默认为:" usd" ) - (可选) )指定货币,默认为“USD”
  •   
  • params 默认为:{} ) - :since [time](可选)仅返回此时间戳后的历史记录。< / LI>   
  • params 默认为:{} ) - :until [time](可选)仅返回此时间戳之前的历史记录。< / LI>   
  • params 默认为:{} ) - :limit [int](可选)限制要返回的条目数。默认值为500。
  •   
  • params 默认为:{} ) - :wallet [string](可选)仅返回此钱包中发生的条目。接受的输入   是:“交易”,“交换”,“存款”
  •   

因此params中允许的密钥为:since:until:limit:wallet

params中的密钥为'since''until''limit''wallet'

allowed_params = [:since, :until, :limit, :wallet]
params = {
  'since' => 1444277602,
  'until' => 0,
  'limit' => 500,
  'wallet' => "exchange"
}

params.keys - allowed_params
# => [:since, :until, :limit, :wallet]

(params.keys - allowed_params).empty?
# => false

解决方案是将history_req的定义更改为使用符号键而不是字符串。

history_req = {
  since: 1444277602,
  until: 0,
  limit: 500,
  wallet: "exchange"
}

您可以在the history method's source中看到allowed_params的来源:

def history(currency="usd", params = {})
  check_params(params, %i{since until limit wallet})
  # ...
end

构造%i{...}(注意iproduces an array of Symbols,ergo [:since, :until, :limit, :wallet]