Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5.16.0 #69

Merged
merged 4 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
## [5.15.0](https://github.com/shivam091/unit_measurements/compare/v5.14.0...v5.15.0) - 2023-12-01
## [5.16.0](https://github.com/shivam091/unit_measurements/compare/v5.15.0...v5.16.0) - 2023-12-13

### What's new

- Added planck units and their conversions.

----------

## [5.15.0](https://github.com/shivam091/unit_measurements/compare/v5.14.0...v5.15.0) - 2023-12-11

### What's new

Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
unit_measurements (5.15.0)
unit_measurements (5.16.0)
activesupport (~> 7.0)

GEM
Expand Down
118 changes: 42 additions & 76 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ A library that encapsulate measurements and their units in Ruby.
[![Test Coverage](https://api.codeclimate.com/v1/badges/b8aec9bffa356d108784/test_coverage)](https://codeclimate.com/github/shivam091/unit_measurements/test_coverage)
[![License](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/shivam091/unit_measurements/blob/main/LICENSE.md)

[Harshal V. Ladhe, Master of Computer Science.](https://shivam091.github.io)
**[Harshal V. Ladhe, Master of Computer Science.](https://shivam091.github.io)**

## Introduction

Expand Down Expand Up @@ -47,8 +47,7 @@ Users are advised to cross-verify conversions for their specific use cases._

## Installation

To use `unit_measurements` in your Rails application, add the
following line to your Gemfile:
To use `unit_measurements` in your Rails application, add the following line to your Gemfile:

```ruby
gem "unit_measurements"
Expand Down Expand Up @@ -88,8 +87,7 @@ viz., `UnitMeasurements::Weight`, `UnitMeasurements::Length`, etc.
**Initialize a measurement:**

```ruby
UnitMeasurements::Length.new(1, "km")
#=> 1 km
UnitMeasurements::Length.new(1, "km") #=> 1 km
```

**Converting to other units:**
Expand All @@ -103,22 +101,19 @@ These methods provide `use_cache` parameter which defaults to `false` to indicat
You can use `#convert_to` as:

```ruby
UnitMeasurements::Length.new(1, "km").convert_to("m", use_cache: true)
#=> 1000.0 m
UnitMeasurements::Length.new(1, "km").convert_to("m", use_cache: true) #=> 1000.0 m
```

If you want to modify measurement object itself, you can use `#convert_to!` method as:

```ruby
UnitMeasurements::Length.new(1, "km").convert_to!("m")
#=> 1000.0 m
UnitMeasurements::Length.new(1, "km").convert_to!("m") #=> 1000.0 m
```

You can convert the measurement directly to the `primitive` unit of the unit group as:

```ruby
UnitMeasurements::Length.new(1, "cm").to_primitive
#=> 0.01 m
UnitMeasurements::Length.new(1, "cm").to_primitive #=> 0.01 m
```

Note: `#to_primitive` method is aliased as `#in_primitive` and `#as_primitive`.
Expand All @@ -128,46 +123,38 @@ Note: `#to_primitive` method is aliased as `#in_primitive` and `#as_primitive`.
This method provides `use_cache` parameter which defaults to `false` to indicate whether the caching of conversion factors should happen.

```ruby
UnitMeasurements::Length.parse("1 km")
#=> 1.0 km
UnitMeasurements::Length.parse("1 km") #=> 1.0 km
```

**Parse string that mentions quantity, source unit, and target unit:**

A source unit can be separated from the target unit using the `in`, `to`, or `as` operators.

```ruby
UnitMeasurements::Length.parse("1 km to m")
#=> 1000.0 m
UnitMeasurements::Length.parse("1 km to m") #=> 1000.0 m
```

**Parse scientific numbers, source unit, and (or) target unit:**

```ruby
UnitMeasurements::Length.parse("2e+2 km").convert_to("m")
#=> 200000.0 m
UnitMeasurements::Length.parse("2e+2 km to m")
#=> 200000.0 m
UnitMeasurements::Length.parse("2e+2 km").convert_to("m") #=> 200000.0 m
UnitMeasurements::Length.parse("2e+2 km to m") #=> 200000.0 m
```
You can check supported special characters for exponents
[here](https://shivam091.github.io/unit_measurements/UnitMeasurements/Normalizer.html).

**Parse complex numbers, source unit, and (or) target unit:**

```ruby
UnitMeasurements::Length.parse("2+3i km").convert_to("m")
#=> 2000.0+3000.0i m
UnitMeasurements::Length.parse("2+3i km to m")
#=> 2000.0+3000.0i m
UnitMeasurements::Length.parse("2+3i km").convert_to("m") #=> 2000.0+3000.0i m
UnitMeasurements::Length.parse("2+3i km to m") #=> 2000.0+3000.0i m
```

**Parse fractional/mixed fractional numbers, source unit, and (or) target unit:**

```ruby
UnitMeasurements::Length.parse("2 ½ km").convert_to("m")
#=> 2500.0 m
UnitMeasurements::Length.parse("2/3 km to m")
#=> 666.666666666667 m
UnitMeasurements::Length.parse("2 ½ km").convert_to("m") #=> 2500.0 m
UnitMeasurements::Length.parse("2/3 km to m") #=> 666.666666666667 m
```

You can check supported special characters for fractional notations
Expand All @@ -176,17 +163,14 @@ You can check supported special characters for fractional notations
**Parse ratios, source unit, and (or) target unit:**

```ruby
UnitMeasurements::Length.parse("1:2 km").convert_to("m")
#=> 500.0 m
UnitMeasurements::Length.parse("1:2 km to m")
#=> 500.0 m
UnitMeasurements::Length.parse("1:2 km").convert_to("m") #=> 500.0 m
UnitMeasurements::Length.parse("1:2 km to m") #=> 500.0 m
```

**Calculate the ratio between two units:**

```ruby
UnitMeasurements::Length.ratio("in", "ft")
#=> "12.0 in/ft"
UnitMeasurements::Length.ratio("in", "ft") #=> "12.0 in/ft"
```

**Formatting measurement:**
Expand All @@ -195,8 +179,7 @@ If you want to format the measurement to certain format, you can use `#to_fs` me
If format is not specified, it defaults to `"%.2<value>f %<unit>s"`.

```ruby
UnitMeasurements::Length.new(100, "m").to("in").to_fs("%.4<quantity>f %<unit>s")
#=> "3937.0079 in"
UnitMeasurements::Length.new(100, "m").to("in").to_fs("%.4<quantity>f %<unit>s") #=> "3937.0079 in"
```

You can check more about formatting along with their examples
Expand All @@ -206,26 +189,23 @@ You can check more about formatting along with their examples

```ruby
length = UnitMeasurements::Length.new(1, "km")
length.quantity
#=> 1
length.unit
#=> #<UnitMeasurements::Unit: km (kilometer, kilometers, kilometre, kilometres)>
length.quantity #=> 1
length.unit #=> #<UnitMeasurements::Unit: km (kilometer, kilometers, kilometre, kilometres)>
```

Unit object can be interrogated for a range of attributes:

```ruby
length.unit.aliases # Alternative names for the unit.
#=> #<Set: {"kilometer", "kilometers", "kilometre", "kilometres"}>
length.unit.conversion_factor # Conversion factor relative to primitive unit.
#=> 1000.0
# Alternative names for the unit.
length.unit.aliases #=> #<Set: {"kilometer", "kilometers", "kilometre", "kilometres"}>
# Conversion factor relative to primitive unit.
length.unit.conversion_factor #=> 1000.0
```

**See primitive unit of the unit group:**

```ruby
UnitMeasurements::Length.primitive
#=> #<UnitMeasurements::Unit: m (meter, meters, metre, metres)>
UnitMeasurements::Length.primitive #=> #<UnitMeasurements::Unit: m (meter, meters, metre, metres)>
```

**See all units of the unit group:**
Expand All @@ -238,8 +218,7 @@ UnitMeasurements::Length.units
**See names of all valid units of the unit group:**

```ruby
UnitMeasurements::Length.unit_names
#=> ["ft", "in", "m", "mi", "yd"]
UnitMeasurements::Length.unit_names #=> ["ft", "in", "m", "mi", "yd"]
```

**See all valid units of the unit group along with their aliases:**
Expand All @@ -252,8 +231,7 @@ UnitMeasurements::Length.unit_names_with_aliases
**See list of unit systems defined within the unit group:**

```ruby
UnitMeasurements::Length.systems
#=> ["metric", "imperial", "us_customary", "astronomical"]
UnitMeasurements::Length.systems #=> ["metric", "imperial", "us_customary", "astronomical"]
```

**See list of units within the unit system:**
Expand All @@ -273,30 +251,23 @@ You can use `#unit_for` or `#unit_for!` (aliased as `#[]`) methods to find units
within the unit group. `#unit_for!` method returns an error if a unit system is not defined within the unit group.

```ruby
UnitMeasurements::Length.unit_for("m")
#=> #<UnitMeasurements::Unit: m (meter, meters, metre, metres)>
UnitMeasurements::Length.unit_for("z")
#=> nil
UnitMeasurements::Length.unit_for!("z")
#=> Invalid unit: 'z'. (UnitMeasurements::UnitError)
UnitMeasurements::Length.unit_for("m") #=> #<UnitMeasurements::Unit: m (meter, meters, metre, metres)>
UnitMeasurements::Length.unit_for("z") #=> nil
UnitMeasurements::Length.unit_for!("z") #=> Invalid unit: 'z'. (UnitMeasurements::UnitError)
```

**Finding whether the unit is defined within the unit group:**

```ruby
UnitMeasurements::Length.defined?("m")
#=> true
UnitMeasurements::Length.defined?("metre")
#=> false
UnitMeasurements::Length.defined?("m") #=> true
UnitMeasurements::Length.defined?("metre") #=> false
```

**Check if the unit is a valid unit or alias within the unit group:**

```ruby
UnitMeasurements::Length.unit_or_alias?("m")
#=> true
UnitMeasurements::Length.unit_or_alias?("metre")
#=> true
UnitMeasurements::Length.unit_or_alias?("m") #=> true
UnitMeasurements::Length.unit_or_alias?("metre") #=> true
```

**Clear cached data for the unit group:**
Expand All @@ -311,8 +282,7 @@ You have ability to compare the measurements with the same or different units wi
For example, comparing length with length will work, comparing a length with a area would fail.

```ruby
UnitMeasurements::Length.parse("1 km") != UnitMeasurements::Length.parse("1 m")
#=> true
UnitMeasurements::Length.parse("1 km") != UnitMeasurements::Length.parse("1 m") #=> true
```

You can check supported comparisons along with their examples
Expand All @@ -326,10 +296,8 @@ measurement by either other measurement with compatible unit or numeric value.
In cases of different units, the left hand side takes precedence.

```ruby
UnitMeasurements::Length.new(1, "km") + UnitMeasurements::Length.new(1, "m")
#=> 1.001 km
UnitMeasurements::Length.new(2, "km") * 2+2i
#=> 4+2i km
UnitMeasurements::Length.new(1, "km") + UnitMeasurements::Length.new(1, "m") #=> 1.001 km
UnitMeasurements::Length.new(2, "km") * 2+2i #=> 4+2i km
```

You can check supported arithmetic operations along with their examples
Expand All @@ -340,8 +308,7 @@ You can check supported arithmetic operations along with their examples
You can perform mathematical functions on the measurements.

```ruby
UnitMeasurements::Length.new(17.625, "m").round
# => 18 m
UnitMeasurements::Length.new(17.625, "m").round #=> 18 m
```

You can check supported mathematical functions along with their examples
Expand All @@ -353,8 +320,7 @@ You can convert measurement quantity directly to other numeric types viz.
`Integer`, `BigDecimal`, `Rational`, `Complex`, and `Float`.

```ruby
UnitMeasurements::Length.new(2.25567, "km").to_i
#=> 2 km
UnitMeasurements::Length.new(2.25567, "km").to_i #=> 2 km
```

You can check more about them along with their examples
Expand Down Expand Up @@ -500,9 +466,9 @@ This will enable the usage of these units as methods to instantiate and use meas

```ruby
# Initialize a measurement
1.m #=> 1 m
5.feet #=> 5 ft
10.inches #=> 10 in
1.m #=> 1 m
5.feet #=> 5 ft
10.inches #=> 10 in

# Usage
## Equality comparison
Expand Down
4 changes: 4 additions & 0 deletions lib/unit_measurements/unit_groups/acceleration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@
system :centimetre_gram_second do
unit "Gal", value: "1e-2 m/s²", aliases: ["gal", "galileo"]
end

system :planck_units do
unit "aₚ", value: "5.5608e+51 m/s²", aliases: ["planck acceleration", "quantum acceleration"]
end
end
4 changes: 4 additions & 0 deletions lib/unit_measurements/unit_groups/area.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
unit "ch² (US)", value: "404.6873 m²", aliases: ["ch^2 (US)", "sq ch (US)", "square chain (US)", "square chains (US)"]
end

system :planck_units do
unit "Aₚ", value: "2.6121e-70 m²", aliases: ["planck area", "quantum area"]
end

unit "bd", value: "0.00774192 m²", aliases: ["board", "boards"]
unit "ro", value: "1/4 ac", aliases: ["rood"]
end
4 changes: 4 additions & 0 deletions lib/unit_measurements/unit_groups/density.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@
unit "slug/ft³", value: "515.3788184 kg/m³", aliases: ["slug/ft^3", "slug per cubic foot", "slugs per cubic foot"]
unit "slug/in³", value: "890574.582782 kg/m³", aliases: ["slug/in^3", "slug per cubic inch", "slugs per cubic inch"]
end

system :planck_units do
unit "ρₚ", value: "5.1550e+96 kg/m³", aliases: ["planck density", "quantum density"]
end
end
4 changes: 4 additions & 0 deletions lib/unit_measurements/unit_groups/electric_charge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@
unit "abC", value: "10 C", aliases: ["abcoulomb", "abcoulombs"]
unit "statC", value: "3.335641e-10 C", aliases: ["statcoulomb", "statcoulombs"]
end

system :planck_units do
unit "qₚ", value: "1.875545956e-18 C", aliases: ["planck length", "quantum length"]
end
end
4 changes: 4 additions & 0 deletions lib/unit_measurements/unit_groups/electric_current.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@
unit "abA", value: "10 A", aliases: ["abampere", "abamperes"]
unit "statA", value: "3.33564e-10 A", aliases: ["statampere", "statamperes"]
end

system :planck_units do
unit "Iₚ", value: "3.4789e+25 A", aliases: ["planck current", "quantum current"]
end
end
4 changes: 4 additions & 0 deletions lib/unit_measurements/unit_groups/energy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
unit "hp⋅h", value: "2.685e+13 erg", aliases: ["hp*h", "horsepower-hour"]
end

system :planck_units do
unit "Eₚ", value: "1.9561e+9 J", aliases: ["planck energy", "quantum energy"]
end

unit "Ry", value: "2.179872e-18 J", aliases: ["rydberg"]
unit "Ha", value: "4.359744e-18 J", aliases: ["hartree", "atomic unit of energy"]
unit "boe", value: "5.86152 GJ", aliases: ["barrel of oil equivalent", "barrels of oil equivalent"]
Expand Down
4 changes: 4 additions & 0 deletions lib/unit_measurements/unit_groups/force.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@
unit "(lb⋅ft)/s²", value: "0.138254954376 N", aliases: ["(lb*ft)/s^2", "pound foot per second squared"]
unit "(lb⋅in)/s²", value: "0.011521246198 N", aliases: ["(lb*in)/s^2", "pound inch per second squared"]
end

system :planck_units do
unit "Fₚ", value: "1.2103e+44 N", aliases: ["planck force", "quantum force"]
end
end
4 changes: 4 additions & 0 deletions lib/unit_measurements/unit_groups/length.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,8 @@
unit "lm", value: "30 ld", aliases: ["light-month", "light-months"]
unit "ly", value: "365.25 ld", aliases: ["light-year", "light-years"]
end

system :planck_units do
unit "lₚ", value: "1.616255e-35 m", aliases: ["planck length", "quantum length"]
end
end
4 changes: 4 additions & 0 deletions lib/unit_measurements/unit_groups/power.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,9 @@
unit "erg/s", value: "1e-7 W", aliases: ["erg per second", "ergs per second"]
end

system :planck_units do
unit "Pₚ", value: "3.6283e+52 W", aliases: ["planck power", "quantum power"]
end

unit "lusec", value: "133.3224 μW", aliases: ["lusecs"]
end
Loading