binary 1.1.1 → 1.2.0

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: 75468b4cae9ef1a47cc46ca0eb99c9fb85add39974ef077578dd40b413afa80d
4
- data.tar.gz: d1aea9ccf064910c3ebf4e8176ae199d2aa3df781c562e572de15034859cc7b8
3
+ metadata.gz: 4ae05d54ed2e252fb2370b80873d8a4f99a19729d2f30a7e1f01b63370640371
4
+ data.tar.gz: 2b654b8942f0bc8f3f05bcee784dbc561384364d647368220e0b4616f65d2c8f
5
5
  SHA512:
6
- metadata.gz: d9df1158b6adc4b43fbde0705077cfa02813c4d7c844b4357e6ef641d114ae41a23d48319c4fe300c49ba154f9059f3f1448a46ada83b52dea34f52e43d1c0dc
7
- data.tar.gz: 4091347bfafee9191823590b5a5ce9f2697c2217d1ea46039d7e7f97b84dc45c505d2342688f4e68a743bbd20fb66721dfc248221e5481b078897c31a0a59db9
6
+ metadata.gz: 54f9d155af4e0e97acf76a15cc74c42bbae04a8b3bc15c5ff7287eed7c0cfca993172d2d29c5627187677c480006be879919549da652fe4175bb4b43d3149d85
7
+ data.tar.gz: 24b917cbf949aa5acec0aaf81a4c34dbef6ae303656d76383062ccef025aac25a54febdd64c02bd9691189477f0e9a64f389710829d076428a309dc55a0fb029
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- binary (1.0.1)
4
+ binary (1.1.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
- Very simple gem to convert an integer into a binary representation
3
+ Simple Ruby gem to convert integers into binaries and binaries into integers
4
4
 
5
5
  ## Installation
6
6
 
@@ -34,9 +34,20 @@ Binary.binary 2018
34
34
 
35
35
  Also you can pass an array of integers to the method to get an array of their binary values.
36
36
  ```ruby
37
- Binary.binary([26,6,1991])
37
+ Binary.binary([[7,9,11])
38
38
  ```
39
- `Output: ["11010", "110", "11111000111"]`.
39
+ `Output: ["111", "1001", "1011"]`.
40
+
41
+ You can also convert binaries into integers by calling method `number` as follows:
42
+ ```ruby
43
+ Binary.number "11111100010"
44
+ ```
45
+ `Output: 2018`.
46
+
47
+ ```ruby
48
+ Binary.number(["111", "1001", "1011"])
49
+ ```
50
+ `Output: [7,9,11]`.
40
51
 
41
52
  Other methods you can use:
42
53
 
@@ -4,22 +4,23 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require "binary/version"
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "binary"
8
- spec.version = Binary::VERSION
9
- spec.authors = ["Mo Almishkawi"]
10
- spec.email = ["mo@almishkawi.me"]
7
+ spec.name = "binary"
8
+ spec.version = Binary::VERSION
9
+ spec.authors = ["Mo Almishkawi"]
10
+ spec.email = ["mo@almishkawi.me"]
11
+ spec.required_ruby_version = '~> 2.0'
11
12
 
12
- spec.summary = "Convert number into a binary"
13
- spec.description = "Takes integer/array of integers and returns binary representation/s"
14
- spec.homepage = ""
15
- spec.license = "MIT"
13
+ spec.summary = "Convert a number into a binary or convert a binary into a number"
14
+ spec.description = "Takes integers and returns binaries or takes binaries and returns numbers and some other methods"
15
+ spec.homepage = ""
16
+ spec.license = "MIT"
16
17
 
17
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
19
  f.match(%r{^(test|spec|features)/})
19
20
  end
20
- spec.bindir = "exe"
21
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
- spec.require_paths = ["lib"]
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
23
24
 
24
25
  spec.add_development_dependency "bundler", "~> 1.16"
25
26
  spec.add_development_dependency "rake", "~> 10.0"
@@ -4,7 +4,7 @@ require "prime"
4
4
  module Binary
5
5
  def self.binary(input=nil)
6
6
  if input.class == Array
7
- input.map { |num| num.class == Integer ? num.to_s(2) : nil }
7
+ input.map { |num| num.class == Integer ? num.abs.to_s(2) : nil }
8
8
  elsif input.class == Integer
9
9
  input = input.abs if input < 0
10
10
  input.to_s(2)
@@ -13,26 +13,47 @@ module Binary
13
13
  end
14
14
  end
15
15
 
16
- def self.bits num
17
- binary = binary(num) if num
16
+ def self.number(input=nil)
17
+ if input.class == Array
18
+ input.map { |num| num.class == String ? num.to_i(2) : nil }
19
+ elsif input.class == String
20
+ input.to_i(2)
21
+ else
22
+ nil
23
+ end
24
+ end
25
+
26
+ def self.bits num=nil
27
+ return nil unless num
28
+ binary = binary(num)
18
29
  binary.length
19
30
  end
20
31
 
21
- def self.zeros num
22
- binary = binary(num) if num
32
+ def self.zeros num=nil
33
+ return nil unless num
34
+ binary = binary(num)
23
35
  binary.count("0")
24
36
  end
25
37
 
26
- def self.ones num
27
- binary = binary(num) if num
38
+ def self.ones num=nil
39
+ return nil unless num
40
+ binary = binary(num)
28
41
  binary.count("1")
29
42
  end
30
43
 
31
- def self.prime num
44
+ def self.prime num=nil
45
+ return nil unless num
32
46
  binaries = []
33
47
  Prime.each(num) do |prime|
34
48
  binaries << binary(prime)
35
49
  end
36
50
  binaries
37
51
  end
52
+
53
+ def self.method_missing(method, *args, &block)
54
+ puts "There is no method called #{method} in Binary."
55
+ p 'methods available are:'
56
+ ['binary', 'number', 'bit', 'zeros', 'ones', 'prime'].each { |m| p "#{m}" }
57
+ nil
58
+ end
38
59
  end
@@ -1,3 +1,3 @@
1
1
  module Binary
2
- VERSION = "1.1.1"
2
+ VERSION = "1.2.0"
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.1.1
4
+ version: 1.2.0
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-03-23 00:00:00.000000000 Z
11
+ date: 2018-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,7 +52,8 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
- description: Takes integer/array of integers and returns binary representation/s
55
+ description: Takes integers and returns binaries or takes binaries and returns numbers
56
+ and some other methods
56
57
  email:
57
58
  - mo@almishkawi.me
58
59
  executables: []
@@ -82,9 +83,9 @@ require_paths:
82
83
  - lib
83
84
  required_ruby_version: !ruby/object:Gem::Requirement
84
85
  requirements:
85
- - - ">="
86
+ - - "~>"
86
87
  - !ruby/object:Gem::Version
87
- version: '0'
88
+ version: '2.0'
88
89
  required_rubygems_version: !ruby/object:Gem::Requirement
89
90
  requirements:
90
91
  - - ">="
@@ -95,5 +96,5 @@ rubyforge_project:
95
96
  rubygems_version: 2.7.4
96
97
  signing_key:
97
98
  specification_version: 4
98
- summary: Convert number into a binary
99
+ summary: Convert a number into a binary or convert a binary into a number
99
100
  test_files: []