Skip to content

Commit 752a9e9

Browse files
committed
Update README and configuration for Ruby 3.2 compatibility; enhance unit methods with keyword arguments
1 parent c153243 commit 752a9e9

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ manipulations to ensure an accurate result.
2525

2626
## Installation
2727

28+
This package requires Ruby 3.2 or later.
29+
2830
This package may be installed using:
2931

3032
```bash
@@ -52,6 +54,7 @@ unit = "1 mm".to_unit # convert string object
5254
unit = object.to_unit # convert any object using object.to_s
5355
unit = Unit.new('1/4 cup') # Rational number
5456
unit = Unit.new('1+1i mm') # Complex Number
57+
unit = Unit.new(scalar: 1.5, numerator: ["<meter>"], denominator: ["<second>"]) # keyword arguments
5558
```
5659

5760
### Rules

lib/ruby_units/configuration.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ class Configuration
4646
#
4747
# @param separator [Boolean] whether to include a space between the scalar and the unit (default: true)
4848
# @param format [Symbol] the format to use when generating output (default: :rational)
49-
# @param ** [Hash] additional keyword arguments (ignored, for forward compatibility)
49+
# @param **_options [Hash] additional keyword arguments (ignored, for forward compatibility)
5050
# @return [Configuration] a new configuration instance
51-
def initialize(separator: true, format: :rational, **)
51+
def initialize(separator: true, format: :rational, **_options)
5252
self.separator = separator
5353
self.format = format
5454
end

lib/ruby_units/unit.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ def initialize(*options)
544544
@scalar = date.ajd
545545
@numerator = ["<day>"]
546546
@denominator = UNITY_ARRAY
547-
in /^\s*$/ => empty
547+
in /^\s*$/ => _empty
548548
raise ArgumentError, "No Unit Specified"
549549
in String => str
550550
parse(str)
@@ -1341,13 +1341,18 @@ def abs
13411341
end
13421342

13431343
# ceil of a unit
1344+
# Forwards all arguments to the scalar's ceil method
1345+
# @param args [Array] arguments to pass to the scalar's ceil method (e.g., number of decimal digits)
13441346
# @return [Numeric,Unit]
13451347
def ceil(...)
13461348
return @scalar.ceil(...) if unitless?
13471349

13481350
self.class.new(scalar: @scalar.ceil(...), numerator: @numerator, denominator: @denominator)
13491351
end
13501352

1353+
# Floor of a unit
1354+
# Forwards all arguments to the scalar's floor method
1355+
# @param args [Array] arguments to pass to the scalar's floor method (e.g., number of decimal digits)
13511356
# @return [Numeric,Unit]
13521357
def floor(...)
13531358
return @scalar.floor(...) if unitless?
@@ -1360,17 +1365,22 @@ def floor(...)
13601365
# Rational, etc..). Because unit conversions can often result in Rational
13611366
# scalars (to preserve precision), it may be advisable to use +to_s+ to
13621367
# format output instead of using +round+.
1368+
# Forwards all arguments to the scalar's round method
13631369
# @example
13641370
# RubyUnits::Unit.new('21870 mm/min').convert_to('m/min').round(1) #=> 2187/100 m/min
13651371
# RubyUnits::Unit.new('21870 mm/min').convert_to('m/min').to_s('%0.1f') #=> 21.9 m/min
13661372
#
1373+
# @param args [Array] arguments to pass to the scalar's round method (e.g., number of decimal digits, mode)
13671374
# @return [Numeric,Unit]
13681375
def round(...)
13691376
return @scalar.round(...) if unitless?
13701377

13711378
self.class.new(scalar: @scalar.round(...), numerator: @numerator, denominator: @denominator)
13721379
end
13731380

1381+
# Truncate the unit according to the scalar's truncate method
1382+
# Forwards all arguments to the scalar's truncate method
1383+
# @param args [Array] arguments to pass to the scalar's truncate method (e.g., number of decimal digits)
13741384
# @return [Numeric, Unit]
13751385
def truncate(...)
13761386
return @scalar.truncate(...) if unitless?

0 commit comments

Comments
 (0)