baseanything 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +5 -0
- data/README.md +62 -26
- data/bin/console +14 -0
- data/lib/baseanything.rb +106 -64
- data/lib/baseanything/version.rb +3 -0
- metadata +3 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f75f0b2c632857cc016a6cd8598b7386d4dbbe9e
|
|
4
|
+
data.tar.gz: a886f50a7f912d1513e688cb29dae4b1062f4540
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f3984fc1ba766bc5e21182fd23baa97afb83bc382ea2ee45ad76eb4d2b242a8ea715c6c50b567f3adde6e0d50a3d44a9d4005c719358abac74ab086d0b7da850
|
|
7
|
+
data.tar.gz: 036fa150893fdf6233502b40ceaabc08440b497cc853396fc466cbac79a0edf032627a6d513a19ffae814c71c15fd71650adeb51c7384cbf6c7aefe0f314dd0c
|
data/.gitignore
CHANGED
data/README.md
CHANGED
|
@@ -1,7 +1,23 @@
|
|
|
1
1
|
#BaseAnything
|
|
2
2
|
|
|
3
|
-
BaseAnything is a Ruby Gem that allows you to create a number system of any base using any symbols you want.
|
|
3
|
+
BaseAnything is a Ruby Gem that allows you to create a number system of any base using any symbols you want. It also allows you to freely convert to any other base.
|
|
4
4
|
|
|
5
|
+
##Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'baseanything'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
```ruby
|
|
15
|
+
$ bundle
|
|
16
|
+
```
|
|
17
|
+
Or install it yourself as:
|
|
18
|
+
```ruby
|
|
19
|
+
$ gem install baseanything
|
|
20
|
+
```
|
|
5
21
|
##Usage
|
|
6
22
|
|
|
7
23
|
###Making your own number system
|
|
@@ -16,7 +32,7 @@ symbols = ['@', '#', '$', '%', '&']
|
|
|
16
32
|
And create the number system like so:
|
|
17
33
|
|
|
18
34
|
```ruby
|
|
19
|
-
NumberSystem.new(symbols)
|
|
35
|
+
BaseAnything::NumberSystem.new(symbols)
|
|
20
36
|
```
|
|
21
37
|
|
|
22
38
|
And each symbol will have the following decimal values:
|
|
@@ -41,7 +57,7 @@ So:
|
|
|
41
57
|
You can take a number written in your base system and convert it to any base up to 35 with the to_base(num, base) method.
|
|
42
58
|
|
|
43
59
|
```ruby
|
|
44
|
-
my_base_4 = NumberSystem.new(['@', '#', '$', '%', '&'])
|
|
60
|
+
my_base_4 = BaseAnything::NumberSystem.new(['@', '#', '$', '%', '&'])
|
|
45
61
|
my_num = '%#'
|
|
46
62
|
target_base = 4
|
|
47
63
|
|
|
@@ -64,7 +80,6 @@ my_base_4.to_decimal(num) #converts num to base10
|
|
|
64
80
|
my_base_4.to_undecimal(num) #converts num to base11
|
|
65
81
|
my_base_4.to_duodecimal(num) #converts num to base12
|
|
66
82
|
my_base_4.to_tridecimal(num) #converts num to base13
|
|
67
|
-
|
|
68
83
|
my_base_4.to_tetradecimal(num) #converts num to base14
|
|
69
84
|
my_base_4.to_pentadecimal(num) #converts num to base15
|
|
70
85
|
my_base_4.to_hexadecimal(num) #converts num to base16
|
|
@@ -81,7 +96,7 @@ my_base_4.to_hexatrigesimal(num) #converts num to base36
|
|
|
81
96
|
You can take a number written in any base system and convert it to your base system using the from_base(num, base) method.
|
|
82
97
|
|
|
83
98
|
```ruby
|
|
84
|
-
my_base_4 = NumberSystem.new(['@', '#', '$', '%', '&'])
|
|
99
|
+
my_base_4 = BaseAnything::NumberSystem.new(['@', '#', '$', '%', '&'])
|
|
85
100
|
my_num = '100011'
|
|
86
101
|
base_of_my_num = 2
|
|
87
102
|
|
|
@@ -92,26 +107,47 @@ my_base_4.from_base(my_num, target_base)
|
|
|
92
107
|
Alternatively, you can use any of the following semantic methods:
|
|
93
108
|
|
|
94
109
|
```ruby
|
|
95
|
-
my_base_4.
|
|
96
|
-
my_base_4.
|
|
97
|
-
my_base_4.
|
|
98
|
-
my_base_4.
|
|
99
|
-
my_base_4.
|
|
100
|
-
my_base_4.
|
|
101
|
-
my_base_4.
|
|
102
|
-
my_base_4.
|
|
103
|
-
my_base_4.
|
|
104
|
-
my_base_4.
|
|
105
|
-
my_base_4.
|
|
106
|
-
my_base_4.
|
|
110
|
+
my_base_4.from_binary(num) #converts num from base2
|
|
111
|
+
my_base_4.from_ternary(num) #converts num from base3
|
|
112
|
+
my_base_4.from_quaternary(num) #converts num from base4
|
|
113
|
+
my_base_4.from_quinary(num) #converts num from base5
|
|
114
|
+
my_base_4.from_senary(num) #converts num from base6
|
|
115
|
+
my_base_4.from_heptary(num) #converts num from base7
|
|
116
|
+
my_base_4.from_octal(num) #converts num from base8
|
|
117
|
+
my_base_4.from_nonary(num) #converts num from base9
|
|
118
|
+
my_base_4.from_decimal(num) #converts num from base10
|
|
119
|
+
my_base_4.from_undecimal(num) #converts num from base11
|
|
120
|
+
my_base_4.from_duodecimal(num) #converts num from base12
|
|
121
|
+
my_base_4.from_tridecimal(num) #converts num from base13
|
|
122
|
+
my_base_4.from_tetradecimal(num) #converts num from base14
|
|
123
|
+
my_base_4.from_pentadecimal(num) #converts num from base15
|
|
124
|
+
my_base_4.from_hexadecimal(num) #converts num from base16
|
|
125
|
+
my_base_4.from_vigesimal(num) #converts num from base20
|
|
126
|
+
my_base_4.from_hexavigesimal(num) #converts num from base24
|
|
127
|
+
my_base_4.from_heptavigesimal(num) #converts num from base27
|
|
128
|
+
my_base_4.from_trigesimal(num) #converts num from base30
|
|
129
|
+
my_base_4.from_duotrigesimal(num) #converts num from base32
|
|
130
|
+
my_base_4.from_hexatrigesimal(num) #converts num from base36
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
###Math
|
|
134
|
+
|
|
135
|
+
You can do mathematical operations with numbers from your number system.
|
|
136
|
+
|
|
137
|
+
```ruby
|
|
138
|
+
my_base_4 = BaseAnything::NumberSystem.new(['@', '#', '$', '%', '&'])
|
|
139
|
+
num1 = '&$'
|
|
140
|
+
num2 = '%#'
|
|
141
|
+
|
|
142
|
+
my_base_4.add(num1, num2)
|
|
143
|
+
my_base_4.subtract(num1, num2)
|
|
144
|
+
my_base_4.multiply(num1, num2)
|
|
145
|
+
my_base_4.divide(num1, num2)
|
|
146
|
+
my_base_4.modulo(num1, num2)
|
|
147
|
+
my_base_4.exponent(num1, num2)
|
|
107
148
|
|
|
108
|
-
my_base_4.to_tetradecimal(num) #converts num to base14
|
|
109
|
-
my_base_4.to_pentadecimal(num) #converts num to base15
|
|
110
|
-
my_base_4.to_hexadecimal(num) #converts num to base16
|
|
111
|
-
my_base_4.to_vigesimal(num) #converts num to base20
|
|
112
|
-
my_base_4.to_hexavigesimal(num) #converts num to base24
|
|
113
|
-
my_base_4.to_heptavigesimal(num) #converts num to base27
|
|
114
|
-
my_base_4.to_trigesimal(num) #converts num to base30
|
|
115
|
-
my_base_4.to_duotrigesimal(num) #converts num to base32
|
|
116
|
-
my_base_4.to_hexatrigesimal(num) #converts num to base36
|
|
117
149
|
```
|
|
150
|
+
|
|
151
|
+
## Contributing
|
|
152
|
+
|
|
153
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/DouglasTGordon/BaseAnything.
|
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "wordsoup"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require "irb"
|
|
14
|
+
IRB.start
|
data/lib/baseanything.rb
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
module BaseAnything
|
|
2
|
+
|
|
2
3
|
class NumberSystem
|
|
3
4
|
|
|
4
5
|
def initialize(arr)
|
|
@@ -44,9 +45,7 @@ module BaseAnything
|
|
|
44
45
|
num -= to_remove
|
|
45
46
|
highest_quot -= 1
|
|
46
47
|
end
|
|
47
|
-
|
|
48
48
|
output
|
|
49
|
-
|
|
50
49
|
end
|
|
51
50
|
|
|
52
51
|
def method_missing(name, num)
|
|
@@ -60,71 +59,103 @@ module BaseAnything
|
|
|
60
59
|
end
|
|
61
60
|
end
|
|
62
61
|
|
|
62
|
+
def add(num1, num2)
|
|
63
|
+
decimal = own_to_dec(num1) + own_to_dec(num2)
|
|
64
|
+
from_base(decimal.to_s, 10)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def subtract(num1, num2)
|
|
68
|
+
decimal = own_to_dec(num1) - own_to_dec(num2)
|
|
69
|
+
from_base(decimal.to_s, 10)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def multiply(num1, num2)
|
|
73
|
+
decimal = own_to_dec(num1) * own_to_dec(num2)
|
|
74
|
+
from_base(decimal.to_s, 10)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def divide(num1, num2)
|
|
78
|
+
decimal = own_to_dec(num1) / own_to_dec(num2)
|
|
79
|
+
from_base(decimal.to_s, 10)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def modulo(num1, num2)
|
|
83
|
+
decimal = own_to_dec(num1) % own_to_dec(num2)
|
|
84
|
+
from_base(decimal.to_s, 10)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def exponent(num1, num2)
|
|
88
|
+
decimal = own_to_dec(num1) ** own_to_dec(num2)
|
|
89
|
+
from_base(decimal.to_s, 10)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
|
|
63
93
|
private
|
|
64
94
|
|
|
95
|
+
ANY_BASE = {
|
|
96
|
+
'0' => '0',
|
|
97
|
+
'1' => '1',
|
|
98
|
+
'2' => '2',
|
|
99
|
+
'3' => '3',
|
|
100
|
+
'4' => '4',
|
|
101
|
+
'5' => '5',
|
|
102
|
+
'6' => '6',
|
|
103
|
+
'7' => '7',
|
|
104
|
+
'8' => '8',
|
|
105
|
+
'9' => '9',
|
|
106
|
+
'10' => 'A',
|
|
107
|
+
'11' => 'B',
|
|
108
|
+
'12' => 'C',
|
|
109
|
+
'13' => 'D',
|
|
110
|
+
'14' => 'E',
|
|
111
|
+
'15' => 'F',
|
|
112
|
+
'16' => 'G',
|
|
113
|
+
'17' => 'H',
|
|
114
|
+
'18' => 'I',
|
|
115
|
+
'19' => 'J',
|
|
116
|
+
'20' => 'K',
|
|
117
|
+
'21' => 'L',
|
|
118
|
+
'22' => 'M',
|
|
119
|
+
'23' => 'N',
|
|
120
|
+
'24' => 'O',
|
|
121
|
+
'25' => 'P',
|
|
122
|
+
'26' => 'Q',
|
|
123
|
+
'27' => 'R',
|
|
124
|
+
'28' => 'S',
|
|
125
|
+
'29' => 'T',
|
|
126
|
+
'30' => 'U',
|
|
127
|
+
'31' => 'V',
|
|
128
|
+
'32' => 'W',
|
|
129
|
+
'33' => 'X',
|
|
130
|
+
'34' => 'Y',
|
|
131
|
+
'35' => 'Z'
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
FIND_BASE = {
|
|
135
|
+
'binary' => 2,
|
|
136
|
+
'ternary' => 3,
|
|
137
|
+
'quaternary' => 4,
|
|
138
|
+
'quinary' => 5,
|
|
139
|
+
'senary' => 6,
|
|
140
|
+
'heptary' => 7,
|
|
141
|
+
'octal' => 8,
|
|
142
|
+
'nonary' => 9,
|
|
143
|
+
'decimal' => 10,
|
|
144
|
+
'undecimal' => 11,
|
|
145
|
+
'duodecimal' => 12,
|
|
146
|
+
'tridecimal' => 13,
|
|
147
|
+
'tetradecimal' => 14,
|
|
148
|
+
'pentadecimal' => 15,
|
|
149
|
+
'hexadecimal' => 16,
|
|
150
|
+
'vigesimal' => 20,
|
|
151
|
+
'hexavigesimal' => 24,
|
|
152
|
+
'heptavigesimal' => 27,
|
|
153
|
+
'trigesimal' => 30,
|
|
154
|
+
'duotrigesimal' => 32,
|
|
155
|
+
'hexatrigesimal' => 36
|
|
156
|
+
}
|
|
157
|
+
|
|
65
158
|
|
|
66
|
-
ANY_BASE = {
|
|
67
|
-
'0' => '0',
|
|
68
|
-
'1' => '1',
|
|
69
|
-
'2' => '2',
|
|
70
|
-
'3' => '3',
|
|
71
|
-
'4' => '4',
|
|
72
|
-
'5' => '5',
|
|
73
|
-
'6' => '6',
|
|
74
|
-
'7' => '7',
|
|
75
|
-
'8' => '8',
|
|
76
|
-
'9' => '9',
|
|
77
|
-
'10' => 'A',
|
|
78
|
-
'11' => 'B',
|
|
79
|
-
'12' => 'C',
|
|
80
|
-
'13' => 'D',
|
|
81
|
-
'14' => 'E',
|
|
82
|
-
'15' => 'F',
|
|
83
|
-
'16' => 'G',
|
|
84
|
-
'17' => 'H',
|
|
85
|
-
'18' => 'I',
|
|
86
|
-
'19' => 'J',
|
|
87
|
-
'20' => 'K',
|
|
88
|
-
'21' => 'L',
|
|
89
|
-
'22' => 'M',
|
|
90
|
-
'23' => 'N',
|
|
91
|
-
'24' => 'O',
|
|
92
|
-
'25' => 'P',
|
|
93
|
-
'26' => 'Q',
|
|
94
|
-
'27' => 'R',
|
|
95
|
-
'28' => 'S',
|
|
96
|
-
'29' => 'T',
|
|
97
|
-
'30' => 'U',
|
|
98
|
-
'31' => 'V',
|
|
99
|
-
'32' => 'W',
|
|
100
|
-
'33' => 'X',
|
|
101
|
-
'34' => 'Y',
|
|
102
|
-
'35' => 'Z'
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
FIND_BASE = {
|
|
106
|
-
'binary' => 2,
|
|
107
|
-
'ternary' => 3,
|
|
108
|
-
'quaternary' => 4,
|
|
109
|
-
'quinary' => 5,
|
|
110
|
-
'senary' => 6,
|
|
111
|
-
'heptary' => 7,
|
|
112
|
-
'octal' => 8,
|
|
113
|
-
'nonary' => 9,
|
|
114
|
-
'decimal' => 10,
|
|
115
|
-
'undecimal' => 11,
|
|
116
|
-
'duodecimal' => 12,
|
|
117
|
-
'tridecimal' => 13,
|
|
118
|
-
'tetradecimal' => 14,
|
|
119
|
-
'pentadecimal' => 15,
|
|
120
|
-
'hexadecimal' => 16,
|
|
121
|
-
'vigesimal' => 20,
|
|
122
|
-
'hexavigesimal' => 24,
|
|
123
|
-
'heptavigesimal' => 27,
|
|
124
|
-
'trigesimal' => 30,
|
|
125
|
-
'duotrigesimal' => 32,
|
|
126
|
-
'hexatrigesimal' => 36
|
|
127
|
-
}
|
|
128
159
|
|
|
129
160
|
def make_correspondance_hash(arr)
|
|
130
161
|
hash = {}
|
|
@@ -156,3 +187,14 @@ module BaseAnything
|
|
|
156
187
|
|
|
157
188
|
end
|
|
158
189
|
end
|
|
190
|
+
|
|
191
|
+
# my_base_4 = BaseAnything::NumberSystem.new(['@', '#', '$', '%', '&'])
|
|
192
|
+
# num1 = '&$'
|
|
193
|
+
# num2 = '%#'
|
|
194
|
+
#
|
|
195
|
+
# p my_base_4.add(num1, num2)
|
|
196
|
+
# p my_base_4.subtract(num1, num2)
|
|
197
|
+
# p my_base_4.multiply(num1, num2)
|
|
198
|
+
# p my_base_4.divide(num1, num2)
|
|
199
|
+
# p my_base_4.modulo(num1, num2)
|
|
200
|
+
# p my_base_4.exponent(num1, num2)
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: baseanything
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- DouglasTGordon
|
|
@@ -50,7 +50,9 @@ files:
|
|
|
50
50
|
- Gemfile
|
|
51
51
|
- Gemfile.lock
|
|
52
52
|
- README.md
|
|
53
|
+
- bin/console
|
|
53
54
|
- lib/baseanything.rb
|
|
55
|
+
- lib/baseanything/version.rb
|
|
54
56
|
homepage: https://github.com/douglastgordon/BaseAnything
|
|
55
57
|
licenses: []
|
|
56
58
|
metadata: {}
|