Reputation: 343
I want to show a greeting message through out the day. If it's 12 am through 11:59 am i want a good morning message and so on. This is what i have but cant seem to get it working.
def greet
if Time.now <= Time.now.beginning_of_day
render :text =>"Good Morning"
elsif Time.now <= Time.now.middle_of_day
render :text => "Good Afternoon"
elsif Time.now <= Time.now.change(:hour => 5 )
render :text => "Good Evening"
elsif Time.now <= Time.now.change(:hour => 8 )
render :text =>"Good Night"
end
end
I will be displaying something like this in my views.
<# This should display a greeting to the user %>
<%= greet %> <%= current_user.name %>
Upvotes: 2
Views: 2939
Reputation: 491
Giving my 20 cents:
def greet
case Time.zone.now.hour
when 4..11 then 'Bom dia'
when 12..17 then 'Boa tarde'
when 18..23 then 'Boa noite'
else
'Olá'
end
end
There is also an option with hash (IMO it is ugly):
{
0..11 => 'Good Morning',
12..17 => 'Good Afternoon',
18..23 => 'Good Night'
}.find { |k, v| break v if k.cover? Time.zone.now.hour }
Upvotes: 3
Reputation: 71
You can use between?
method from Comparable
module.
def greet
if Time.now.between? Time.now.beginning_of_day, Time.now.middle_of_day
render :text =>"Good Morning"
elsif Time.now.between? Time.now.middle_of_day, Time.now.change(hour: 17)
render :text => "Good Afternoon"
elsif Time.now.between? Time.now.change(hour: 17), Time.now.change(hour: 20)
render :text => "Good Evening"
elsif Time.now.between? Time.now.change(hour: 20), Time.now.end_of_day
render :text =>"Good Night"
end
end
Upvotes: 0
Reputation: 818
Surprisingly @daslicious answer gave can't iterate from Time
error. So I modified it to use the if-else
conditionals.
def greet
now = Time.now
today = Date.today.to_time
morning = today.beginning_of_day
noon = today.noon
evening = today.change( hour: 17 )
night = today.change( hour: 20 )
tomorrow = today.tomorrow
if (morning..noon).cover? now
'Good Morning'
elsif (noon..evening).cover? now
'Good Afternoon'
elsif (evening..night).cover? now
'Good Evening'
elsif (night..tomorrow).cover? now
'Good Night'
end
end
Upvotes: 0
Reputation: 1534
now = Time.now
today = Date.today.to_time
morning = today.beginning_of_day
noon = today.noon
5pm = today.change( hour: 17 )
8pm = today.change( hour: 20 )
tomorrow = today.tomorrow
message = case now
when morning..noon
'Good Morning'
when noon..5pm
'Good Afternoon'
when 5pm..8pm
'Good Evening'
when 8pm..tomorrow
'Good Night'
end
render text: message
I don't think Time.now
will ever be less than Time.now.beginning_of_day
Upvotes: 1
Reputation: 7070
First, let's make the time a bit easier to read. I set the five variables below based on your conditions. I also convert these to epoch time via to_i
current_time = Time.now.to_i
midnight = Time.now.beginning_of_day.to_i
noon = Time.now.middle_of_day.to_i
five_pm = Time.now.change(:hour => 17 ).to_i
eight_pm = Time.now.change(:hour => 20 ).to_i
You can then use your if
conditions to display the correct text (or render as you had it).
if midnight.upto(noon).include?(current_time)
puts "Good Morning"
elsif noon.upto(five_pm).include?(current_time)
puts "Good Afternoon"
elsif five_pm.upto(eight_pm).include?(current_time)
puts "Good Evening"
elsif eight_pm.upto(midnight + 1.day).include?(current_time)
puts "Good Night"
end
Or, you can move away from if
statements and use case
.
case
when midnight.upto(noon).include?(current_time)
puts "Good Morning"
when noon.upto(five_pm).include?(current_time)
puts "Good Afternoon"
when five_pm.upto(eight_pm).include?(current_time)
puts "Good Evening"
when eight_pm.upto(midnight + 1.day).include?(current_time)
puts "Good Night"
end
Personally, what you had works, but you're not creating true definitions of the boundaries. While this may not be a big deal, I do like having definitive boundaries set just in case something happens down the road which could cause problems.
Upvotes: 12