It's been about a month since I started learning Ruby with Progate. I was starting to get too much input, so For the first time, I decided on a theme and made a petite product. I will post it as an article posting practice! It took about 3 hours. ..
As a simple flow, __Get current time __ → __ Group the time you want to change the output __ → Output greeting with __if statement __ It's like that.
time = Time.now
First get the current time.
require "time"
# morning = m
m1 = Time.parse("6:00:00")
m2 = Time.parse("10:59:59")
# afternoon = a
a1 = Time.parse("11:00:00")
a2 = Time.parse("16:59:59")
# evening = e
e1 = Time.parse("17:00:00")
e2 = Time.parse("1:59:59")
#During times other than the above(2:00:00 ~ 5:59:59) zzz...Output with
Since I want to compare the current time with the specified time in the if statement, specify the time in the morning, noon, and night respectively. At this time, by setting ** Time.parse ("hour: minute: second") **, it is converted from a simple character string to a Time object. See references below for more information.
if m1 <= time && time <= m2
puts "Good morning"
elsif a1= time && time <= a2
puts "Hello"
elsif time <= e2 || e1 <= time
puts "Good evening"
else
puts "Zzz..."
The greeting text is output by comparing the specified time grouped with the current time. It's a little confusing,
Morning (6:00:00)-(10:59:59) time zone → Good morning Noon (11:00:00) time zone of ~ (16:59:59) → Hello Time zone from night (17:00:00) to (1:59:59) → Good evening Other time zones (2: 00: 00-5: 59: 59) → zzz ...
Is output.
[Introduction to Ruby 14. Handling Dates and Times (Covering All Patterns)](https://qiita.com/prgseek/items/c0fc2ffc8e1736348486#-%E7%89%B9%E5%AE%9A%E3%81%AE % E6% 97% A5% E6% 99% 82% E3% 81% A7time% E3% 82% AA% E3% 83% 96% E3% 82% B8% E3% 82% A7% E3% 82% AF% E3 % 83% 88% E3% 82% 92% E7% 94% 9F% E6% 88% 90)
Recommended Posts