radix 2.2.0 → 2.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.index +9 -12
- data/DEMO.md +433 -414
- data/HISTORY.md +17 -0
- data/README.md +40 -20
- data/demo/issues/012_array_base.md +13 -0
- data/lib/radix.yml +9 -12
- data/lib/radix/float.rb +4 -4
- data/lib/radix/integer.rb +3 -4
- data/lib/radix/numeric.rb +3 -3
- data/lib/radix/operator.rb +14 -1
- data/lib/radix/rational.rb +1 -1
- metadata +19 -26
data/HISTORY.md
CHANGED
@@ -1,5 +1,22 @@
|
|
1
1
|
# RELEASE HISTORY
|
2
2
|
|
3
|
+
## 2.2.1 / 2014-03-02
|
4
|
+
|
5
|
+
This release fixes a couple of issues. One unsettling issue in particular,
|
6
|
+
somewhere around Ruby 2.0, `String#b` became an offical core method
|
7
|
+
that force-encodes a string to ASCII 8-bit. This, of course, broke Radix :(
|
8
|
+
That aside, it seems like a rather weak name for such a method. `String#ascii`
|
9
|
+
seems a much better choice (IOHO). Thankfully, there's just enough wiggle-room
|
10
|
+
to implement a work around, so the Radix API remains the same. In addition,
|
11
|
+
there was a bug when trying to decode custom character sets, which has been
|
12
|
+
fixed.
|
13
|
+
|
14
|
+
Changes:
|
15
|
+
|
16
|
+
* Work around Ruby's `String#b` method.
|
17
|
+
* Fix custom base decoding.
|
18
|
+
|
19
|
+
|
3
20
|
## 2.2.0 / 2013-03-20
|
4
21
|
|
5
22
|
Good documentation is so under-addressed by most developers that we really
|
data/README.md
CHANGED
@@ -1,23 +1,26 @@
|
|
1
1
|
# Radix
|
2
2
|
|
3
|
-
[
|
4
|
-
|
5
|
-
[
|
6
|
-
[![
|
7
|
-
[![
|
3
|
+
[![Gem Version](http://img.shields.io/gem/v/radix.svg?style=flat)](http://rubygems.org/gem/radix)
|
4
|
+
|
5
|
+
[![Fork Me](http://img.shields.io/badge/scm-github-blue.svg?style=flat)](http://github.com/rubyworks/radix)
|
6
|
+
[![Report Issue](http://img.shields.io/github/issues/rubyworks/radix.svg?style=flat)](http://github.com/rubyworks/radix/issues)
|
7
|
+
[![Build Status](http://img.shields.io/travis/rubyworks/radix.svg?style=flat)](http://travis-ci.org/rubyworks/radix)
|
8
|
+
|
9
|
+
[![Gittip](http://img.shields.io/badge/gittip-$1/wk-green.svg?style=flat)](https://www.gittip.com/on/github/rubyworks/)
|
8
10
|
[![Flattr Me](http://api.flattr.com/button/flattr-badge-large.png)](http://flattr.com/thing/324911/Rubyworks-Ruby-Development-Fund)
|
9
11
|
|
10
12
|
|
11
|
-
Radix is a very easy to use Ruby library
|
12
|
-
any base. It supports
|
13
|
-
representational string-notations that need not be
|
13
|
+
<b>[Radix]((http://rubyworks.github.com/radix)) is a very easy to use Ruby library
|
14
|
+
for converting numbers to and from any base. It supports Integer, Float and
|
15
|
+
Rational numbers, as well as representational string-notations that need not be
|
16
|
+
in ASCII order.</b>
|
14
17
|
|
15
18
|
|
16
19
|
## Features
|
17
20
|
|
18
21
|
* Convert to and from any base.
|
19
|
-
* Convert
|
20
|
-
* Define custom
|
22
|
+
* Convert Integer, Float and Rational numbers.
|
23
|
+
* Define custom encodings and character sets.
|
21
24
|
* Can be used to encode/decode bytecode strings.
|
22
25
|
* Very intuitive API.
|
23
26
|
|
@@ -26,49 +29,66 @@ representational string-notations that need not be in ASCII order.
|
|
26
29
|
|
27
30
|
Base conversions with ASCII ordered notations are easy in Ruby.
|
28
31
|
|
29
|
-
|
32
|
+
```ruby
|
33
|
+
255.to_s(16) #=> "FF"
|
30
34
|
|
31
|
-
|
35
|
+
"FF".to_i(16) #=> 255
|
36
|
+
```
|
32
37
|
|
33
38
|
But Ruby reaches it's limit at base 36.
|
34
39
|
|
35
|
-
|
40
|
+
```ruby
|
41
|
+
255.to_s(37) #=> Error
|
42
|
+
```
|
36
43
|
|
37
44
|
Radix provides the means of converting to and from any base.
|
38
45
|
|
39
46
|
For example, a number in base 256 can be represented by the array `[100, 10]`
|
40
47
|
(`100**256 + 10**1`) and can be convert to base 10.
|
41
48
|
|
42
|
-
|
49
|
+
```ruby
|
50
|
+
[100,10].b(256).to_a(10) #=> [2,5,6,1,0]
|
51
|
+
```
|
43
52
|
|
44
53
|
Or, to get a string representation for any base up to 62.
|
45
54
|
|
46
|
-
|
55
|
+
```ruby
|
56
|
+
[100,10].b(256).to_s(10) #=> "25610"
|
57
|
+
```
|
47
58
|
|
48
59
|
A string representation of a number can be converted too, again,
|
49
60
|
up to base 62.
|
50
61
|
|
51
|
-
|
62
|
+
```ruby
|
63
|
+
"10".b(62).to_s(10) #=> "62"
|
64
|
+
```
|
52
65
|
|
53
66
|
To use a custom character set, use an array of characters as the base
|
54
67
|
rather than an integer. For example we can convert a base 10 number
|
55
68
|
to another base 10 number using a different encoding.
|
56
69
|
|
57
|
-
|
70
|
+
```ruby
|
71
|
+
base = [:Q, :W, :E, :R, :T, :Y, :U, :I, :O, :U]
|
58
72
|
|
59
|
-
|
73
|
+
"10".b(10).to_a(base) #=> [:W, :Q]
|
74
|
+
```
|
60
75
|
|
61
76
|
To learn more have a look at the [QED Demo](http://rubydoc.info/gems/radix/file/DEMO.md).
|
62
77
|
|
63
78
|
|
64
79
|
## Installing
|
65
80
|
|
81
|
+
If using Bundler, then add the ususal gem entry to your project's Gemfile.
|
82
|
+
|
83
|
+
gem 'radix'
|
84
|
+
|
66
85
|
To install with RubyGems simply open a console and type:
|
67
86
|
|
68
87
|
$ gem install radix
|
69
88
|
|
70
89
|
Radix follows [Ruby Setup](http://rubyworks.github.com/setup) package standard
|
71
|
-
so it can also be installed in an FHS compliant manner using setup.rb
|
90
|
+
so it can also be installed in an FHS compliant manner using setup.rb (very
|
91
|
+
old-school and no longer recommeded).
|
72
92
|
|
73
93
|
|
74
94
|
## Special Thanks
|
@@ -80,7 +100,7 @@ so your contribution is greatly appreciated. Thank you, Douglas!
|
|
80
100
|
|
81
101
|
## Copyrights
|
82
102
|
|
83
|
-
Copyright (c) 2009 Rubyworks
|
103
|
+
Copyright (c) 2009 [Rubyworks](https://rubyworks.github.io)
|
84
104
|
|
85
105
|
This program is distributable in accordance with the *BSD-2-Clause* license.
|
86
106
|
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# Encoding/decoding with custom character sets (#12)
|
2
|
+
|
3
|
+
There is not problem converting a numeric value into a custom base.
|
4
|
+
|
5
|
+
c62 = (0..9).map{ |x| x.to_s } + ('a'..'z').to_a + ('A'..'Z').to_a
|
6
|
+
|
7
|
+
809145531659995.b(c62).to_s #=> "3HLszsQsP"
|
8
|
+
|
9
|
+
But there was a problem with decoding custom number back.
|
10
|
+
|
11
|
+
"3HLszsQsP".b(c62).to_i #=> 809145531659995
|
12
|
+
|
13
|
+
|
data/lib/radix.yml
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
revision: 2013
|
3
3
|
type: ruby
|
4
4
|
sources:
|
5
|
-
-
|
5
|
+
- index
|
6
6
|
authors:
|
7
7
|
- name: Thomas Sawyer
|
8
8
|
email: transfire@gmail.com
|
@@ -29,9 +29,6 @@ resources:
|
|
29
29
|
- type: code
|
30
30
|
uri: http://github.com/rubyworks/radix
|
31
31
|
label: Source Code
|
32
|
-
- type: mail
|
33
|
-
uri: http://groups.google.com/groups/rubyworks-mailinglist
|
34
|
-
label: Mailing List
|
35
32
|
- type: bugs
|
36
33
|
uri: http://github.com/rubyworks/radix/issues
|
37
34
|
label: Issue Tracker
|
@@ -48,12 +45,12 @@ customs: []
|
|
48
45
|
paths:
|
49
46
|
lib:
|
50
47
|
- lib
|
51
|
-
created: '2009-07-01'
|
52
|
-
summary: Convert to and from any base.
|
53
|
-
title: Radix
|
54
|
-
version: 2.2.0
|
55
48
|
name: radix
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
49
|
+
title: Radix
|
50
|
+
summary: Convert to and from any base.
|
51
|
+
created: '2009-07-01'
|
52
|
+
description: "Radix is a very easy to use Ruby library for converting numbers to and
|
53
|
+
from\nany base. It supports both Integer, Float and Rational numbers, as well as
|
54
|
+
\nrepresentational string-notations that need not be in ASCII order."
|
55
|
+
version: 2.2.1
|
56
|
+
date: '2015-03-02'
|
data/lib/radix/float.rb
CHANGED
@@ -49,8 +49,8 @@ module Radix
|
|
49
49
|
#
|
50
50
|
# @return [void]
|
51
51
|
def initialize(value, base=10)
|
52
|
-
@value = parse_value(value, base)
|
53
52
|
@base, @code = parse_base(base)
|
53
|
+
@value = parse_value(value, @base)
|
54
54
|
end
|
55
55
|
|
56
56
|
##
|
@@ -77,10 +77,10 @@ module Radix
|
|
77
77
|
end
|
78
78
|
end
|
79
79
|
|
80
|
-
|
80
|
+
public
|
81
81
|
|
82
82
|
##
|
83
|
-
#
|
83
|
+
# Convert the Radix::Float a Ruby Integer.
|
84
84
|
#
|
85
85
|
# @return [Integer] Base(10) value as Integer.
|
86
86
|
def to_i
|
@@ -90,7 +90,7 @@ module Radix
|
|
90
90
|
alias_method :to_int, :to_i
|
91
91
|
|
92
92
|
##
|
93
|
-
#
|
93
|
+
# Convert Radix::Float to a Ruby float.
|
94
94
|
#
|
95
95
|
# @return [Float] Base(10) value as Float.
|
96
96
|
def to_f
|
data/lib/radix/integer.rb
CHANGED
@@ -32,7 +32,7 @@ module Radix
|
|
32
32
|
# @return [Array<String>, nil] Substitution chars or nil if default.
|
33
33
|
attr :code
|
34
34
|
|
35
|
-
|
35
|
+
private
|
36
36
|
|
37
37
|
##
|
38
38
|
# Starts a new instance of the Radix::Integer class
|
@@ -45,8 +45,8 @@ module Radix
|
|
45
45
|
#
|
46
46
|
# @return [void]
|
47
47
|
def initialize(value, base=10)
|
48
|
-
@value = parse_value(value, base)
|
49
48
|
@base, @code = parse_base(base)
|
49
|
+
@value = parse_value(value, @base)
|
50
50
|
end
|
51
51
|
|
52
52
|
##
|
@@ -94,8 +94,7 @@ module Radix
|
|
94
94
|
|
95
95
|
## digits << #Radix.convert(d, base, 10).to_i
|
96
96
|
|
97
|
-
|
98
|
-
|
97
|
+
public
|
99
98
|
|
100
99
|
##
|
101
100
|
# Makes this Radix::Integer a ruby integer.
|
data/lib/radix/numeric.rb
CHANGED
@@ -68,7 +68,7 @@ module Radix
|
|
68
68
|
operation(:/, other)
|
69
69
|
end
|
70
70
|
|
71
|
-
|
71
|
+
private
|
72
72
|
|
73
73
|
##
|
74
74
|
# Parses the value of the base and character set to use.
|
@@ -85,7 +85,7 @@ module Radix
|
|
85
85
|
# 1 - Nil, or Array of characters representing the base values.
|
86
86
|
def parse_base(base)
|
87
87
|
case base
|
88
|
-
|
88
|
+
when Array
|
89
89
|
code = base
|
90
90
|
base = base.size
|
91
91
|
else
|
@@ -192,7 +192,7 @@ module Radix
|
|
192
192
|
end
|
193
193
|
|
194
194
|
##
|
195
|
-
# Decode an encoded array. Defaults to BASE::B62 if
|
195
|
+
# Decode an encoded array. Defaults to BASE::B62 if @code is not set.
|
196
196
|
#
|
197
197
|
# @param [Array<String, Numeric>] digits The encoded characters.
|
198
198
|
#
|
data/lib/radix/operator.rb
CHANGED
@@ -40,6 +40,17 @@ end
|
|
40
40
|
# instances.
|
41
41
|
class ::String
|
42
42
|
|
43
|
+
# Ruby 2.x defines it's own `String#b` method for converting to ASCII 8-bit.
|
44
|
+
# That breaks Radix (of course), but it a terrbile name. `String#ascii` is
|
45
|
+
# much better (duh!). So that's what we are doing.
|
46
|
+
if method_defined?(:b)
|
47
|
+
alias :ascii :b
|
48
|
+
else
|
49
|
+
def ascii
|
50
|
+
force_encoding('ASCII')
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
43
54
|
##
|
44
55
|
# Takes a String and makes it into a Radix::Integer or Radix::Float as given
|
45
56
|
# base. Float is determined by a "." character in string instance
|
@@ -48,7 +59,9 @@ class ::String
|
|
48
59
|
# The desired base.
|
49
60
|
#
|
50
61
|
# @return [Radix::Integer, Radix::Float]
|
51
|
-
def b(base)
|
62
|
+
def b(base=nil)
|
63
|
+
return ascii unless base
|
64
|
+
|
52
65
|
if index('.')
|
53
66
|
Radix::Float.new(self, base)
|
54
67
|
else
|
data/lib/radix/rational.rb
CHANGED
metadata
CHANGED
@@ -1,75 +1,68 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: radix
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.2.
|
5
|
-
prerelease:
|
4
|
+
version: 2.2.1
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Thomas Sawyer
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2015-03-02 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: ergo
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - '>='
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - '>='
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '0'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: qed
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- -
|
31
|
+
- - '>='
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: '0'
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- -
|
38
|
+
- - '>='
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: '0'
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: ae
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
|
-
- -
|
45
|
+
- - '>='
|
52
46
|
- !ruby/object:Gem::Version
|
53
47
|
version: '0'
|
54
48
|
type: :development
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
|
-
- -
|
52
|
+
- - '>='
|
60
53
|
- !ruby/object:Gem::Version
|
61
54
|
version: '0'
|
62
|
-
description:
|
63
|
-
|
64
|
-
|
55
|
+
description: "Radix is a very easy to use Ruby library for converting numbers to and
|
56
|
+
from\nany base. It supports both Integer, Float and Rational numbers, as well as
|
57
|
+
\nrepresentational string-notations that need not be in ASCII order."
|
65
58
|
email:
|
66
59
|
- transfire@gmail.com
|
67
60
|
executables: []
|
68
61
|
extensions: []
|
69
62
|
extra_rdoc_files:
|
70
63
|
- LICENSE.txt
|
71
|
-
- HISTORY.md
|
72
64
|
- README.md
|
65
|
+
- HISTORY.md
|
73
66
|
- DEMO.md
|
74
67
|
files:
|
75
68
|
- .index
|
@@ -83,6 +76,7 @@ files:
|
|
83
76
|
- demo/applique/check.rb
|
84
77
|
- demo/applique/radix.rb
|
85
78
|
- demo/issues/004_zero_empty_string.md
|
79
|
+
- demo/issues/012_array_base.md
|
86
80
|
- lib/radix/base.rb
|
87
81
|
- lib/radix/float.rb
|
88
82
|
- lib/radix/integer.rb
|
@@ -91,34 +85,33 @@ files:
|
|
91
85
|
- lib/radix/rational.rb
|
92
86
|
- lib/radix.rb
|
93
87
|
- lib/radix.yml
|
94
|
-
- HISTORY.md
|
95
88
|
- README.md
|
89
|
+
- HISTORY.md
|
96
90
|
- DEMO.md
|
97
91
|
- LICENSE.txt
|
98
92
|
homepage: http://rubyworks.github.com/radix
|
99
93
|
licenses:
|
100
94
|
- BSD-2-Clause
|
95
|
+
metadata: {}
|
101
96
|
post_install_message:
|
102
97
|
rdoc_options: []
|
103
98
|
require_paths:
|
104
99
|
- lib
|
105
100
|
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
-
none: false
|
107
101
|
requirements:
|
108
|
-
- -
|
102
|
+
- - '>='
|
109
103
|
- !ruby/object:Gem::Version
|
110
104
|
version: '0'
|
111
105
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
112
|
-
none: false
|
113
106
|
requirements:
|
114
|
-
- -
|
107
|
+
- - '>='
|
115
108
|
- !ruby/object:Gem::Version
|
116
109
|
version: '0'
|
117
110
|
requirements: []
|
118
111
|
rubyforge_project:
|
119
|
-
rubygems_version:
|
112
|
+
rubygems_version: 2.0.3
|
120
113
|
signing_key:
|
121
|
-
specification_version:
|
114
|
+
specification_version: 4
|
122
115
|
summary: Convert to and from any base.
|
123
116
|
test_files: []
|
124
117
|
has_rdoc:
|