struct

Measure::Length

Inherits JSON::Serializable < Comparable < Struct < Value < Object

Constructors

new(magnitude : Float64, unit : Measure::Length::Unit)

Instantiate a Length instance with the given magnitude and Unit.

Source
new(*, __pull_for_json_serializable pull : JSON::PullParser)
Source

Instance methods

*(scalar : Number) : self

Multiply by a scalar value

2.miles * 5
# => Measure::Length(@magnitude=10.0, @unit=Measure::Length::Unit::Mile)
Source
*(other : Length) : Area

Multiply by another Length to get an Area

2.meters * 3.meters
# => Measure::Area(@magnitude=6.0, @unit=Measure::Area::Unit::SquareMeter)
Source
+(other : self) : self

Add two Lengths of any units together, returning an instance using self's Unit.

1.kilometer + 1.mile
# => Measure::Length(@magnitude=2.6093439485009937, @unit=Measure::Length::Unit::Kilometer)
Source
-(other : self) : self

Subtract a Length from self, returning an instance using self's Unit.

1.mile - 1.kilometer
# => Measure::Length(@magnitude=0.37862878787878795, @unit=Measure::Length::Unit::Mile)
Source
/(scalar : Number) : self

Divide by a scalar value

10.miles / 2
# => Measure::Length(@magnitude=5.0, @unit=Measure::Length::Unit::Mile)
Source
<=>(other : self)

Returns -1 if self is less than other, 0 if they're equal, or -1 otherwise.

1.mile <=> 1.kilometer # => 1
1.mile <=> 1.mile      # => 0
1.kilometer <=> 1.mile # => -1
1.kilometer < 1.mile   # => true
1.kilometer > 1.mile   # => false
Source
==(other : self)

Returns true if self and other are close enough to each other to be considered equivalent — within a femtometer (1/1_000_000_000_000_000th of a meter). This isn't technically correct, but if you need that level of precision, open an issue and we can discuss how to support it.

Source
cubed

Return the cube of this length as a Volume

2.meters.cubed
# => Measure::Volume(@magnitude=8.0, @unit=Measure::Volume::Unit::CubicMeter)
Source
magnitude

The numeric part of the measurement — the 1 in 1.meter.

Source
squared

Return the square of this length as an Area

2.meters.squared
# => Measure::Area(@magnitude=4.0, @unit=Measure::Area::Unit::SquareMeter)
Source
to(unit : Unit) : self

Convert this instance to the given Unit.

1.mile.to(:feet)
# => Measure::Length(@magnitude=5280.0, @unit=Measure::Length::Unit::Foot)
Source
to_s(io : IO) : Nil

Output a human-readable representation of this Length to the given IO.

Source
total_meters

Returns the number of meters represented by this Length instance

Source
unit

The unit used in measuring the length.

Source

Nested types