romanic 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +20 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +47 -0
- data/Rakefile +2 -0
- data/lib/romanic.rb +7 -0
- data/lib/romanic/base.rb +13 -0
- data/lib/romanic/romanic_integers.rb +19 -0
- data/lib/romanic/romanic_strings.rb +19 -0
- data/lib/romanic/version.rb +3 -0
- data/romanic.gemspec +17 -0
- data/tests/romanic_tests.rb +45 -0
- metadata +59 -0
data/.gitignore
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
.DS_Store
|
19
|
+
lib/.DS_Store
|
20
|
+
tests/.DS_Store
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 profh
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# Romanic
|
2
|
+
==========
|
3
|
+
This is a simple gem written as a class demonstration that converts integers to roman numerals and roman numerals to integers. It adds methods to the Integer and String classes that allow a user to send a 'roman' message to either of these types of objects and get an appropriate response. More details on how to use the gem can be found below.
|
4
|
+
|
5
|
+
|
6
|
+
Installation
|
7
|
+
------------
|
8
|
+
Installing this gem is pretty simple -- just type on the command line:
|
9
|
+
|
10
|
+
```
|
11
|
+
$ gem install romanic
|
12
|
+
```
|
13
|
+
|
14
|
+
And add this gem into any other code with:
|
15
|
+
|
16
|
+
```
|
17
|
+
require 'rubygems'
|
18
|
+
require 'romanic'
|
19
|
+
```
|
20
|
+
|
21
|
+
|
22
|
+
Usage
|
23
|
+
------------
|
24
|
+
There are basically two methods to this gem:
|
25
|
+
|
26
|
+
* to_roman
|
27
|
+
* from_roman
|
28
|
+
|
29
|
+
**to_roman** -- This method simply allows an integer to be converted to its equivalent roman numeral. Because of limitations in the roman numeral system, the integer must be positive (i.e., greater than zero) and less than 5,000. If the integer is not within these bounds, then the method will return nil.
|
30
|
+
|
31
|
+
**from_roman** -- This method looks at a string and if it is a reasonably valid roman numeral (i.e., limited to the 7 upper-case characters used in roman numerals and follows general rules) then it will convert it to its integer equivalent. If the string is considered non-romanic, it will return nil.
|
32
|
+
|
33
|
+
|
34
|
+
### Note on usage ###
|
35
|
+
|
36
|
+
This gem has a set of basic unit tests associated with it. The tests provide other examples of what is possible using this methods and may help the user further understand how this gem can be applied. The testing suite requires the minitest gem to run properly.
|
37
|
+
|
38
|
+
Also note that this gem does not allow for lower-case roman numerals. Such lower-case numerals are used in outlines and publishing, but as far as I can tell (not professing expertise here) actual roman numerals are all upper-case.
|
39
|
+
|
40
|
+
|
41
|
+
Contributing
|
42
|
+
------------
|
43
|
+
1. Fork it
|
44
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
45
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
46
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
47
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/romanic.rb
ADDED
data/lib/romanic/base.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
module Romanic
|
2
|
+
extend self
|
3
|
+
|
4
|
+
# some initial setup is necessary...
|
5
|
+
IS_ROMAN = /^(?=.)M*(D?C{0,3}|C[DM])(L?X{0,3}|X[LC])(V?I{0,3}|I[VX])$/
|
6
|
+
|
7
|
+
NUMERAL_ARABIC = [ 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
|
8
|
+
NUMERAL_ROMANS = %w(M CM D CD C XC L XL X IX V IV I)
|
9
|
+
|
10
|
+
# Create my lookup array from previous two with zip()
|
11
|
+
ROMAN_LOOKUP = NUMERAL_ROMANS.zip(NUMERAL_ARABIC)
|
12
|
+
|
13
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Romanic
|
2
|
+
module RomanicIntegers
|
3
|
+
# Convert integers to roman numerals
|
4
|
+
def to_roman
|
5
|
+
number = self
|
6
|
+
return nil unless number.class == Fixnum
|
7
|
+
return nil unless number > 0 and number < 5000
|
8
|
+
ROMAN_LOOKUP.inject("") do |result, (roman, arabic)|
|
9
|
+
# divmod() returns array of quotient and modulus
|
10
|
+
count, number = number.divmod(arabic)
|
11
|
+
result << (roman * count)
|
12
|
+
result
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# Add methods to the Integer class
|
19
|
+
Integer.send :include, Romanic::RomanicIntegers
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Romanic
|
2
|
+
module RomanicStrings
|
3
|
+
# Convert roman numerals to integers
|
4
|
+
def from_roman
|
5
|
+
string = self
|
6
|
+
return nil unless string.to_s.match(IS_ROMAN)
|
7
|
+
ROMAN_LOOKUP.inject(0) do |result, (roman, arabic)|
|
8
|
+
while string.index(roman) == 0
|
9
|
+
result += arabic
|
10
|
+
string.slice!(roman)
|
11
|
+
end
|
12
|
+
result
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# Add methods to the String class
|
19
|
+
String.send :include, Romanic::RomanicStrings
|
data/romanic.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/romanic/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Klingon Code Warrior"]
|
6
|
+
gem.email = ["profh@cmu.edu"]
|
7
|
+
gem.description = %q{Converts strings and integers to and from roman numerals}
|
8
|
+
gem.summary = %q{This gem adds a method to the Integer class called to_roman, which converts an integer between 1 and 5000 to roman numerals. It also adds a method to String called from_roman which converts roman numerals to integers.}
|
9
|
+
gem.homepage = "https://github.com/profh/romanic"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "romanic"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Romanic::VERSION
|
17
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require "romanic"
|
2
|
+
require "minitest/autorun"
|
3
|
+
|
4
|
+
class TestRoman < MiniTest::Unit::TestCase
|
5
|
+
|
6
|
+
def test_conversion_to_roman_numerals_works
|
7
|
+
assert_equal('I', 1.to_roman)
|
8
|
+
assert_equal('II', 2.to_roman)
|
9
|
+
assert_equal('IV', 4.to_roman)
|
10
|
+
assert_equal('XIV', 14.to_roman)
|
11
|
+
assert_equal('XLIX', 49.to_roman)
|
12
|
+
assert_equal('C', 100.to_roman)
|
13
|
+
assert_equal('MCMXCIX', 1999.to_roman)
|
14
|
+
assert_equal('MMMMCMXCIX', 4999.to_roman)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_out_of_bounds_conversions_to_roman_fail
|
18
|
+
assert_equal(nil, 10001.to_roman)
|
19
|
+
assert_equal(nil, 5000.to_roman)
|
20
|
+
assert_equal(nil, 0.to_roman)
|
21
|
+
assert_equal(nil, -3.to_roman)
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_string_conversions_from_roman_works
|
25
|
+
assert_equal(1, 'I'.from_roman)
|
26
|
+
assert_equal(2, 'II'.from_roman)
|
27
|
+
assert_equal(4, 'IV'.from_roman)
|
28
|
+
assert_equal(14, 'XIV'.from_roman)
|
29
|
+
assert_equal(49, 'XLIX'.from_roman)
|
30
|
+
assert_equal(100, 'C'.from_roman)
|
31
|
+
assert_equal(1999, 'MCMXCIX'.from_roman)
|
32
|
+
assert_equal(4999, 'MMMMCMXCIX'.from_roman)
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_non_roman_strings_fail
|
36
|
+
assert_equal(nil, 'ii'.from_roman)
|
37
|
+
assert_equal(nil, 'LKD'.from_roman)
|
38
|
+
assert_equal(nil, '2010'.from_roman)
|
39
|
+
assert_equal(nil, 'XX!'.from_roman)
|
40
|
+
assert_equal(nil, 'IIII'.from_roman)
|
41
|
+
assert_equal(nil, 'IIIII'.from_roman)
|
42
|
+
assert_equal(nil, 'XIIII'.from_roman)
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: romanic
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Klingon Code Warrior
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-05-01 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Converts strings and integers to and from roman numerals
|
15
|
+
email:
|
16
|
+
- profh@cmu.edu
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/romanic.rb
|
27
|
+
- lib/romanic/base.rb
|
28
|
+
- lib/romanic/romanic_integers.rb
|
29
|
+
- lib/romanic/romanic_strings.rb
|
30
|
+
- lib/romanic/version.rb
|
31
|
+
- romanic.gemspec
|
32
|
+
- tests/romanic_tests.rb
|
33
|
+
homepage: https://github.com/profh/romanic
|
34
|
+
licenses: []
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ! '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
requirements: []
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 1.8.24
|
54
|
+
signing_key:
|
55
|
+
specification_version: 3
|
56
|
+
summary: This gem adds a method to the Integer class called to_roman, which converts
|
57
|
+
an integer between 1 and 5000 to roman numerals. It also adds a method to String
|
58
|
+
called from_roman which converts roman numerals to integers.
|
59
|
+
test_files: []
|