Skip to content

Commit a39dee1

Browse files
committed
🐛 Fix issue #12 by ensure base is parsed before value.
1 parent fd54bf6 commit a39dee1

5 files changed

Lines changed: 24 additions & 12 deletions

File tree

demo/issues/012_array_base.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+

lib/radix/float.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ class Float < Numeric
4949
#
5050
# @return [void]
5151
def initialize(value, base=10)
52-
@value = parse_value(value, base)
5352
@base, @code = parse_base(base)
53+
@value = parse_value(value, @base)
5454
end
5555

5656
##
@@ -77,10 +77,10 @@ def parse_value(value, base)
7777
end
7878
end
7979

80-
public
80+
public
8181

8282
##
83-
# Makes this Radix::Float a ruby Integer.
83+
# Convert the Radix::Float a Ruby Integer.
8484
#
8585
# @return [Integer] Base(10) value as Integer.
8686
def to_i
@@ -90,7 +90,7 @@ def to_i
9090
alias_method :to_int, :to_i
9191

9292
##
93-
# Makes this Radix::Float a ruby float.
93+
# Convert Radix::Float to a Ruby float.
9494
#
9595
# @return [Float] Base(10) value as Float.
9696
def to_f

lib/radix/integer.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Integer < Numeric
3232
# @return [Array<String>, nil] Substitution chars or nil if default.
3333
attr :code
3434

35-
private
35+
private
3636

3737
##
3838
# Starts a new instance of the Radix::Integer class
@@ -45,8 +45,8 @@ class Integer < Numeric
4545
#
4646
# @return [void]
4747
def initialize(value, base=10)
48-
@value = parse_value(value, base)
4948
@base, @code = parse_base(base)
49+
@value = parse_value(value, @base)
5050
end
5151

5252
##
@@ -94,8 +94,7 @@ def parse_array(value, base)
9494

9595
## digits << #Radix.convert(d, base, 10).to_i
9696

97-
public
98-
97+
public
9998

10099
##
101100
# Makes this Radix::Integer a ruby integer.

lib/radix/numeric.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def /(other)
6868
operation(:/, other)
6969
end
7070

71-
private
71+
private
7272

7373
##
7474
# Parses the value of the base and character set to use.
@@ -85,7 +85,7 @@ def /(other)
8585
# 1 - Nil, or Array of characters representing the base values.
8686
def parse_base(base)
8787
case base
88-
when Array
88+
when Array
8989
code = base
9090
base = base.size
9191
else
@@ -192,7 +192,7 @@ def base_encode(digits)
192192
end
193193

194194
##
195-
# Decode an encoded array. Defaults to BASE::B62 if self.code is not set.
195+
# Decode an encoded array. Defaults to BASE::B62 if @code is not set.
196196
#
197197
# @param [Array<String, Numeric>] digits The encoded characters.
198198
#

lib/radix/rational.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def self.[](n,d=nil,b=10)
4040
# @return [Array<String>, nil] Substitution chars or nil if default.
4141
attr :code
4242

43-
private
43+
private
4444

4545
##
4646
# Create a new Radix::Rational instance.

0 commit comments

Comments
 (0)