Ruby-从哈希返回特定值

  • 本文关键字:返回 哈希 Ruby- ruby hash
  • 更新时间 :
  • 英文 :


我正在制作一个Ruby应用程序,该应用程序将使某人进入邮政编码时看到其区域中的天气条件。最初,它给了我这么长的哈希(这只是其中的一部分):

ekday_short"=>"Thu", "weekday"=>"Thursday", "ampm"=>"PM",  "tz_short"=>"EDT", "tz_long"=>"America/New_York"}, "period"=>1, "high"=>{"fah
renheit"=>"86", "celsius"=>"30"}, "low"=>{"fahrenheit"=>"70", "celsius"=>"21"}, "conditions"=>"Thunderstorm", "icon"=>"tstorms", "icon_
url"=>"http://icons.wxug.com/i/c/k/tstorms.gif", "skyicon"=>"", "pop"=>80, "qpf_allday"=>{"in"=>0.39, "mm"=>10}, "qpf_day"=>{"in"=>0.3,
 "mm"=>8}, "qpf_night"=>{"in"=>0.08, "mm"=>2}, "snow_allday"=>{"in"=>0.0, "cm"=>0.0}, "snow_day"=>{"in"=>0.0, "cm"=>0.0}, "snow_night"=
>{"in"=>0.0, "cm"=>0.0}, "maxwind"=>{"mph"=>15, "kph"=>24, "dir"=>"ESE", "degrees"=>103}, "avewind"=>{"mph"=>10, "kph"=>16, "dir"=>"ESE
", "degrees"=>103}, "avehumidity"=>59, "maxhumidity"=>0, "minhumidity"=>0}, {"date"=>{"epoch"=>"1431126000", "pretty"=>"7:00 PM EDT on 
May 08, 2015", "day"=>8, "month"=>5, "year"=>2015, "yday"=>127, "hour"=>19, "min"=>"00", "sec"=>0, "isdst"=>"1", "monthname"=>"May", "m
onthname_short"=>"May", "weekday_short"=>"Fri", "weekday"=>"Friday", "ampm"=>"PM", "tz_short"=>"EDT", "tz_long"=>"America/New_York"}, "
period"=>2, "high"=>{"fahrenheit"=>"88", "celsius"=>"31"}, "low"=>{"fahrenheit"=>"72", "celsius"=>"22"}, "conditions"=>"Chance of a Thu
nderstorm", "icon"=>"chancetstorms", "icon_url"=>"http://icons.wxug.com/i/c/k/chancetstorms.gif", "skyicon"=>"", "pop"=>60, "qpf_allday
"=>{"in"=>0.13, "mm"=>3}, "qpf_day"=>{"in"=>0.13, "mm"=>3}, "qpf_night"=>{"in"=>0.0, "mm"=>0}, "snow_allday"=>{"in"=>0.0, "cm"=>0.0}, "
snow_day"=>{"in"=>0.0, "cm"=>0.0}, "snow_night"=>{"in"=>0.0, "cm"=>0.0}, "maxwind"=>{"mph"=>10, "kph"=>16, "dir"=>"SSW", "degrees"=>198
}, "avewind"=>{"mph"=>9, "kph"=>14, "dir"=>"SSW", "degrees"=>198}, "avehumidity"=>64, "maxhumidity"=>0, "minhumidity"=>0}, {"date"=>{"e
poch"=>"1431212400", "pretty"=>"7:00 PM EDT on May 09, 2015", "day"=>9, "month"=>5, "year"=>2015, "yday"=>128, "hour"=>19, "min"=>"00",
 "sec"=>0, "isdst"=>"1", "monthname"=>"May", "monthname_short"=>"May", "weekday_short"=>"Sat", "weekday"=>"Saturday", "ampm"=>"PM", "tz
_short"=>"EDT", "tz_long"=>"America/New_York"}, "period"=>3, "high"=>{"fahrenheit"=>"87", "celsius"=>"31"}, "low"=>{"fahrenheit"=>"74",
 "celsius"=>"23"}, "conditions"=>"Clear", "icon"=>"clear", "icon_url"=>"http://icons.wxug.com/i/c/k/clear.gif", "skyicon"=>"", "pop"=>2
0, "qpf_allday"=>{"in"=>0.0, "mm"=>0}, "qpf_day"=>{"in"=>0.0, "mm"=>0}, "qpf_night"=>{"in"=>0.0, "mm"=>0}, "snow_allday"=>{"in"=>0.0, "
cm"=>0.0}, "snow_day"=>{"in"=>0.0, "cm"=>0.0}, "snow_night"=>{"in"=>0.0, "cm"=>0.0}, "maxwind"=>{"mph"=>10, "kph"=>16, "dir"=>"SSE", "d
egrees"=>157}, "avewind"=>{"mph"=>9, "kph"=>14, "dir"=>"SSE", "degrees"=>157}, "avehumidity"=>63, "maxhumidity"=>0, "minhumidity"=>0}, 
{"date"=>{"epoch"=>"1431298800", "pretty"=>"7:00 PM EDT on May 10, 2015", "day"=>10, "month"=>5, "year"=>2015, "yday"=>129, "hour"=>19,
 "min"=>"00", "sec"=>0, "isdst"=>"1", "monthname"=>"May", "monthname_short"=>"May", "weekday_short"=>"Sun", "weekday"=>"Sunday", "ampm"
=>"PM", "tz_short"=>"EDT", "tz_long"=>"America/New_York"}, "period"=>4, "high"=>{"fahrenheit"=>"86", "celsius"=>"30"}, "low"=>{"fahrenh
eit"=>"75", "celsius"=>"24"}, "conditions"=>"Clear", "icon"=>"clear", "icon_url"=>"http://icons.wxug.com/i/c/k/clear.gif", "skyicon"=>"
", "pop"=>10, "qpf_allday"=>{"in"=>0.0, "mm"=>0}, "qpf_day"=>{"in"=>0.0, "mm"=>0}, "qpf_night"=>{"in"=>0.0, "mm"=>0}, "snow_allday"=>{"
in"=>0.0, "cm"=>0.0}, "snow_day"=>{"in"=>0.0, "cm"=>0.0}, "snow_night"=>{"in"=>0.0, "cm"=>0.0}, "maxwind"=>{"mph"=>15, "kph"=>24, "dir"
=>"ESE", "degrees"=>121}, "avewind"=>{"mph"=>12, "kph"=>19, "dir"=>"ESE", "degrees"=>121}, "avehumidity"=>66, "maxhumidity"=>0, "minhum
idity"=>0}]}}}

等等。其中之一是"conditions"=>"Clear",

我只想返回conditions字段的当前内容,而不是返回整个哈希。当我尝试这样做时,我会得到这个错误: weather.rb:18:in <main> undefined method [] for nil:NilClass (NoMethodError)

weather.rb

require "rubygems"
require 'wunderground'
require 'open-uri'
require 'json'
def wunderground
w = wunderground::Base.new
end
w_api = Wunderground.new("2d2f765c1499b60e")
ask_location = puts "Find out what the weather is like today! Enter your zip code."
zip = gets.chomp

weather = w_api.forecast_and_conditions_for("#{zip}")
puts weather ["weather"]["conditions"]

我一直在看文档,但不确定哪个部分是相关的。

http://ruby-doc.org/core-2.1.5/hash.html#method-i-5b-5d

像马克·托马斯(Mark Thomas)建议一样,您应该使用 awesome_print之类的东西来找出返回的哈希的实际结构。

您可以通过:

来安装awesome_print
gem install awesome_print

这样做后,您可以将代码更新为以下内容:

require 'wunderground'
require 'awesome_print'
w = Wunderground.new('2d2f765c1499b60e')
puts 'Find out what the weather is like today! Enter your zip code.'
zip = gets.chomp
weather = w.forecast_and_conditions_for(zip)
ap weather

查看返回的哈希,您会注意到conditions实际上在这里:

puts weather['forecast']['simpleforecast']['forecastday'][0]['conditions']

最新更新