Youyouaidi
Ruby Gem Youyouaidi
offers a UUID class for parsing, validating and converting UUIDs into / from shorter representations.
Installation
Add this line to your application's Gemfile:
gem 'youyouaidi'
And then execute:
$ bundle
Or install it yourself as:
$ gem install youyouaidi
Usage
Initializing UUIDs
uuid_string = '550e8400-e29b-41d4-a716-446655440000' # A valid UUID in string format
uuid_short = '_oGOAbD9fsFFEHWSMal1v' # Same UUID in its short format
uuid = UUID uuid_string # creates new Youyouaidi::UUID object, patches Youyouaidi::UUID.parse uuid_string into kernel.
# => #<Youyouaidi::UUID:0x0000010150bb60 @converter=Youyouaidi::Converter, @uuid="550e8400-e29b-41d4-a716-446655440000">
# Alternatively a short UUID can be passed:
uuid = UUID uuid_short # creates similar Youyouaidi::UUID object
# => #<Youyouaidi::UUID:0x0000010150bb60 @converter=Youyouaidi::Converter, @uuid="550e8400-e29b-41d4-a716-446655440000">
Validity check and conversions
uuid_string = '550e8400-e29b-41d4-a716-446655440000' # A valid UUID in string format
uuid = UUID uuid_string
Youyouaidi::UUID.valid? uuid_string # => true
uuid.to_s # Returns the string representation of the UUID object
# => '550e8400-e29b-41d4-a716-446655440000'
uuid.to_short_s # Returns the short string representation of the UUID object
# => '_oGOAbD9fsFFEHWSMal1v', alias for method: #to_param
Comparing UUIDs
uuid_string = '550e8400-e29b-41d4-a716-446655440000' # A valid UUID in string format
uuid = UUID uuid_string
similar_uuid = UUID uuid_string
other_uuid = UUID 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'
uuid == similar_uuid # Two UUID objects representing same UUID (#=== behaves similar for this)
# => true
uuid == other_uuid # Two UUID objects representing different UUIDs (#=== behaves similar for this)
# => false
uuid == uuid_string # Comparing a UUID object and its string representation with `=='
# => false
uuid === uuid_string # Comparing a UUID object and its string representation with `===' (case insensetive)
# => true
Contributing
- Fork it ( http://github.com/
/youyouaidi/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 new Pull Request