binary 1.3.1 → 1.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +1 -1
- data/README.md +3 -3
- data/lib/binary.rb +34 -37
- data/lib/binary/extensions.rb +1 -1
- data/lib/binary/extensions/array.rb +2 -2
- data/lib/binary/extensions/integer.rb +1 -2
- data/lib/binary/extensions/string.rb +1 -2
- data/lib/binary/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 630faaced4bb8ce31bced4ae54808e4e9cb19038
|
4
|
+
data.tar.gz: 352e9baad2a5baae26cde91e59e088928be4aecc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 418c33ddff87d92d5c143deef1bbd830614d7db4c06bd7d971c45ffe41d5b73f25462a1e131a92504555f3d86388b508cb9724e99fe83300355f8633e7e7fc36
|
7
|
+
data.tar.gz: c783f5208d9e9d4073fe137d097ad6520ad6b4fa033bf6486271928357f35bd0b7814e7ab835564bcfe9531d5b9598a7dbc4fff3694a59a8244ce9b3373f045b
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -24,7 +24,7 @@ Or from the terminal:
|
|
24
24
|
require 'binary'
|
25
25
|
```
|
26
26
|
|
27
|
-
|
27
|
+
Number to binary:
|
28
28
|
|
29
29
|
```ruby
|
30
30
|
Binary.binary 2018
|
@@ -45,7 +45,7 @@ Or
|
|
45
45
|
```
|
46
46
|
`Output: ["111", "1001", "1011"]`.
|
47
47
|
|
48
|
-
|
48
|
+
Binary to number:
|
49
49
|
```ruby
|
50
50
|
Binary.number "11111100010"
|
51
51
|
|
@@ -85,7 +85,7 @@ Binary.ones 1000
|
|
85
85
|
```ruby
|
86
86
|
# number of zeros in a number's binary
|
87
87
|
Binary.zeros 1000
|
88
|
-
# number of
|
88
|
+
# number of zeros in 1000.to_b ("1111101000")
|
89
89
|
```
|
90
90
|
`Output: 4`.
|
91
91
|
|
data/lib/binary.rb
CHANGED
@@ -1,50 +1,47 @@
|
|
1
|
-
require_relative 'binary/extensions
|
1
|
+
require_relative 'binary/extensions'
|
2
|
+
|
2
3
|
require 'binary/version'
|
3
4
|
require 'prime'
|
4
5
|
|
5
6
|
module Binary
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
i = i.abs if i.to_i < 0
|
10
|
-
return i.to_s(2) if i.class == Integer
|
11
|
-
nil
|
12
|
-
end
|
7
|
+
class << self
|
8
|
+
def binary(i = nil)
|
9
|
+
return i.map { |e| e.is_a?(Integer) ? e.abs.to_s(2) : nil } if i.is_a?(Array)
|
13
10
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
nil
|
18
|
-
end
|
11
|
+
i = i.abs if i&.to_i < 0
|
12
|
+
return i.to_s(2) if i.is_a?(Integer)
|
13
|
+
end
|
19
14
|
|
20
|
-
|
21
|
-
|
22
|
-
|
15
|
+
def number(i = nil)
|
16
|
+
return i.map { |e| e.is_a?(String) ? e.to_i(2) : nil } if i.is_a?(Array)
|
17
|
+
return i.to_i(2) if i.is_a?(String)
|
18
|
+
end
|
23
19
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
end
|
20
|
+
def bits_count(num)
|
21
|
+
binary(num).length
|
22
|
+
end
|
28
23
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
end
|
24
|
+
def zeros_count(num)
|
25
|
+
binary(num).count("0")
|
26
|
+
end
|
33
27
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
end
|
28
|
+
def ones_count(num)
|
29
|
+
binary(num).count("1")
|
30
|
+
end
|
38
31
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
32
|
+
def prime(num)
|
33
|
+
Prime.each(num).map { |prime| binary(prime) }
|
34
|
+
end
|
35
|
+
|
36
|
+
def random(num = 1000)
|
37
|
+
binary rand(1..num)
|
38
|
+
end
|
39
|
+
|
40
|
+
def method_missing(method, *args, &block)
|
41
|
+
puts "No method found: #{method}"
|
44
42
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
puts (Binary.methods - Object.methods - [:method_missing])
|
43
|
+
puts 'Methods available are:'
|
44
|
+
puts (Binary.methods - Object.methods - [:method_missing])
|
45
|
+
end
|
49
46
|
end
|
50
47
|
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.
|
4
|
+
version: 1.4.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:
|
11
|
+
date: 2019-05-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -96,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
96
96
|
version: '0'
|
97
97
|
requirements: []
|
98
98
|
rubyforge_project:
|
99
|
-
rubygems_version: 2.
|
99
|
+
rubygems_version: 2.6.10
|
100
100
|
signing_key:
|
101
101
|
specification_version: 4
|
102
102
|
summary: Convert numbers into binaries and binaries into numbers
|