Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ruby
rational
communitycreator

What is time.to_r in Ruby?

okwudili onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

to_r is a Ruby attribute that is invoked by a time object. It returns the value of a time object as a rational number of seconds since the Epoch.

Syntax

timeObj.to_r
Syntax to get number of seconds of time as rational number

Return value

What is returned is the value of the time object timeObj as a rational number of seconds since the Epoch.

Code

# create time objects
t1 = Time.now # current time
t2 = Time.at(946702800)
t3 = Time.new(2021)
# get seconds since unix epoch
# as rational number
# and print results
puts t1.to_r
puts t2.to_r
puts t3.to_r
Get seconds of a time object as a rational number

Explanation

  • line 2-4: we created some time objects.
  • line 9-11: the rational number of seconds of the time objects were gotten using the to_r attribute and printed to the console.

RELATED TAGS

ruby
rational
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring