2015-05-07 60 views
0

我正在制作一个Ruby应用程序,它允许某人在输入邮政编码时查看其区域中的天气状况。起初,它给了我这个长散列(这只是它的一部分):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

您提供的这个散列没有条件键,pas完整的散列。 –

+0

我现在将它添加...我只是不知道如何正确格式化整个字符串 –

+0

你粘贴的哈希是无效的。 –

回答

3

像马克·托马斯建议,你应该使用类似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'] 
+0

如何查看/复制我终端的整个散列输出?它不会让我一直向上滚动。我正在使用Cloud 9 IDE。 –

+0

我不知道Cloud 9 IDE,对不起。无论如何,你要找的是我的答案的底部:'puts weather ['forecast'] ['simpleforecast'] ['forecastday'] [0] ['conditions']' – Mischa

+0

当我使用它时,它说'没有将字符串隐式转换为整数(TypeError)' –