From 792e55ba1a28934060ac8534a3ef61bd8ccd8da4 Mon Sep 17 00:00:00 2001 From: Erwin Boskma Date: Tue, 24 Feb 2015 22:00:02 +0100 Subject: [PATCH 1/3] Extra methods --- lib/team_ferwin.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/team_ferwin.rb b/lib/team_ferwin.rb index 90ea37d..e355824 100644 --- a/lib/team_ferwin.rb +++ b/lib/team_ferwin.rb @@ -51,6 +51,14 @@ def *(other) self.to_i * other.to_i end + def %(other) + self.to_i * other.to_i + end + + def **(other) + self.to_i * other.to_i + end + private def arabic(token) ROMAN_NUMERALS[token] From ba35161111aaa457abcacbced076baa0451793de Mon Sep 17 00:00:00 2001 From: Erwin Boskma Date: Tue, 24 Feb 2015 22:02:03 +0100 Subject: [PATCH 2/3] Kleine fix --- lib/team_ferwin.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/team_ferwin.rb b/lib/team_ferwin.rb index e355824..528cf26 100644 --- a/lib/team_ferwin.rb +++ b/lib/team_ferwin.rb @@ -52,11 +52,11 @@ def *(other) end def %(other) - self.to_i * other.to_i + self.to_i % other.to_i end def **(other) - self.to_i * other.to_i + self.to_i ** other.to_i end private From 80b08100123576044df72d6e9ecb320d995b3cd4 Mon Sep 17 00:00:00 2001 From: Erwin Boskma Date: Tue, 24 Feb 2015 22:54:01 +0100 Subject: [PATCH 3/3] Now with Roman Numerals --- lib/team_ferwin.rb | 51 +++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/lib/team_ferwin.rb b/lib/team_ferwin.rb index 528cf26..52b00e8 100644 --- a/lib/team_ferwin.rb +++ b/lib/team_ferwin.rb @@ -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 @@ -32,35 +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 - end - - def %(other) - self.to_i % other.to_i - 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