How to know if today or a Time Object is Sunday in Ruby
Overview
We use the sunday? method to learn if today or a time object is Sunday or represents Sunday. The method returns a Boolean value true or false. If the time object is Sunday, true is returned. Otherwise, false.
Syntax
t_o.sunday?
Syntax to Check if Today is Sunday
Parameters
t_o: This is the time object we want to check for representing Sunday.
Return value
The value returned is a Boolean value. If the time object is Sunday, true will be returned. Otherwise, false will be returned.
Code example
# create time objectst1 = Time.now # current timet2 = Time.new(1990)t3 = Time.new(1990, 4, 1) # sunday in 1998t4 = Time.new()# call the "sunday?" method# and print resultputs t1.sunday?puts t2.sunday?puts t3.sunday?puts t4.sunday?
Explanation
- line 2-5: We create time objects using the
Time.nowor theTime.new()methods. - line 9-12: We use the
sunday?method to check if the time objects we create are actually Sunday or not. Then we printed the results.