struct
Measure::Length
- Measure::Length
- Struct
- Value
- Object
Included Modules
- Comparable(Measure::Length)
- JSON::Serializable
Defined in:
length.crConstructors
-
.new(magnitude : Float64, unit : Measure::Length::Unit)
Instantiate a
Lengthinstance with the given#magnitudeandUnit. - .new(pull : JSON::PullParser)
- .new(*, __pull_for_json_serializable pull : JSON::PullParser)
Instance Method Summary
-
#*(scalar : Number) : self
Multiply by a scalar value
- #*(other : Length) : Area
- #+(other : self) : self
- #-(other : self) : self
-
#/(scalar : Number) : self
Divide by a scalar value
-
#<=>(other : self)
Returns
-1ifselfis less thanother,0if they're equal, or-1otherwise. -
#==(other : self)
Returns
trueifselfandotherare close enough to each other to be considered equivalent — within a femtometer (1/1_000_000_000_000_000th of a meter). -
#cubed : Volume
Return the cube of this length as a
Volume -
#magnitude : Float64
The numeric part of the measurement — the
1in1.meter. -
#squared : Area
Return the square of this length as an
Area -
#to(unit : Unit) : self
Convert this instance to the given
Unit. -
#to_s(io : IO) : Nil
Output a human-readable representation of this
Lengthto the givenIO. -
#total_meters : Float64
Returns the number of meters represented by this
Lengthinstance -
#unit : Unit
The unit used in measuring the length.
Constructor Detail
Instantiate a Length instance with the given #magnitude and Unit.
Instance Method Detail
Multiply by a scalar value
2.miles * 5
# => Measure::Length(@magnitude=10.0, @unit=Measure::Length::Unit::Mile)
Multiply by another Length to get an Area
2.meters * 3.meters
# => Measure::Area(@magnitude=6.0, @unit=Measure::Area::Unit::SquareMeter)
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)
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)
Divide by a scalar value
10.miles / 2
# => Measure::Length(@magnitude=5.0, @unit=Measure::Length::Unit::Mile)
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
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.
Return the cube of this length as a Volume
2.meters.cubed
# => Measure::Volume(@magnitude=8.0, @unit=Measure::Volume::Unit::CubicMeter)
Return the square of this length as an Area
2.meters.squared
# => Measure::Area(@magnitude=4.0, @unit=Measure::Area::Unit::SquareMeter)
Convert this instance to the given Unit.
1.mile.to(:feet)
# => Measure::Length(@magnitude=5280.0, @unit=Measure::Length::Unit::Foot)