Created
June 26, 2013 04:01
-
-
Save yeehaa123/5864682 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'minitest/spec' | |
require 'minitest/autorun' | |
describe Integer do | |
describe "#in_words" do | |
it "should return four for 4" do | |
4.in_words.must_equal "four" | |
end | |
it "should return twenty seven for 27" do | |
27.in_words.must_equal "twenty seven" | |
end | |
it "should return twenty seven for 102" do | |
102.in_words.must_equal "one hundred two" | |
end | |
it "should return two hundred thirty eight for 238" do | |
238.in_words.must_equal "two hundred thirty eight" | |
end | |
end | |
end | |
class Integer | |
def in_words | |
number_words = [ | |
[1000, "thousand"], | |
[100, "one hundred"], | |
[30, "thirty"], | |
[20, "twenty"], | |
[19, "nineteen"], | |
[18, "eighteen"], | |
[17, "seventeen"], | |
[16, "sixteen"], | |
[15, "fifteen"], | |
[14, "fourteen"], | |
[13, "thirteen"], | |
[12, "twelve"], | |
[11, "eleven"], | |
[10, "ten"], | |
[9, "nine"], | |
[8, "eight"], | |
[7, "seven"], | |
[6, "six"], | |
[5, "five"], | |
[4, "four"], | |
[3, "three"], | |
[2, "two"], | |
[1, "one"] | |
] | |
output_word = [] | |
inbetween_number = self | |
number_words.each do |number, word| | |
if inbetween_number >= number | |
output_word << word | |
inbetween_number -= number | |
end | |
end | |
output_word.join(" ") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment