Class: Physical::Cuboid
- Inherits:
-
Object
- Object
- Physical::Cuboid
- Includes:
- PropertyReaders
- Defined in:
- lib/physical/cuboid.rb
Overview
Represents a cube-shaped physical object with dimensions and weight.
Instance Attribute Summary collapse
-
#dimensions ⇒ Array<Measured::Length>
readonly
The length, width, and height of this cuboid.
-
#height ⇒ Measured::Length
readonly
The height of this cuboid.
-
#id ⇒ String
readonly
A unique identifier for this cuboid.
-
#length ⇒ Measured::Length
readonly
The length of this cuboid.
-
#properties ⇒ Hash
readonly
Additional custom properties for this cuboid.
-
#weight ⇒ Measured::Weight
readonly
The weight of the cuboid itself (excluding what's inside).
-
#width ⇒ Measured::Length
readonly
The width of this cuboid.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Returns true if the given object shares the same class and ID with this cuboid.
-
#density ⇒ Measured::Density
Calculates and returns this cuboid's density based on its volume and weight.
-
#initialize(id: nil, dimensions: [], weight: Measured::Weight(0, :g), properties: {}) ⇒ Cuboid
constructor
A new instance of Cuboid.
-
#volume ⇒ Measured::Volume
Calculates and returns this cuboid's volume based on its dimensions.
Constructor Details
#initialize(id: nil, dimensions: [], weight: Measured::Weight(0, :g), properties: {}) ⇒ Cuboid
Returns a new instance of Cuboid.
42 43 44 45 46 47 48 49 |
# File 'lib/physical/cuboid.rb', line 42 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 |
Instance Attribute Details
#dimensions ⇒ Array<Measured::Length> (readonly)
The length, width, and height of this cuboid
16 17 18 |
# File 'lib/physical/cuboid.rb', line 16 def dimensions @dimensions end |
#height ⇒ Measured::Length (readonly)
The height of this cuboid
28 29 30 |
# File 'lib/physical/cuboid.rb', line 28 def height @height end |
#id ⇒ String (readonly)
A unique identifier for this cuboid
12 13 14 |
# File 'lib/physical/cuboid.rb', line 12 def id @id end |
#length ⇒ Measured::Length (readonly)
The length of this cuboid
20 21 22 |
# File 'lib/physical/cuboid.rb', line 20 def length @length end |
#properties ⇒ Hash (readonly)
Additional custom properties for this cuboid
36 37 38 |
# File 'lib/physical/cuboid.rb', line 36 def properties @properties end |
#weight ⇒ Measured::Weight (readonly)
The weight of the cuboid itself (excluding what's inside)
32 33 34 |
# File 'lib/physical/cuboid.rb', line 32 def weight @weight end |
#width ⇒ Measured::Length (readonly)
The width of this cuboid
24 25 26 |
# File 'lib/physical/cuboid.rb', line 24 def width @width end |
Instance Method Details
#==(other) ⇒ Boolean
Returns true if the given object shares the same class and ID with this cuboid.
69 70 71 72 |
# File 'lib/physical/cuboid.rb', line 69 def ==(other) other.is_a?(self.class) && id == other&.id end |
#density ⇒ Measured::Density
Calculates and returns this cuboid's density based on its volume and weight.
59 60 61 62 63 64 |
# File 'lib/physical/cuboid.rb', line 59 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 ⇒ Measured::Volume
Calculates and returns this cuboid's volume based on its dimensions.
53 54 55 |
# File 'lib/physical/cuboid.rb', line 53 def volume Measured::Volume(dimensions.map { |d| d.convert_to(:cm).value }.reduce(1, &:*), :ml) end |