@@ -416,7 +416,6 @@ def self.unit_match_regex
416416
417417 # return a regexp fragment used to match prefixes
418418 # @return [String]
419- # @private
420419 def self . prefix_regex
421420 @prefix_regex ||= prefix_map . keys . sort_by { [ _1 . length , _1 ] } . reverse . join ( "|" )
422421 end
@@ -1553,22 +1552,6 @@ def hash
15531552 # Protected and Private Functions that should only be called from this class
15541553 protected
15551554
1556- # Ensure that this unit is compatible with another unit
1557- # @param [Object] other
1558- # @return [void]
1559- # @raise [ArgumentError] if units are not compatible
1560- def ensure_compatible_with ( other )
1561- raise ArgumentError , "Incompatible Units ('#{ self } ' not compatible with '#{ other } ')" unless compatible_with? ( other )
1562- end
1563-
1564- # Validate that a time_point is a Time, Date, or DateTime
1565- # @param [Object] time_point
1566- # @return [void]
1567- # @raise [ArgumentError] when time_point is not a Time, Date, or DateTime
1568- def validate_time_point ( time_point )
1569- raise ArgumentError , "Must specify a Time, Date, or DateTime" unless time_point . is_a? ( Time ) || time_point . is_a? ( Date ) || time_point . is_a? ( DateTime )
1570- end
1571-
15721555 # figure out what the scalar part of the base unit for this unit is
15731556 # @return [nil]
15741557 def update_base_scalar
@@ -1620,12 +1603,28 @@ def apply_signature_items(vector, items, sign)
16201603 end
16211604 end
16221605
1606+ # Ensure that this unit is compatible with another unit
1607+ # @param [Object] other the unit to check compatibility with
1608+ # @return [void]
1609+ # @raise [ArgumentError] if units are not compatible
1610+ def ensure_compatible_with ( other )
1611+ raise ArgumentError , "Incompatible Units ('#{ self } ' not compatible with '#{ other } ')" unless compatible_with? ( other )
1612+ end
1613+
1614+ # Validate that a time_point is a Time, Date, or DateTime
1615+ # @param [Object] time_point the object to validate
1616+ # @return [void]
1617+ # @raise [ArgumentError] when time_point is not a Time, Date, or DateTime
1618+ def validate_time_point ( time_point )
1619+ raise ArgumentError , "Must specify a Time, Date, or DateTime" unless time_point . is_a? ( Time ) || time_point . is_a? ( Date ) || time_point . is_a? ( DateTime )
1620+ end
1621+
16231622 # Helper methods for power operation
16241623
16251624 # Validate that power operation is allowed
1626- # @param exponent [Numeric] the exponent
1625+ # @param exponent [Numeric] the exponent value
16271626 # @return [void]
1628- # @raise [ArgumentError] if operation is not allowed
1627+ # @raise [ArgumentError] if operation is not allowed (temperature units or non-integer exponent)
16291628 def validate_power_operation ( exponent )
16301629 raise ArgumentError , "Cannot raise a temperature to a power" if temperature?
16311630 raise ArgumentError , "Exponent must an Integer" unless exponent . is_a? ( Integer )
@@ -1714,11 +1713,13 @@ def convert_string_target(target_units, format)
17141713 end
17151714
17161715 # Convert with a format string
1717- # @param format_str [String] the format string
1718- # @param target_unit [String, nil] the target unit
1719- # @param original_target [String] the original target_units string (for strftime fallback)
1720- # @param format [Symbol] the format to use
1721- # @return [String] formatted string
1716+ # Handles formatting of unit conversions with custom format strings or strftime patterns
1717+ # @param format_str [String] the format string (e.g., '%0.2f')
1718+ # @param target_unit [String, nil] the target unit to convert to, nil for no conversion
1719+ # @param original_target [String] the original target_units string for strftime fallback
1720+ # @param format [Symbol] the output format symbol
1721+ # @return [String] the formatted unit string
1722+ # @raise [StandardError] caught and handled by attempting strftime parsing
17221723 def convert_with_format_string ( format_str , target_unit , original_target , format )
17231724 if target_unit # unit specified, need to convert
17241725 convert_to ( target_unit ) . to_s ( format_str , format : format )
@@ -1730,9 +1731,10 @@ def convert_with_format_string(format_str, target_unit, original_target, format)
17301731 ( DateTime . new ( 0 ) + self ) . strftime ( original_target )
17311732 end
17321733
1733- # Format the scalar value
1734- # @param unit_str [String] the unit string
1735- # @return [String] formatted string
1734+ # Format the scalar value with appropriate separator and unit string
1735+ # Handles Complex, Rational, and numeric scalars appropriately
1736+ # @param unit_str [String] the unit string to append
1737+ # @return [String] the formatted scalar with separator and unit string
17361738 def format_scalar ( unit_str )
17371739 separator = RubyUnits . configuration . separator
17381740 is_integer = scalar_is_integer?
@@ -1746,45 +1748,49 @@ def format_scalar(unit_str)
17461748 end . strip
17471749 end
17481750
1749- # Helper to check if scalar is effectively an integer
1750- # @return [Boolean]
1751+ # Check if the scalar is effectively an integer
1752+ # Handles Complex numbers by returning false, and compares the scalar to its integer representation
1753+ # @return [Boolean] true if scalar equals its integer representation, false otherwise
17511754 def scalar_is_integer?
17521755 return false if @scalar . is_a? ( Complex )
17531756
17541757 @scalar == @scalar . to_i
17551758 end
17561759
1757- # Helper to create a new unit with modified scalar but same units
1758- # @param [Numeric] new_scalar
1759- # @return [Unit]
1760+ # Create a new unit with a modified scalar but the same units
1761+ # @param new_scalar [Numeric] the new scalar value for the unit
1762+ # @return [Unit] a new unit with the same numerator and denominator but different scalar
17601763 def with_new_scalar ( new_scalar )
17611764 unit_class . new ( scalar : new_scalar , numerator : @numerator , denominator : @denominator )
17621765 end
17631766
1764- # used by #dup to duplicate a Unit
1765- # @param [Unit] other
1766- # @private
1767+ # Initialize copy: used by #dup to duplicate a Unit
1768+ # Duplicates the numerator and denominator arrays to ensure deep copying
1769+ # @param other [Unit] the unit to copy from
17671770 def initialize_copy ( other )
17681771 @numerator = other . numerator . dup
17691772 @denominator = other . denominator . dup
1773+ super
17701774 end
17711775
1772- # Return scalar if unitless, otherwise raise an error
1773- # @param method [Symbol] method to call on scalar
1774- # @param type [Class] the type being converted to (for error message)
1775- # @param converter [Proc] optional converter proc (defaults to calling method on scalar)
1776- # @return [Numeric]
1777- # @raise [RuntimeError] when not unitless
1776+ # Return the scalar if unitless, otherwise raise an error
1777+ # This helper method is used by conversion methods like #to_f, #to_i, #to_c, #to_r
1778+ # @param method [Symbol] the method to call on the scalar (e.g., :to_f, :to_i)
1779+ # @param type [Class] the type being converted to (used in error message)
1780+ # @param converter [Proc, nil] optional converter proc; if provided, called instead of method
1781+ # @return [Numeric] the scalar converted using the provided method or converter
1782+ # @raise [RuntimeError] when the unit is not unitless
17781783 def return_scalar_or_raise ( method , type , converter = nil )
17791784 raise "Cannot convert '#{ self } ' to #{ type } unless unitless. Use Unit#scalar" unless unitless?
17801785
17811786 converter ? converter . call ( @scalar ) : @scalar . public_send ( method )
17821787 end
17831788
1784- # Return scalar if unitless, otherwise return a new unit with the modified scalar
1789+ # Return the scalar if unitless, otherwise return a new unit with the modified scalar
1790+ # This helper method is used by unary operations like #-@, #abs, #ceil, #floor, #round, #truncate
17851791 # @param scalar_value [Numeric] the scalar value to return if unitless
1786- # @param new_scalar [Numeric] the new scalar for the unit if not unitless
1787- # @return [Numeric,Unit]
1792+ # @param new_scalar [Numeric] the new scalar value for the unit if not unitless
1793+ # @return [Numeric, Unit] the scalar if unitless, or a new unit with the modified scalar
17881794 def return_scalar_or_unit ( scalar_value , new_scalar )
17891795 return scalar_value if unitless?
17901796
0 commit comments