Skip to content
This repository was archived by the owner on Mar 26, 2019. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 28 additions & 15 deletions lib/team_ferwin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@ class TeamFerwin

ROMAN_NUMERALS = {
"I" => 1,
"IV" => 4,
"V" => 5,
"IX" => 9,
"X" => 10,
"XV" => 40,
"L" => 50,
"XC" => 90,
"C" => 100,
"CD" => 400,
"D" => 500,
"CM" => 900,
"M" => 1000
}
# Use this method to convert roman numbers to arabic
#
# Don't you dare cheat on spec!!!
def initialize(roman)
def initialize(roman, mode)
@roman_numeral_tokens = roman
@mode = mode.to_sym
end

def to_i
Expand All @@ -32,27 +39,33 @@ def to_i
end

def to_roman(num)
roman = ""
ROMAN_NUMERALS.each_pair.to_a.reverse.each do |token, digit|
while num >= digit do
roman << token
num -= digit
end
end

roman
end

def +(other)
self.to_i + other.to_i
end

def -(other)
self.to_i - other.to_i
end

def /(other)
self.to_i.to_f / other.to_i.to_f
end

def *(other)
self.to_i * other.to_i
[:+, :-, :*, :**, :/, :%].each do |op|
define_method op do |other|
result(self.to_i.send(op, other.to_i))
end
end

private
def arabic(token)
ROMAN_NUMERALS[token]
end

def result(num)
if @mode == :roman
self.to_roman(num)
else
num
end
end
end