Class: Physical::Cuboid
- Inherits:
-
Object
- Object
- Physical::Cuboid
- Includes:
- PropertyReaders
- Defined in:
- lib/physical/cuboid.rb
Instance Attribute Summary collapse
-
#dimensions ⇒ Object
readonly
Returns the value of attribute dimensions.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#length ⇒ Object
readonly
Returns the value of attribute length.
-
#properties ⇒ Object
readonly
Returns the value of attribute properties.
-
#weight ⇒ Object
readonly
Returns the value of attribute weight.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #density ⇒ Object
-
#initialize(id: nil, dimensions: [], weight: Measured::Weight(0, :g), properties: {}) ⇒ Cuboid
constructor
A new instance of Cuboid.
- #volume ⇒ Object
Constructor Details
#initialize(id: nil, dimensions: [], weight: Measured::Weight(0, :g), properties: {}) ⇒ Cuboid
Returns a new instance of Cuboid.
11 12 13 14 15 16 17 18 |
# File 'lib/physical/cuboid.rb', line 11 def initialize(id: nil, dimensions: [], weight: Measured::Weight(0, :g), properties: {}) @id = id || SecureRandom.uuid @weight = Types::Weight[weight] @dimensions = [] @dimensions = fill_dimensions(Types::Dimensions[dimensions]) @length, @width, @height = *@dimensions @properties = properties end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Physical::PropertyReaders
Instance Attribute Details
#dimensions ⇒ Object (readonly)
Returns the value of attribute dimensions.
9 10 11 |
# File 'lib/physical/cuboid.rb', line 9 def dimensions @dimensions end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
9 10 11 |
# File 'lib/physical/cuboid.rb', line 9 def height @height end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
9 10 11 |
# File 'lib/physical/cuboid.rb', line 9 def id @id end |
#length ⇒ Object (readonly)
Returns the value of attribute length.
9 10 11 |
# File 'lib/physical/cuboid.rb', line 9 def length @length end |
#properties ⇒ Object (readonly)
Returns the value of attribute properties.
9 10 11 |
# File 'lib/physical/cuboid.rb', line 9 def properties @properties end |
#weight ⇒ Object (readonly)
Returns the value of attribute weight.
9 10 11 |
# File 'lib/physical/cuboid.rb', line 9 def weight @weight end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
9 10 11 |
# File 'lib/physical/cuboid.rb', line 9 def width @width end |
Instance Method Details
#==(other) ⇒ Object
31 32 33 34 |
# File 'lib/physical/cuboid.rb', line 31 def ==(other) other.is_a?(self.class) && id == other&.id end |
#density ⇒ Object
24 25 26 27 28 29 |
# File 'lib/physical/cuboid.rb', line 24 def density return Measured::Density(Float::INFINITY, :g_ml) if volume.value.zero? return Measured::Density(0.0, :g_ml) if volume.value.infinite? Measured::Density(weight.convert_to(:g).value / volume.convert_to(:ml).value, :g_ml) end |
#volume ⇒ Object
20 21 22 |
# File 'lib/physical/cuboid.rb', line 20 def volume Measured::Volume(dimensions.map { |d| d.convert_to(:cm).value }.reduce(1, &:*), :ml) end |