ToCardinal
Ruby gem which will allow to convert from ordinal numbers to cardinal numbers. It only supports ordinals from 1 to 100.
Installation
Add this line to your application's Gemfile:
gem 'to_cardinal'
And then execute:
$ bundle
Or install it yourself as:
$ gem install to_cardinal
Usage
Please use which ever is most comfortable:
You can use ToCardinal without include it in a class.
Just require to_cardinal
into your Ruby code and use cardinalize
method from Module
require 'to_cardinal'
# some ordinal numbers to cardinals
ToCardinal.cardinalize 'first' # => 1
ToCardinal.cardinalize '1st' # => 1
ToCardinal.cardinalize 'fourth' # => 4
ToCardinal.cardinalize '4th' # => 4
You can include ToCardinal in String Ruby’s core library.
Just require to_cardinal/core_ext/string
into your Ruby code and use String#to_cardinal
method.
require 'to_cardinal/core_ext/string'
# some ordinal numbers to cardinals
'first'.to_cardinal # => 1
'1st'.to_cardinal # => 1
'fourth'.to_cardinal # => 4
'4th'.to_cardinal # => 4
You can include ToCardinal in a String's sub-class.
Just require to_cardinal
into your Ruby code and then include
it in your sub-class
require 'to_cardinal'
class A < String
include ToCardinal
...
end
# some ordinal numbers to cardinals
A.new('first').to_cardinal # => 1
A.new('1st').to_cardinal # => 1
A.new('fourth').to_cardinal # => 4
A.new('4th').to_cardinal # => 4
If you are in Ruby on Rails and you want to use ToCardinal like String#to_cardinal
. You can create an initializer
file like this:
# your_project/config/initializers/to_cardinal.rb
require 'to_cardinal/core_ext/string'
# And then you use it
'first'.to_cardinal # => 1
'1st'.to_cardinal # => 1
'fourth'.to_cardinal # => 4
'4th'.to_cardinal # => 4
Contributing
- Fork it ( https://github.com/[my-github-username]/to_cardinal/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
License
Distributed under the MIT license
First of all, thank ruby community for your help! 👊