binary 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +14 -3
- data/binary.gemspec +13 -12
- data/lib/binary.rb +29 -8
- data/lib/binary/version.rb +1 -1
- metadata +7 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4ae05d54ed2e252fb2370b80873d8a4f99a19729d2f30a7e1f01b63370640371
|
4
|
+
data.tar.gz: 2b654b8942f0bc8f3f05bcee784dbc561384364d647368220e0b4616f65d2c8f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 54f9d155af4e0e97acf76a15cc74c42bbae04a8b3bc15c5ff7287eed7c0cfca993172d2d29c5627187677c480006be879919549da652fe4175bb4b43d3149d85
|
7
|
+
data.tar.gz: 24b917cbf949aa5acec0aaf81a4c34dbef6ae303656d76383062ccef025aac25a54febdd64c02bd9691189477f0e9a64f389710829d076428a309dc55a0fb029
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Binary
|
2
2
|
|
3
|
-
|
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([
|
37
|
+
Binary.binary([[7,9,11])
|
38
38
|
```
|
39
|
-
`Output: ["
|
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
|
|
data/binary.gemspec
CHANGED
@@ -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
|
8
|
-
spec.version
|
9
|
-
spec.authors
|
10
|
-
spec.email
|
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
|
13
|
-
spec.description
|
14
|
-
spec.homepage
|
15
|
-
spec.license
|
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
|
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
|
21
|
-
spec.executables
|
22
|
-
spec.require_paths
|
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"
|
data/lib/binary.rb
CHANGED
@@ -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.
|
17
|
-
|
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
|
-
|
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
|
-
|
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
|
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.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-
|
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
|
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: []
|