Ruby - 从哈希返回一个特定值

时间:2015-05-07 14:20:18

标签: 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

1 个答案:

答案 0 :(得分:3)

像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']