Skip to content

Commit 089922f

Browse files
authored
Update documentation and fix typos (#376)
* Update documentation for unit methods * Fix typo in format description in string_spec.rb
1 parent 62f2058 commit 089922f

File tree

4 files changed

+55
-20
lines changed

4 files changed

+55
-20
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,15 @@ Unit#*() # Multiply.
8181
Unit#/() # Divide.
8282
Unit#**() # Exponentiate. Exponent must be an integer, can be positive, negative, or zero
8383
Unit#inverse # Returns 1/unit
84-
Unit#abs # Returns absolute value of the unit quantity. Strips off the units
84+
Unit#abs # Returns absolute value of the unit quantity. Units are preserved
8585
Unit#ceil # rounds quantity to next highest integer
8686
Unit#floor # rounds quantity down to next lower integer
8787
Unit#round # rounds quantity to nearest integer
88+
Unit#truncate # truncates quantity to an integer
8889
Unit#to_int # returns the quantity as an integer
90+
Unit#divmod # divide and return quotient and remainder
91+
Unit#remainder# returns remainder of division
92+
Unit#quo # returns quotient as a float or complex
8993
```
9094

9195
Unit will coerce other objects into a Unit if used in a formula. This means...
@@ -260,6 +264,8 @@ end
260264
2. `base_scalar` will return the scalar in base units (SI)
261265
3. `units` will return the name of the units (without the scalar)
262266
4. `base` will return the unit converted to base units (SI)
267+
5. `best_prefix` will return a new unit scaled with an appropriate prefix for
268+
human readability (e.g., '1000 m' becomes '1 km')
263269

264270
### Storing in a database
265271

lib/ruby_units/date.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ def to_unit(other = nil)
4545
other ? RubyUnits::Unit.new(self).convert_to(other) : RubyUnits::Unit.new(self)
4646
end
4747

48-
# @deprecated
48+
# @deprecated The dump parameter is deprecated and will be removed in a future version.
49+
# @param dump [Boolean] if true, use default inspect; if false, use to_s (deprecated behavior)
50+
# @return [String]
4951
def inspect(dump = false)
5052
dump ? super : to_s
5153
end

lib/ruby_units/unit.rb

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,8 @@ def to_s(target_units = nil, precision: 0.0001, format: RubyUnits.configuration.
727727
end
728728

729729
# Normally pretty prints the unit, but if you really want to see the guts of it, pass ':dump'
730-
# @deprecated
730+
# @deprecated The dump parameter is deprecated. Use the default inspect behavior for debugging.
731+
# @param dump [Symbol, nil] pass :dump to see internal structure (deprecated)
731732
# @return [String]
732733
def inspect(dump = nil)
733734
return super() if dump
@@ -1014,8 +1015,11 @@ def %(other)
10141015
end
10151016
alias modulo %
10161017

1017-
# @param [Object] other
1018-
# @return [Unit]
1018+
# Divide two units and return quotient as a float or complex.
1019+
# Similar to division but ensures floating point result.
1020+
#
1021+
# @param other [Numeric, Unit]
1022+
# @return [Float, Complex, Unit]
10191023
# @raise [ZeroDivisionError] if other is zero
10201024
def quo(other)
10211025
self / other
@@ -1063,8 +1067,10 @@ def **(other)
10631067
end
10641068
end
10651069

1066-
# returns the unit raised to the n-th power
1067-
# @param [Integer] n
1070+
# Raise a unit to a power.
1071+
# Returns the unit raised to the n-th power.
1072+
#
1073+
# @param n [Integer] the exponent (must be an integer)
10681074
# @return [Unit]
10691075
# @raise [ArgumentError] when attempting to raise a temperature to a power
10701076
# @raise [ArgumentError] when n is not an integer
@@ -1080,8 +1086,10 @@ def power(n)
10801086
end
10811087

10821088
# Calculates the n-th root of a unit
1083-
# if n < 0, returns 1/unit^(1/n)
1084-
# @param [Integer] n
1089+
# Returns the nth root of a unit.
1090+
# If n < 0, returns 1/unit^(1/n)
1091+
#
1092+
# @param n [Integer] the root degree (must be an integer, cannot be 0)
10851093
# @return [Unit]
10861094
# @raise [ArgumentError] when attempting to take the root of a temperature
10871095
# @raise [ArgumentError] when n is not an integer
@@ -1267,9 +1275,9 @@ def as_json(*)
12671275

12681276
# Returns the 'unit' part of the Unit object without the scalar
12691277
#
1270-
# @param with_prefix [Boolean] include prefixes in output
1271-
# @param format [Symbol] Set to :exponential to force all units to be displayed in exponential format
1272-
#
1278+
# @param with_prefix [Boolean] include prefixes in output (default: true)
1279+
# @param format [Symbol] Set to :exponential to force exponential notation (e.g., 'm*s^-2'),
1280+
# or :rational for rational notation (e.g., 'm/s^2'). Defaults to configuration setting.
12731281
# @return [String]
12741282
def units(with_prefix: true, format: nil)
12751283
return "" if @numerator == UNITY_ARRAY && @denominator == UNITY_ARRAY
@@ -1367,8 +1375,11 @@ def truncate(*args)
13671375
self.class.new(@scalar.truncate(*args), @numerator, @denominator)
13681376
end
13691377

1370-
# returns next unit in a range. '1 mm'.to_unit.succ #=> '2 mm'.to_unit
1371-
# only works when the scalar is an integer
1378+
# Returns next unit in a range. Increments the scalar by 1.
1379+
# Only works when the scalar is an integer.
1380+
# This is used primarily to make ranges work.
1381+
#
1382+
# @example '1 mm'.to_unit.succ #=> '2 mm'.to_unit
13721383
# @return [Unit]
13731384
# @raise [ArgumentError] when scalar is not equal to an integer
13741385
def succ
@@ -1379,8 +1390,11 @@ def succ
13791390

13801391
alias next succ
13811392

1382-
# returns previous unit in a range. '2 mm'.to_unit.pred #=> '1 mm'.to_unit
1383-
# only works when the scalar is an integer
1393+
# Returns previous unit in a range. Decrements the scalar by 1.
1394+
# Only works when the scalar is an integer.
1395+
# This is used primarily to make ranges work.
1396+
#
1397+
# @example '2 mm'.to_unit.pred #=> '1 mm'.to_unit
13841398
# @return [Unit]
13851399
# @raise [ArgumentError] when scalar is not equal to an integer
13861400
def pred
@@ -1416,13 +1430,22 @@ def zero?
14161430
end
14171431

14181432
# @example '5 min'.to_unit.ago
1419-
# @return [Unit]
1433+
# Returns the time that was this duration ago from now.
1434+
# Alias for #before with default time of Time.now
1435+
#
1436+
# @example '5 min'.to_unit.ago #=> Time 5 minutes ago
1437+
# @return [Time, DateTime]
14201438
def ago
14211439
before
14221440
end
14231441

1424-
# @example '5 min'.before(time)
1425-
# @return [Unit]
1442+
# Returns the time that was this duration before the given time point.
1443+
#
1444+
# @example '5 min'.to_unit.before(time) #=> Time 5 minutes before time
1445+
# @example '5 min'.to_unit.before #=> Time 5 minutes ago
1446+
# @param time_point [Time, Date, DateTime] the reference time (defaults to Time.now)
1447+
# @return [Time, Date, DateTime]
1448+
# @raise [ArgumentError] when time_point is not a Time, Date, or DateTime
14261449
def before(time_point = ::Time.now)
14271450
case time_point
14281451
when Time, Date, DateTime
@@ -1506,6 +1529,10 @@ def coerce(other)
15061529
# * Units containing 'kg' will be returned as is. The prefix in 'kg' makes this an odd case.
15071530
# * It will use `centi` instead of `milli` when the scalar is between 0.01 and 0.001
15081531
#
1532+
# @example
1533+
# Unit.new('1000 m').best_prefix #=> '1 km'.to_unit
1534+
# Unit.new('0.5 m').best_prefix #=> '50 cm'.to_unit
1535+
# Unit.new('1500 W').best_prefix #=> '1.5 kW'.to_unit
15091536
# @return [Unit]
15101537
def best_prefix
15111538
return to_base if scalar.zero?

spec/ruby_units/string_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
specify { expect("1 m".convert_to("ft")).to be_within(RubyUnits::Unit.new("0.01 ft")).of RubyUnits::Unit.new("3.28084 ft") }
99
end
1010

11-
describe "% (format)S" do
11+
describe "% (format)" do
1212
subject(:unit) { RubyUnits::Unit.new("1.23456 m/s^2") }
1313

1414
specify { expect("%0.2f" % 1.23).to eq("1.23") }

0 commit comments

Comments
 (0)