struct Measure::Length

Included Modules

Defined in:

length.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(magnitude : Float64, unit : Measure::Length::Unit) #

Instantiate a Length instance with the given #magnitude and Unit.


[View source]
def self.new(pull : JSON::PullParser) #

[View source]

Instance Method Detail

def *(scalar : Number) : self #

Multiply by a scalar value

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

[View source]
def +(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)

[View source]
def -(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)

[View source]
def /(scalar : Number) : self #

Multiply by a scalar value

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

[View source]
def <=>(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

[View source]
def ==(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.


[View source]
def magnitude : Float64 #

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


[View source]
def 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)

[View source]
def to_s(io : IO) : Nil #

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


[View source]
def total_meters : Float64 #

Returns the number of meters represented by this Length instance


[View source]
def unit : Unit #

The unit used in measuring the length.


[View source]