【问题标题】:Ruby - hashes, symbols and strings [closed]Ruby - 哈希、符号和字符串 [关闭]
【发布时间】:2013-07-03 14:08:49
【问题描述】:

我有返回下一个哈希对象的 gem

{:response=>"There was an error authenticating the sender account.", :status_code=>401}

在我的 rake 任务中,我正在尝试访问它的属性:

response = my_gem.execute
puts response
puts response.has_key?(:respose)
puts response[:respose]

但我不明白为什么会打印出来

{:response=>"There was an error authenticating the sender account.", :status_code=>401}
false

为什么我不能使用 response[:respose] 访问 :response 属性?

附: response[response.keys.first] 有效,但这是非常奇怪的解决方案

【问题讨论】:

  • 这个问题似乎是题外话,因为它是关于一个错字/语法错误

标签: ruby-on-rails ruby gem rake


【解决方案1】:

以下有错别字:

puts response.has_key?(:respose)

has_key?(:respose) 应该是has_key?(:response)

response = {:response=>"There was an error authenticating the sender account.", :status_code=>401}
response.has_key?(:response)
# => true

【讨论】:

  • 是的,漫长的工作日... :(
  • @Olexandr 碰巧我知道...休息一下,奶头 :))
猜你喜欢
  • 2012-03-31
  • 2014-03-20
  • 1970-01-01
  • 1970-01-01
  • 2020-12-13
  • 2021-12-23
  • 1970-01-01
  • 2021-03-26
  • 1970-01-01
相关资源
最近更新 更多