YAB62 (Yet Another Base 62 Convertor)

The idea of this gem is simple: I want to write a url shortener with base62. Ruby doesn’t support it natively. I checked out other gems: [radix62], [base62] and [alphadecimal].

They are all written in Ruby. Then, I have this idea to write this gem in C extension. Hence, YAB62 is born.

Btw, most of the tests are borrowed from [alphadecimal].

How to use

Install it with ‘gem install yab62`.

“‘ruby require ’yab62’

1.encode62 # => “1” “1”.decode62 # => 1

# Or, you can do this: YAB62.encode62(1) # => “1” YAB62.decode62(“1”) # => 1 “‘

Benchmark

Check out the benchmark here:

“‘ruby # gem alphadecimal time = Benchmark.measure do

1_000_000.times do |i|
  encode = i.alphadecimal
  decode = encode.alphadecimal
  raise "Assertion error!" unless i == decode
end

end

puts time # 17.060000 0.050000 17.110000 ( 17.507562)

# gem base62 time = Benchmark.measure do

1_000_000.times do |i|
  encode = i.base62_encode
  decode = encode.base62_decode
  raise "Assertion error!" unless i == decode
end

end

puts time # 9.600000 0.020000 9.620000 ( 9.802189)

# gem radix62 time = Benchmark.measure do

1_000_000.times do |i|
  encode = i.encode62
  decode = encode.decode62
  raise "Assertion error!" unless i == decode
end

end

puts time # 19.070000 0.040000 19.110000 ( 19.483596)

# gem yab62 time = Benchmark.measure do

1_000_000.times do |i|
  encode = i.encode62
  decode = encode.decode62
  raise "Assertion error!" unless i == decode
end

end

puts time # 0.550000 0.000000 0.550000 ( 0.605562) “‘

Check out the full script [here].

LICENSE

(The MIT License)

Copyright © 2011 Teng Siong Ong

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

[1]: github.com/k33l0r/radix62 [2]: github.com/jtzemp/base62 [3]: github.com/JackDanger/alphadecimal [4]: gist.github.com/1369278