binToDec 0.0.1 → 0.0.2
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 +4 -4
- data/Rakefile +7 -0
- data/lib/binToDec.rb +7 -2
- data/test/test_binToDec.rb +31 -0
- metadata +9 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f29ce3797c9016ea5929be748b10abf05fedceec
|
4
|
+
data.tar.gz: 39c1c194ab47c6b893aaa8e8f6904e7af1e6aca9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b6f9b4760ec0a41c994f47114f270f398f98c80dfeb17f52204ba6a15bb7f5a31f48aada53620421c63896b3e4c8cdcbcdc29bb293b99eb97a186953548dcaf5
|
7
|
+
data.tar.gz: 79be66dd2362227f630391c2f647b36ae2b0c95a8da277fbbe90fea4316164c1cfdec1ffa7404782cd78fbd99fa3f06a1e43517635f9fe21ba78ba1f562abde5
|
data/Rakefile
ADDED
data/lib/binToDec.rb
CHANGED
@@ -1,14 +1,19 @@
|
|
1
1
|
module Converter
|
2
2
|
def self.binToDec(arg1)
|
3
|
-
|
3
|
+
|
4
|
+
#code written by Tom Dowling (tomgdow)
|
5
|
+
|
6
|
+
#TEST FOR BINARY INPUT
|
7
|
+
#my_test will be equal to 1 only if arg1 is a binary number
|
4
8
|
my_test = arg1.to_s.split(//).map { |i| i.to_i }.find_all { |value| value > 0 }.inject(:*)
|
9
|
+
|
5
10
|
if my_test ==1
|
6
11
|
# Generate a binary number using the "move to the right and double" method
|
7
12
|
result = arg1.to_s.split(//).map { |i| i.to_i }.inject(0) { |accumulator, value| (accumulator + value) * 2 }
|
8
13
|
#The above result will be double what it should be, so need to divide by 2
|
9
14
|
return result/2
|
10
15
|
else
|
11
|
-
return "!Not
|
16
|
+
return "!Not binary: #{arg1}"
|
12
17
|
end
|
13
18
|
end
|
14
19
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'binToDec'
|
3
|
+
class BinaryTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
# Test if a valid binary number gives the correct result
|
6
|
+
def test_binary_true
|
7
|
+
assert_equal 8,
|
8
|
+
Converter.binToDec(1000)
|
9
|
+
end
|
10
|
+
|
11
|
+
# Test if an valid binary number given an incorrect value
|
12
|
+
def test_binary_false
|
13
|
+
assert_not_equal 9,
|
14
|
+
Converter.binToDec(1000)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Test if a invalid binary number gives correct error message
|
18
|
+
|
19
|
+
def test_non_binary_true
|
20
|
+
assert_equal "!Not binary: 2222",
|
21
|
+
Converter.binToDec(2222)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Test if a invalid binary number gives correct error message
|
25
|
+
|
26
|
+
def test_non_binary_false
|
27
|
+
assert_not_equal "On the Rails, Matz, and off the Rails. Such is life",
|
28
|
+
Converter.binToDec(2222)
|
29
|
+
end
|
30
|
+
|
31
|
+
end # End of Class
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: binToDec
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- tomgdow
|
@@ -10,15 +10,18 @@ bindir: bin
|
|
10
10
|
cert_chain: []
|
11
11
|
date: 2013-12-11 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
13
|
+
description: An experimental gem
|
14
14
|
email: thomasgdowling@gmail.com
|
15
15
|
executables: []
|
16
16
|
extensions: []
|
17
17
|
extra_rdoc_files: []
|
18
18
|
files:
|
19
|
+
- Rakefile
|
19
20
|
- lib/binToDec.rb
|
21
|
+
- test/test_binToDec.rb
|
20
22
|
homepage: http://rubygems.org/gems/binToDec
|
21
|
-
licenses:
|
23
|
+
licenses:
|
24
|
+
- MIT
|
22
25
|
metadata: {}
|
23
26
|
post_install_message:
|
24
27
|
rdoc_options: []
|
@@ -39,5 +42,6 @@ rubyforge_project:
|
|
39
42
|
rubygems_version: 2.1.5
|
40
43
|
signing_key:
|
41
44
|
specification_version: 4
|
42
|
-
summary: Convert a Binary Number to Decimal
|
43
|
-
test_files:
|
45
|
+
summary: Convert a Binary Number to Decimal only if input is binary
|
46
|
+
test_files:
|
47
|
+
- test/test_binToDec.rb
|