Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if its morning or afternoon in ruby

Tags:

ruby

How can i print morning if the Time.new is morning and afternoon if its afternoon

00:00 - 12:00

12:00 - 00:00

like image 244
ahmet Avatar asked Nov 28 '22 09:11

ahmet


1 Answers

Another possibility if you will be doing this test a lot (not that I am advocating monkey patching at the drop of a hat):

class Time
  def morning?
    hour < 12
  end
  def afternoon?
    hour >= 12
  end
end

puts Time.now.morning? ? 'morning' : 'afternoon'
like image 165
Russell Avatar answered Dec 23 '22 08:12

Russell