2015-05-07 79 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)' –