What is the Time.at() method in Ruby?

Overview

The Time.at() method is used to get time in Ruby. It returns the time at a particular period depending on the parameter passed.

Syntax

Time.at(param)

Parameters

param: This could be a time object or a number of seconds in Unix timestamp in the form of an integer.

Return value

The value returned is a time object.

Code

In the example below, we’ll see how to use the Time.at() method.

# get time
a = Time.at(Time.new)
b = Time.at(Time.now)
c = Time.at(946702800)
d = Time.at(Time.new(2002))
# print results
puts a
puts b
puts c
puts d

Explanation

  • Line 2: We pass a time object to the Time.at() method using Time.new, which creates a time object.
  • Line 3: We get a time object by using the Time.now method and pass it to the Time.at() method.
  • Line 4: We pass a number of seconds in integers to the Time.at() method.
  • Line 5: We create a new time object with the Time.new() method and pass the result to the Time.at() method.
  • Lines 8–11: We print the results to the console.

Free Resources