How to check if today is Friday in Ruby

Overview

We can check if today is Friday in Ruby when you call the friday? method on a time object. It returns a Boolean value. true is returned if today is Friday. Otherwise, false is returned.

Syntax

t_o.friday?
Syntax to check if today is Friday in Ruby

Parameters

t_o: this is the time object. This time object can be created in many ways.

Return value

The value returned is a Boolean value. It returns true if the day or the time represents Friday.

Code example

# create time objects
t1 = Time.now
t2 = Time.new(2020)
t3 = Time.new(1987, 12, 18) # friday December, 1987
# check if Friday
# and print result
puts t1.friday?
puts t2.friday?
puts t3.friday?

Explanation

  • Line 2 and 3: We create two time objects.
  • Line 3: We create an actual time that is Friday.
  • Line 8-10: We check if the time objects are Friday and print their results.

Free Resources