[Learning record] I got the current time in Ruby and output a different greeting for each time.

Introduction

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.

Impressions (mainly where I stumbled)

References

[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

[Learning record] I got the current time in Ruby and output a different greeting for each time.
[Java] for Each and sorted in Lambda
Store in Java 2D map and turn with for statement
[Java] Make variables in extended for statement and for Each statement immutable
[Learning record] I got the current time in Ruby and output a different greeting for each time.
[Deep Learning from scratch] in Java 1. For the time being, differentiation and partial differentiation
I searched for a web framework with Gem in Ruby
A story that a Ruby beginner made and released a LINE BOT that tells the train time in 2 months
Java14 came out, so I tried record for the time being
Get the current date and time by specifying the time zone in Thymeleaf
A memorial service for the library used when competing in Ruby
[Ruby] I want to output only the odd-numbered characters in the character string
The parameters I received in Rails were a bit different than I expected
[Firestore] Extract the collection with where condition in Ruby and delete the record
I thought about the best way to create a ValueObject in Ruby
[Java] for Each and sorted in Lambda
Learning for the first time java [Introduction]
Output the sum of each name and its contents from a multiple array
I tried to solve the tribonatch sequence problem in Ruby (time limit 10 minutes)
Impressions and doubts about using java for the first time in Android Studio
A story about a super beginner participating in the AtCoder contest for the first time (AtCoder Beginner Contest 140)
left4dead2 I made a Docker image for the server and tried running it on GCE # 3 (I had a hard time building the server)