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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: 82df33a480338b1fb25a0f9038fec31a739b578bbc32d202ee3f09788a695c4f
4
- data.tar.gz: 4091f4434ddcc264a9ae92e6a590ae970d91eb771ed0d0b7b690a25b751ac93b
2
+ SHA1:
3
+ metadata.gz: 630faaced4bb8ce31bced4ae54808e4e9cb19038
4
+ data.tar.gz: 352e9baad2a5baae26cde91e59e088928be4aecc
5
5
  SHA512:
6
- metadata.gz: dcea50018ebfe0e4b610e1f2dd566865bbea44cda44faa7209f761fdcb9c841abb06b5e94b5791a7e53b6cf2eb9b634fad6a371ed5d2bd0980f23cc808fb6d63
7
- data.tar.gz: 54b779f76a81a82b04dedc2421fa418795dec2d3515399c8966c2c511cd20acdbc125a4e371d21094c4f659bfeff86c767d896ddf8807690d0df38dd4c725d2a
6
+ metadata.gz: 418c33ddff87d92d5c143deef1bbd830614d7db4c06bd7d971c45ffe41d5b73f25462a1e131a92504555f3d86388b508cb9724e99fe83300355f8633e7e7fc36
7
+ data.tar.gz: c783f5208d9e9d4073fe137d097ad6520ad6b4fa033bf6486271928357f35bd0b7814e7ab835564bcfe9531d5b9598a7dbc4fff3694a59a8244ce9b3373f045b
@@ -2,4 +2,4 @@ sudo: false
2
2
  language: ruby
3
3
  rvm:
4
4
  - 2.5.0
5
- before_install: gem install bundler -v 1.16.0
5
+ before_install: gem install bundler
data/README.md CHANGED
@@ -24,7 +24,7 @@ Or from the terminal:
24
24
  require 'binary'
25
25
  ```
26
26
 
27
- Binary to number:
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
- Number to binary:
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 ones in 1000.to_b ("1111101000")
88
+ # number of zeros in 1000.to_b ("1111101000")
89
89
  ```
90
90
  `Output: 4`.
91
91
 
@@ -1,50 +1,47 @@
1
- require_relative 'binary/extensions.rb'
1
+ require_relative 'binary/extensions'
2
+
2
3
  require 'binary/version'
3
4
  require 'prime'
4
5
 
5
6
  module Binary
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
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
- 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
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
- def self.array?(i)
21
- i.class == Array
22
- end
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
- def self.bits num=nil
25
- return nil unless num
26
- binary(num).length
27
- end
20
+ def bits_count(num)
21
+ binary(num).length
22
+ end
28
23
 
29
- def self.zeros num=nil
30
- return nil unless num
31
- binary(num).count("0")
32
- end
24
+ def zeros_count(num)
25
+ binary(num).count("0")
26
+ end
33
27
 
34
- def self.ones num=nil
35
- return nil unless num
36
- binary(num).count("1")
37
- end
28
+ def ones_count(num)
29
+ binary(num).count("1")
30
+ end
38
31
 
39
- def self.prime num=nil
40
- return nil unless num
41
- binaries = []
42
- binaries = Prime.each(num) { |prime| binaries << binary(prime) }
43
- end
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
- def self.method_missing(method, *args, &block)
46
- puts "No method was found with the name #{method}."
47
- puts 'Methods available are:'
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
@@ -1,3 +1,3 @@
1
1
  require 'binary/extensions/array'
2
2
  require 'binary/extensions/integer'
3
- require 'binary/extensions/string'
3
+ require 'binary/extensions/string'
@@ -1,9 +1,9 @@
1
1
  class Array
2
-
3
2
  def to_b
4
3
  Binary.binary self
5
4
  end
5
+
6
6
  def to_num
7
7
  Binary.number self
8
8
  end
9
- end
9
+ end
@@ -1,6 +1,5 @@
1
1
  class Integer
2
-
3
2
  def to_b
4
3
  Binary.binary self
5
4
  end
6
- end
5
+ end
@@ -1,6 +1,5 @@
1
1
  class String
2
-
3
2
  def to_num
4
3
  Binary.number self
5
4
  end
6
- end
5
+ end
@@ -1,3 +1,3 @@
1
1
  module Binary
2
- VERSION = "1.3.1"
2
+ VERSION = "1.4.1"
3
3
  end
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.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: 2018-07-21 00:00:00.000000000 Z
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.7.4
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