# Measurement
[![Build Status](https://github.com/mhuggins/ruby-measurement/actions/workflows/CI.yml/badge.svg)](https://github.com/mhuggins/ruby-measurement/actions/workflows/CI.yml)
[![Code Climate](https://codeclimate.com/github/mhuggins/ruby-measurement.png)](https://codeclimate.com/github/mhuggins/ruby-measurement)
[ruby-measurement](https://github.com/mhuggins/ruby-measurement) is a simple
Ruby gem for calculating and converting units of measure.
## Installation
Add this line to your application's Gemfile:
gem 'ruby-measurement'
And then execute:
$ bundle
Or install it yourself as:
$ gem install ruby-measurement
## Usage
The `Measurement` class is responsible for parsing strings to determine the
quantity entered as well as the unit of measure. This can be done through the
`parse` method, which returns a `Measurement` object.
Measurement.parse('3 feet') # => 3.0 ft.
Measurement.parse('25 fl oz') # => 25.0 fl. oz.
Measurement.parse('12 tonnes') # => 12.0 t
Measurement.parse('25 "') # => 25.0 in.
A measurement includes both a quantity and a unit. Refer to the "Units"
section below for more details on how to interact with a unit.
measurement = Measurement.parse('12 yd') # => 12.0 yd.
measurement.quantity # => 12.0
measurement.unit # => yd.
The `String` and `Numeric` classes have also been extended to simplify the
parsing of measurement values.
'12 yd'.to_measurement # => 12.0 yd.
20.to_measurement # => 20.0 count
### Units
The `Measurement::Unit` class is used to represent the units for a measurement.
Predefined units can be looked up via `Measurement::Unit[]`.
unit = Measurement::Unit[:dozen] # => doz
Similarly, the `String#to_unit` and `Symbol#to_unit` method can be used to
accomplish the same thing.
unit = 'dozen'.to_unit # => doz
unit = :dozen.to_unit # => doz
Units provide access to their names and aliases.
unit.name # => "yd."
unit.aliases # => ["yd.", "yd", "yard", "yards"]
### Converting units
This gem allows you to convert among compatible units as long as a conversion
has been supplied when defining a unit. The default supplied units include
conversions among the same unit types (e.g.: converting feet to yards or meters
to kilometers).
measurement = Measurement.parse('3 feet') # => 3.0 ft.
measurement.convert_to(:yards) # => 1.0 yd.
measurement.convert_to(:in) # => 36.0 in.
measurement.convert_to(:inches) # => 36.0 in.
NOTE: Converting between metric and U.S. customary systems is currently not
included by default. It is intended to be shipped in a future release.
### Calculations
This gem allows you to add, subtract, multiply, or divide measurements that can
be converted into one-another.
measurement1 = Measurement.parse('3 feet') # => 3.0 ft.
measurement2 = Measurement.parse('6 inch') # => 6.0 in.
measurement1 + measurement2 # => 3.5 ft.
Additionally, measurements can have basic math operations performed using basic
numeric values.
measurement = Measurement.parse('2 yards') # => 2.0 yd.
measurement * 6 # => 12.0 yd.
measurement / 5 # => 0.4 yd.
measurement + 3 # => 5.0 yd.
measurement - 1 # => 1.0 yd.
measurement ** 2 # => 4.0 yd.
### Specifying units
By default, ruby-measurement ships with common areas, lengths, volumes, and
weights/masses for the metric and U.S. customary systems. This happens
automatically when requiring the gem in the following manner.
require 'ruby-measurement'
To include just one of these systems of measure, require the gem in per the
following.
require 'ruby-measurement/measurement'
# Metric units/conversions
require 'ruby-measurement/definitions/metric'
# U.S. customary units/conversions
require 'ruby-measurement/definitions/us_customary'
Additionally, specific categories of units of measure can be included per
system.
require 'ruby-measurement/measurement'
# Metric units/conversions
require 'ruby-measurement/definitions/metric/area'
require 'ruby-measurement/definitions/metric/length'
require 'ruby-measurement/definitions/metric/volume'
require 'ruby-measurement/definitions/metric/weight'
# U.S. customary units/conversions
require 'ruby-measurement/definitions/us_customary/area'
require 'ruby-measurement/definitions/us_customary/length'
require 'ruby-measurement/definitions/us_customary/volume'
require 'ruby-measurement/definitions/us_customary/weight'
### Defining custom units & conversions
This gem also enabled you to define units of measure that aren't included by
default.
Measurement.define(:day) do |unit|
unit.alias :days
unit.convert_to(:week) { |value| value / 7.0 }
unit.convert_to(:year) { |value| value / 365.0 }
end
Measurement.define(:wk) do |unit|
unit.alias :week, :weeks
unit.convert_to(:day) { |value| value * 7.0 }
unit.convert_to(:year) { |value| value / 52.0 }
end
Measurement.define(:yr) do |unit|
unit.alias :year, :years
unit.convert_to(:day) { |value| value * 365.0 }
unit.convert_to(:week) { |value| value * 52.0 }
end
Note that the first value passed to `Measurement.define` is the unit format
that will be used when displaying a measurement.
Measurement.parse('3 yr') # => 3.0 yr
Measurement.parse('3 year') # => 3.0 yr
Measurement.parse('3 years') # => 3.0 yr
### List all units names defined
List all keys you can use as unit
Measurement::Unit.names # => ['count','doz','dozen',...]
## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
## License
ruby-measurement is released under the [MIT License](http://www.opensource.org/licenses/MIT).
没有合适的资源?快使用搜索试试~ 我知道了~
用于计算和转换测量值的简单 Ruby gem_Ruby_代码_相关文件_下载
共48个文件
rb:38个
rake:2个
md:2个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 121 浏览量
2022-07-11
22:34:50
上传
评论
收藏 31KB ZIP 举报
温馨提示
ruby-measurement是一个简单的 Ruby gem,用于计算和转换测量单位。 安装 将此行添加到应用程序的 Gemfile: gem 'ruby-measurement' 然后执行: $ bundle 或者自己安装: $ gem install ruby-measurement 用法 该类Measurement负责解析字符串以确定输入的数量和计量单位。这可以通过 parse返回一个Measurement对象的方法来完成。 Measurement.parse('3 feet') # => 3.0 ft. Measurement.parse('25 fl oz') # => 25.0 fl. oz. Measurement.parse('12 tonnes') # => 12.0 t Measurement.parse('25 "') # => 25.0 in. 度量包括数量和单位。 更多详情、使用方法,请下载后阅读README.md文件
资源推荐
资源详情
资源评论
收起资源包目录
ruby-measurement-master.zip (48个子文件)
ruby-measurement-master
ruby-measurement.gemspec 930B
tasks
debug.rake 127B
rspec.rake 135B
Rakefile 132B
.github
workflows
CI.yml 557B
Gemfile 96B
spec
ruby-measurement
unit_spec.rb 4KB
core_ext
string_spec.rb 1KB
symbol_spec.rb 396B
numeric_spec.rb 183B
measurement_spec.rb 12KB
definitions
metric
capacity_spec.rb 996B
weight_spec.rb 8KB
length_spec.rb 6KB
volume_spec.rb 6KB
area_spec.rb 2KB
us_customary
capacity_spec.rb 2KB
weight_spec.rb 3KB
length_spec.rb 6KB
volume_spec.rb 5KB
area_spec.rb 4KB
unit_builder_spec.rb 3KB
spec_helper.rb 86B
.gitignore 160B
CHANGELOG.md 1KB
lib
ruby-measurement.rb 149B
ruby-measurement
measurement.rb 5KB
core_ext.rb 130B
core_ext
numeric.rb 111B
symbol.rb 148B
string.rb 211B
unit.rb 2KB
definitions.rb 98B
version.rb 42B
definitions
metric.rb 265B
metric
area.rb 1KB
volume.rb 3KB
length.rb 3KB
capacity.rb 696B
weight.rb 4KB
us_customary
area.rb 2KB
volume.rb 3KB
length.rb 3KB
capacity.rb 1KB
weight.rb 2KB
us_customary.rb 295B
README.md 6KB
LICENSE.txt 1KB
共 48 条
- 1
资源评论
快撑死的鱼
- 粉丝: 1w+
- 资源: 9156
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功