Time in Ruby
Ruby offers extensive support for times and durations; here are some examples.
To run the program, save it as time_example.rb
and use ruby
:
Ruby’s Time
class provides similar functionality to Go’s time
package. The main differences are:
- Ruby uses method calls instead of separate functions (e.g.,
time.Now()
in Go becomes Time.now
in Ruby). - Ruby’s
Time
objects are mutable, unlike Go’s time.Time
structs. - Ruby represents durations as simple Float values (seconds), while Go has a separate
Duration
type. - Ruby’s time arithmetic is done with standard
+
and -
operators, while Go uses methods like Add
and Sub
.
Despite these differences, you can achieve similar time-related operations in Ruby as you can in Go.
Next, we’ll look at the related idea of time relative to the Unix epoch.