I'm still going through Chris Pine's book on Ruby programming and came
across the following exercise. To put it simply, I just don't
understand how this code works.
def englishNumber number
# We only want numbers from 0-100.
if number < 0
return 'Please enter a number zero or greater.'
end
if number > 100
return 'Please enter a number 100 or lesser.'
end
numString = '' # This is the string we will return.
# "left" is how much of the number we still have left to write out.
# "write" is the part we are writing out right now.
# write and left... get it?
left = number
write = left/100 # How many hundreds left to write out?
left = left - write*100 # Subtract off those hundreds.
if write > 0
return 'one hundred'
end
QUESTION 1: For example, if I choose 99 as my number, left = number, and
write = left/100, wouldn't write = 99/100? In that case, why would
write > 0 return 'one hundred'?
write = left/10 # How many tens left to write out?
left = left - write*10 # Subtract off those tens.
if write > 0
if write == 1 # Uh-oh...
# Since we can't write "tenty-two" instead of "twelve",
# we have to make a special exception for these.
if left == 0
numString = numString + 'ten'
elsif left == 1
numString = numString + 'eleven'
elsif left == 2
numString = numString + 'twelve'
elsif left == 3
numString = numString + 'thirteen'
elsif left == 4
numString = numString + 'fourteen'
elsif left == 5
numString = numString + 'fifteen'
elsif left == 6
numString = numString + 'sixteen'
elsif left == 7
numString = numString + 'seventeen'
elsif left == 8
numString = numString + 'eighteen'
elsif left == 9
numString = numString + 'nineteen'
end
# Since we took care of the digit in the ones place already,
# we have nothing left to write.
left = 0
elsif write == 2
numString = numString + 'twenty'
elsif write == 3
numString = numString + 'thirty'
elsif write == 4
numString = numString + 'forty'
elsif write == 5
numString = numString + 'fifty'
elsif write == 6
numString = numString + 'sixty'
elsif write == 7
numString = numString + 'seventy'
elsif write == 8
numString = numString + 'eighty'
elsif write == 9
numString = numString + 'ninety'
end
if left > 0
numString = numString + '-'
end
end
write = left # How many ones left to write out?
left = 0 # Subtract off those ones.
if write > 0
if write == 1
numString = numString + 'one'
elsif write == 2
numString = numString + 'two'
elsif write == 3
numString = numString + 'three'
elsif write == 4
numString = numString + 'four'
elsif write == 5
numString = numString + 'five'
elsif write == 6
numString = numString + 'six'
elsif write == 7
numString = numString + 'seven'
elsif write == 8
numString = numString + 'eight'
elsif write == 9
numString = numString + 'nine'
end
end
if numString == ''
# The only way "numString" could be empty is if
# "number" is 0.
return 'zero'
end
# If we got this far, then we had a number somewhere
# in between 0 and 100, so we need to return "numString".
numString
end
QUESTION 2: The second part of this code is just plain confusing me,
perhaps because I still can't figure out the first part which it builds
upon.
Any clarification & help would be greatly appreciated.
···
--
Posted via http://www.ruby-forum.com/.