binary 1.3.0 → 1.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +0 -2
- data/Gemfile.lock +1 -1
- data/README.md +11 -12
- data/lib/binary.rb +21 -31
- data/lib/binary/extensions.rb +0 -1
- data/lib/binary/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 82df33a480338b1fb25a0f9038fec31a739b578bbc32d202ee3f09788a695c4f
|
4
|
+
data.tar.gz: 4091f4434ddcc264a9ae92e6a590ae970d91eb771ed0d0b7b690a25b751ac93b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dcea50018ebfe0e4b610e1f2dd566865bbea44cda44faa7209f761fdcb9c841abb06b5e94b5791a7e53b6cf2eb9b634fad6a371ed5d2bd0980f23cc808fb6d63
|
7
|
+
data.tar.gz: 54b779f76a81a82b04dedc2421fa418795dec2d3515399c8966c2c511cd20acdbc125a4e371d21094c4f659bfeff86c767d896ddf8807690d0df38dd4c725d2a
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Binary
|
2
2
|
|
3
|
-
Simple Ruby gem to convert
|
3
|
+
Simple Ruby gem to convert numbers into binaries & binaries into numbers
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -10,22 +10,21 @@ Add this line to your application's Gemfile:
|
|
10
10
|
gem 'binary'
|
11
11
|
```
|
12
12
|
|
13
|
-
|
13
|
+
Then run:
|
14
14
|
|
15
15
|
$ bundle
|
16
16
|
|
17
|
-
Or
|
17
|
+
Or from the terminal:
|
18
18
|
|
19
19
|
$ gem install binary
|
20
20
|
|
21
21
|
## Usage
|
22
|
-
Simply require the gem in your code:
|
23
22
|
|
24
23
|
```ruby
|
25
24
|
require 'binary'
|
26
25
|
```
|
27
26
|
|
28
|
-
|
27
|
+
Binary to number:
|
29
28
|
|
30
29
|
```ruby
|
31
30
|
Binary.binary 2018
|
@@ -36,7 +35,7 @@ Or
|
|
36
35
|
```
|
37
36
|
`Output: "11111100010"`.
|
38
37
|
|
39
|
-
|
38
|
+
Array of numbers to binaries.
|
40
39
|
```ruby
|
41
40
|
Binary.binary([[7,9,11])
|
42
41
|
|
@@ -46,7 +45,7 @@ Or
|
|
46
45
|
```
|
47
46
|
`Output: ["111", "1001", "1011"]`.
|
48
47
|
|
49
|
-
|
48
|
+
Number to binary:
|
50
49
|
```ruby
|
51
50
|
Binary.number "11111100010"
|
52
51
|
|
@@ -56,6 +55,7 @@ Or
|
|
56
55
|
```
|
57
56
|
`Output: 2018`.
|
58
57
|
|
58
|
+
Array of binaries to numbers:
|
59
59
|
```ruby
|
60
60
|
Binary.number(["111", "1001", "1011"])
|
61
61
|
|
@@ -71,31 +71,30 @@ Other methods available:
|
|
71
71
|
```ruby
|
72
72
|
# number of bits in a number's binary
|
73
73
|
Binary.bits 1000
|
74
|
+
# number of bits in 1000.to_b ("1111101000")
|
74
75
|
```
|
75
76
|
`Output: 10`.
|
76
77
|
|
77
78
|
```ruby
|
78
79
|
# number of ones in a number's binary
|
79
80
|
Binary.ones 1000
|
81
|
+
# number of ones in 1000.to_b ("1111101000")
|
80
82
|
```
|
81
83
|
`Output: 6`.
|
82
84
|
|
83
85
|
```ruby
|
84
86
|
# number of zeros in a number's binary
|
85
87
|
Binary.zeros 1000
|
88
|
+
# number of ones in 1000.to_b ("1111101000")
|
86
89
|
```
|
87
90
|
`Output: 4`.
|
88
91
|
|
89
92
|
```ruby
|
90
|
-
#
|
93
|
+
# list of binaries of prime numbers from 2 to 25
|
91
94
|
Binary.prime 25
|
92
95
|
```
|
93
96
|
`Output: ["10", "11", "101", "111", "1011", "1101", "10001", "10011", "10111"]`.
|
94
97
|
|
95
|
-
## Contributing
|
96
|
-
|
97
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/almishkawi/binary.
|
98
|
-
|
99
98
|
## License
|
100
99
|
|
101
100
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/lib/binary.rb
CHANGED
@@ -1,59 +1,49 @@
|
|
1
|
-
|
2
1
|
require_relative 'binary/extensions.rb'
|
3
2
|
require 'binary/version'
|
4
3
|
require 'prime'
|
5
4
|
|
6
5
|
module Binary
|
7
|
-
def self.binary(
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
else
|
24
|
-
nil
|
25
|
-
end
|
6
|
+
def self.binary(i=nil)
|
7
|
+
return nil if block_given?
|
8
|
+
return i.map { |n| n.class == Integer ? n.abs.to_s(2) : nil } if array? i
|
9
|
+
i = i.abs if i.to_i < 0
|
10
|
+
return i.to_s(2) if i.class == Integer
|
11
|
+
nil
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.number(i=nil)
|
15
|
+
return i.map { |n| n.class == String ? n.to_i(2) : nil } if array? i
|
16
|
+
return i.to_i(2) if i.class == String
|
17
|
+
nil
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.array?(i)
|
21
|
+
i.class == Array
|
26
22
|
end
|
27
23
|
|
28
24
|
def self.bits num=nil
|
29
25
|
return nil unless num
|
30
|
-
binary
|
31
|
-
binary.length
|
26
|
+
binary(num).length
|
32
27
|
end
|
33
28
|
|
34
29
|
def self.zeros num=nil
|
35
30
|
return nil unless num
|
36
|
-
binary
|
37
|
-
binary.count("0")
|
31
|
+
binary(num).count("0")
|
38
32
|
end
|
39
33
|
|
40
34
|
def self.ones num=nil
|
41
35
|
return nil unless num
|
42
|
-
binary
|
43
|
-
binary.count("1")
|
36
|
+
binary(num).count("1")
|
44
37
|
end
|
45
38
|
|
46
39
|
def self.prime num=nil
|
47
40
|
return nil unless num
|
48
41
|
binaries = []
|
49
|
-
Prime.each(num)
|
50
|
-
binaries << binary(prime)
|
51
|
-
end
|
52
|
-
binaries
|
42
|
+
binaries = Prime.each(num) { |prime| binaries << binary(prime) }
|
53
43
|
end
|
54
44
|
|
55
45
|
def self.method_missing(method, *args, &block)
|
56
|
-
puts "
|
46
|
+
puts "No method was found with the name #{method}."
|
57
47
|
puts 'Methods available are:'
|
58
48
|
puts (Binary.methods - Object.methods - [:method_missing])
|
59
49
|
end
|
data/lib/binary/extensions.rb
CHANGED
data/lib/binary/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: binary
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.3.
|
4
|
+
version: 1.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mo Almishkawi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-07-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|