binary 1.3.0 → 1.3.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 655c7349b0f48a1172a4ff56001ffacdb35ea96fa679524cc2a3ccf87cc095f1
4
- data.tar.gz: 641c38cc2c323d4ac026c51f2b8ba2e17df737deed9d2747e8ced0adf3188fc8
3
+ metadata.gz: 82df33a480338b1fb25a0f9038fec31a739b578bbc32d202ee3f09788a695c4f
4
+ data.tar.gz: 4091f4434ddcc264a9ae92e6a590ae970d91eb771ed0d0b7b690a25b751ac93b
5
5
  SHA512:
6
- metadata.gz: d5c9e16648b2f0e3aa53ff598f58e41d08c3d6fd00d8993fe4a3b8119983e7278ed3ea7e642d1af76cee801a348fe7062679ec44700d017fcefbe53335712345
7
- data.tar.gz: 33f9370e77174484d52fdfe55233d6283a1cd18f145c3c5baf98bbf22aff780a606c29ee48f83562e7efc70b0fc4c253b013b44f8f14dec5c0076a106d2f2a45
6
+ metadata.gz: dcea50018ebfe0e4b610e1f2dd566865bbea44cda44faa7209f761fdcb9c841abb06b5e94b5791a7e53b6cf2eb9b634fad6a371ed5d2bd0980f23cc808fb6d63
7
+ data.tar.gz: 54b779f76a81a82b04dedc2421fa418795dec2d3515399c8966c2c511cd20acdbc125a4e371d21094c4f659bfeff86c767d896ddf8807690d0df38dd4c725d2a
data/Gemfile CHANGED
@@ -1,6 +1,4 @@
1
1
  source "https://rubygems.org"
2
2
 
3
3
  git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
-
5
- # Specify your gem's dependencies in binary.gemspec
6
4
  gemspec
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- binary (1.2.1)
4
+ binary (1.3.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Binary
2
2
 
3
- Simple Ruby gem to convert integers into binaries and binaries into integers
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
- And then execute:
13
+ Then run:
14
14
 
15
15
  $ bundle
16
16
 
17
- Or install it yourself as:
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
- Then to get the binary representation of a number, you just need to call `Binary.binary` and pass the number to it, something like follows:
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
- Also you can pass an array of integers to the method to get an array of their binary values.
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
- You can also convert binaries into integers by calling method `number` as follows:
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
- # get array of binaries of all prime numbers between 2 and the given number
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).
@@ -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(input=nil)
8
- if input.class == Array
9
- input.map { |num| num.class == Integer ? num.abs.to_s(2) : nil }
10
- elsif input.class == Integer
11
- input = input.abs if input < 0
12
- input.to_s(2)
13
- else
14
- nil
15
- end
16
- end
17
-
18
- def self.number(input=nil)
19
- if input.class == Array
20
- input.map { |num| num.class == String ? num.to_i(2) : nil }
21
- elsif input.class == String
22
- input.to_i(2)
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 = binary(num)
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 = binary(num)
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 = binary(num)
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) do |prime|
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 "There's no method #{method} found in Binary module."
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
@@ -1,4 +1,3 @@
1
-
2
1
  require 'binary/extensions/array'
3
2
  require 'binary/extensions/integer'
4
3
  require 'binary/extensions/string'
@@ -1,3 +1,3 @@
1
1
  module Binary
2
- VERSION = "1.3.0"
2
+ VERSION = "1.3.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.0
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-04-10 00:00:00.000000000 Z
11
+ date: 2018-07-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler