What is time.to_a in Ruby?

Overview

The time.to_a attribute returns an array which contains ten elements when called on an object. Each element represents values for the particular time attribute.

It returns the values:

  • sec: this is the time in seconds
  • min: this is the minute of the time
  • hour: this is the hour of the time
  • day: this is the day of the time
  • month: this is the month of the time
  • year: this is the year of the time
  • wday: this is the weekday value in integer.
  • yday: this represents yearday value in integer.
  • isdst: returns a Boolean value if time occurs during Daylight Saving Time in its time zone. zone: this is the time zone.

Syntax

timeObj.to_a
Syntax to Get all Values of a Time Object

Return value

This attribute returns an array of ten-elements that are values of time object timeObject.

Code example

# create time object
time_object = Time.now # current time
# get ten-element values
puts "#{time_object.to_a}"

Explanation

  • line 2: we created a time object using the Time.now object. And this one we created is the time as of now or current system time.
  • line 5: we printed the results to the console.

Free Resources