struct Measure::Weight

Included Modules

Defined in:

weight.cr

Constructors

Instance Method Summary

Constructor Detail

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

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


[View source]

Instance Method Detail

def *(scalar : Number) : self #

Multiply by a scalar value

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

[View source]
def +(other : self) : self #

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

1.kilogram + 1.pound
# => Measure::Weight(@magnitude=1.4535929094356397, @unit=Measure::Weight::Unit::Kilogram)

[View source]
def -(other : self) : self #

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

1.kilogram - 1.pound
# => Measure::Weight(@magnitude=0.5464070905643603, @unit=Measure::Weight::Unit::Kilogram)

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

Multiply by a scalar value

10.kg / 2
# => Measure::Length(@magnitude=2.0, @unit=Measure::Length::Unit::Kilogram)

[View source]
def <=>(other : self) #

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

1.kg <=> 1.lb # => 1
1.lb <=> 1.lb # => 0
1.lb <=> 1.kg # => -1
1.lb < 1.kg   # => true
1.lb > 1.kg   # => false

[View source]
def ==(other : self) #

Returns true if self and other are close enough to each other to be considered equivalent — within a femtogram (1/1_000_000_000_000_000th of a gram). 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.kilogram.


[View source]
def to(unit : Unit) : self #

Convert this instance to the given Unit.

1.kilogram.to(:lbs)
# => Measure::Weight(@magnitude=2.20462, @unit=Measure::Weight::Unit::Pound)

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

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


[View source]
def total_grams #

Returns the number of grams represented by this Weight instance


[View source]
def unit : Unit #

The unit used in measuring the weight.


[View source]