Skip to content

Commit 041a8b4

Browse files
committed
Refactor return_scalar_or_unit method to simplify parameter usage and improve clarity in unary operations
1 parent e861f45 commit 041a8b4

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

lib/ruby_units/unit.rb

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,30 +1321,30 @@ def units(with_prefix: true, format: nil)
13211321
# @return [Numeric,Unit]
13221322
def -@
13231323
neg_scalar = -@scalar
1324-
return_scalar_or_unit(neg_scalar, neg_scalar)
1324+
return_scalar_or_unit(neg_scalar)
13251325
end
13261326

13271327
# absolute value of a unit
13281328
# @return [Numeric,Unit]
13291329
def abs
13301330
abs_scalar = @scalar.abs
1331-
return_scalar_or_unit(abs_scalar, abs_scalar)
1331+
return_scalar_or_unit(abs_scalar)
13321332
end
13331333

13341334
# ceil of a unit
13351335
# Forwards all arguments to the scalar's ceil method
13361336
# @return [Numeric,Unit]
13371337
def ceil(...)
13381338
ceiled_scalar = @scalar.ceil(...)
1339-
return_scalar_or_unit(ceiled_scalar, ceiled_scalar)
1339+
return_scalar_or_unit(ceiled_scalar)
13401340
end
13411341

13421342
# Floor of a unit
13431343
# Forwards all arguments to the scalar's floor method
13441344
# @return [Numeric,Unit]
13451345
def floor(...)
13461346
floored_scalar = @scalar.floor(...)
1347-
return_scalar_or_unit(floored_scalar, floored_scalar)
1347+
return_scalar_or_unit(floored_scalar)
13481348
end
13491349

13501350
# Round the unit according to the rules of the scalar's class. Call this
@@ -1360,15 +1360,15 @@ def floor(...)
13601360
# @return [Numeric,Unit]
13611361
def round(...)
13621362
rounded_scalar = @scalar.round(...)
1363-
return_scalar_or_unit(rounded_scalar, rounded_scalar)
1363+
return_scalar_or_unit(rounded_scalar)
13641364
end
13651365

13661366
# Truncate the unit according to the scalar's truncate method
13671367
# Forwards all arguments to the scalar's truncate method
13681368
# @return [Numeric, Unit]
13691369
def truncate(...)
13701370
truncated_scalar = @scalar.truncate(...)
1371-
return_scalar_or_unit(truncated_scalar, truncated_scalar)
1371+
return_scalar_or_unit(truncated_scalar)
13721372
end
13731373

13741374
# Returns next unit in a range. Increments the scalar by 1.
@@ -1787,11 +1787,10 @@ def return_scalar_or_raise(method, type)
17871787

17881788
# Return the scalar if unitless, otherwise return a new unit with the modified scalar
17891789
# This helper method is used by unary operations like #-@, #abs, #ceil, #floor, #round, #truncate
1790-
# @param scalar_value [Numeric] the scalar value to return if unitless
1791-
# @param new_scalar [Numeric] the new scalar value for the unit if not unitless
1790+
# @param new_scalar [Numeric] the new scalar value
17921791
# @return [Numeric, Unit] the scalar if unitless, or a new unit with the modified scalar
1793-
def return_scalar_or_unit(scalar_value, new_scalar)
1794-
return scalar_value if unitless?
1792+
def return_scalar_or_unit(new_scalar)
1793+
return new_scalar if unitless?
17951794

17961795
with_new_scalar(new_scalar)
17971796
end

0 commit comments

Comments
 (0)