No commit activity in last 3 years
No release in over 3 years
Add Integer#to_string_with_unit to easily express a number with singular/plural unit
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.12
~> 10.0
~> 3.0
 Project Readme

StringWithUnit

Gem Version Build Status

Installation

gem 'string_with_unit'

Usage

require 'string_with_unit'

(-2..2).each do |i|
  puts i.to_string_with_unit('gem', 'gems')  # singular unit, plural unit
end
# => "-2 gems"
# => "-1 gems"
# => "0 gems"
# => "1 gem"
# => "2 gems"

# alias: Integer#to_s_with_unit
puts 101.to_s_with_unit('dalmatian', 'dalmatians')   # => "101 dalmatians"

With ActiveSupport::Inflector

require 'string_with_unit'
require 'active_support/inflector'

(-2..2).each do |i|
  puts i.to_string_with_unit('gem')  # Specify singular unit only
end
# => "-2 gems"
# => "-1 gems"
# => "0 gems"
# => "1 gem"
# => "2 gems"

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/meganemura/string_with_unit.

License

The gem is available as open source under the terms of the MIT License.