Skip to content

Commit e861f45

Browse files
committed
Refactor return_scalar_or_raise method to simplify conversion logic and improve readability
1 parent b70585c commit e861f45

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

lib/ruby_units/unit.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,7 +1246,7 @@ def to_f
12461246
# @return [Complex]
12471247
# @raise [RuntimeError] when not unitless
12481248
def to_c
1249-
return_scalar_or_raise(:to_c, Complex, ->(s) { Complex(s) })
1249+
return_scalar_or_raise(:to_c, Complex)
12501250
end
12511251

12521252
# if unitless, returns an int, otherwise raises an error
@@ -1777,13 +1777,12 @@ def initialize_copy(other)
17771777
# This helper method is used by conversion methods like #to_f, #to_i, #to_c, #to_r
17781778
# @param method [Symbol] the method to call on the scalar (e.g., :to_f, :to_i)
17791779
# @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
1780+
# @return [Numeric] the scalar converted using the provided method
17821781
# @raise [RuntimeError] when the unit is not unitless
1783-
def return_scalar_or_raise(method, type, converter = nil)
1782+
def return_scalar_or_raise(method, type)
17841783
raise "Cannot convert '#{self}' to #{type} unless unitless. Use Unit#scalar" unless unitless?
17851784

1786-
converter ? converter.call(@scalar) : @scalar.public_send(method)
1785+
@scalar.public_send(method)
17871786
end
17881787

17891788
# Return the scalar if unitless, otherwise return a new unit with the modified scalar

0 commit comments

Comments
 (0)