The problem
From labs/8-weather.rb in your copy of ruby-intro:
Given the following Ruby hash that represents weather data, write a weather summary out to the screen. Something like this:
Currently it is 67 degrees and Sunny. Forecast for the next 7 days: - 65 degrees and Mostly Cloudy - 70 degrees and Partly Cloudy - 62 degrees and Sunny - 68 degrees and Sunny - 65 degrees and Partly Cloudy with a 70% chance of rain - 72 degrees and Partly Cloudy - 60 degrees and RainyNote: only display the chance of rain if above 50%.
Note: do not display the chance of rain if conditions are already rainy.
Run it from the top folder of the repo:
bin/rails runner labs/8-weather.rb
This one uses everything: a hash, an array inside it, a loop over the array, and a conditional inside the loop.
Steps
- Look at the
weather_datahash. - Find the current conditions and display the first line.
- Find the array of forecast data.
- Loop through it, displaying one line per day.
- Inside the loop, decide whether that day mentions the chance of rain.
1. Look at the hash, and notice the keys
weather_data = {
current: {
temperature: 67,
conditions: "Sunny"
},
forecast: [
{ temperature: 65, conditions: "Mostly Cloudy", precipitation: 0.4 },
...
{ temperature: 60, conditions: "Rainy", precipitation: 0.9 }
]
}
The top level has two keys, current and forecast. current is a hash. forecast is an array of hashes, one per day: a table, with a row for each day.
The keys are written differently from the hashes we've built so far. current: { ... } is short for :current => { ... }. The key is a symbol, a word that starts with a colon, not a string. The hashes code-along mentioned this at the end: two ways to write a hash. It matters here because a symbol key and a string key are different keys:
puts weather_data["current"] # nothing: there is no "current" key
puts weather_data[:current] # {temperature: 67, conditions: "Sunny"}
If you get nil, or an error like undefined method '[]' for nil, this is the first thing to check. Use the key the way the hash was written.
2. The current conditions
Start with the sample line exactly as written, then swap the parts that should change:
puts "Currently it is 67 degrees and Sunny."
current = weather_data[:current]
puts "Currently it is #{current[:temperature]} degrees and #{current[:conditions]}."
3 and 4. Loop through the forecast
We could pull out each day by its index, weather_data[:forecast][0], then [1], and so on. But that's seven nearly identical lines, and it breaks the day the forecast has eight days. A loop does it once, for however many days there are:
puts "Forecast for the next #{weather_data[:forecast].size} days:"
for day in weather_data[:forecast]
puts "- #{day[:temperature]} degrees and #{day[:conditions]}"
end
Each time around, day is one hash from the array. .size counts the days, so the heading is right even if the forecast changes.
5. The chance of rain
Two notes in the problem, two conditions, and both have to be true:
- the chance of rain is above 50%:
day[:precipitation] > 0.5 - it isn't already rainy:
day[:conditions] != "Rainy"
&& means both. != means "is not equal to."
if day[:precipitation] > 0.5 && day[:conditions] != "Rainy"
chance_of_rain = (day[:precipitation] * 100).round
puts "- #{day[:temperature]} degrees and #{day[:conditions]} with a #{chance_of_rain}% chance of rain"
else
puts "- #{day[:temperature]} degrees and #{day[:conditions]}"
end
precipitation is stored as a fraction, 0.7, so we multiply by 100 for a percentage. 0.7 * 100 is 70.0, a Float, which would print as "70.0%"; .round makes it a whole number.
Check the output against the notes. Day 5 (0.7, Partly Cloudy) mentions rain. Day 7 (0.9) has a higher chance, but it's already Rainy, so it doesn't. Day 1 (0.4) is under 50%.
Final code
# 1. inspect the weather_data hash
# puts weather_data
# 2. the current conditions; the keys are symbols, so :current, not "current"
current = weather_data[:current]
puts "Currently it is #{current[:temperature]} degrees and #{current[:conditions]}."
# 3. the forecast is an array of hashes, one per day
puts "Forecast for the next #{weather_data[:forecast].size} days:"
# 4. loop through it, one line per day
for day in weather_data[:forecast]
# 5. mention the chance of rain only if it's above 50% and it isn't already rainy
if day[:precipitation] > 0.5 && day[:conditions] != "Rainy"
chance_of_rain = (day[:precipitation] * 100).round
puts "- #{day[:temperature]} degrees and #{day[:conditions]} with a #{chance_of_rain}% chance of rain"
else
puts "- #{day[:temperature]} degrees and #{day[:conditions]}"
end
end
Output:
Currently it is 67 degrees and Sunny.
Forecast for the next 7 days:
- 65 degrees and Mostly Cloudy
- 70 degrees and Partly Cloudy
- 62 degrees and Sunny
- 68 degrees and Sunny
- 65 degrees and Partly Cloudy with a 70% chance of rain
- 72 degrees and Partly Cloudy
- 60 degrees and Rainy
Challenge
Add a line under the forecast that says which day will be the warmest, something like "The warmest day will be day 6, at 72 degrees." You'll need a variable that remembers the highest temperature so far, set before the loop and updated inside it.