MisterCal - 5 months ago 19

Ruby Question

New to ruby , apologize in advance. Im struggling with this example from a learn to program tutorial. I'm still not sure what "write" means exactly ..is it the input number divided by 100? if so how does write>0 return 100. If anyone can explain at least the first portion of this example with clarity it would ve very helpful.

`We'll call this one englishNumber. It will take a number, like 22, and return the english version of it (in this case, the string 'twenty-two'). For now, let's have it only work on integers from 0 to 100.`

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

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

puts englishNumber( 0)

puts englishNumber( 9)

puts englishNumber( 10)

puts englishNumber( 11)

puts englishNumber( 17)

puts englishNumber( 32)

puts englishNumber( 88)

puts englishNumber( 99)

puts englishNumber(100)

Answer

The comments tell the story, even if the reuse of variables can become a little confusing as the algorithm progresses: `write`

holds the value that will be used to determine what part of the number you're writing longform.

What's misleading at first is that it doesn't take numbers over 100 (it exits early if that's the case), but talks about "how many hundreds are left". This is misleading: the only way this affects the algorithm is if it's *exactly* a hundred, which will make `write = 1`

and short-circuit the algorithm:

```
if write > 0
return 'one hundred'
end
```

To make the mechanics clear, let's go through it with four inputs: -1, 0, 42, 101.

When we call `englishNumber -1`

, the method will hit the first condition and determine that `number`

is smaller than `0`

, returning `'Please enter a number zero or greater.'`

.

When we call `englishNumber 0`

, the method will pass the first two conditions. It will then hit a series of conditions that are only met by numbers larger than `0`

, which won't change `numberString`

, ending up here:

```
if numString == ''
# The only way "numString" could be empty is if
# "number" is 0.
return 'zero'
end
```

When we call `englishNumber 42`

, the first part of the algorithm will check how many hundreds are left in that number:

```
write = number / 100
```

Replacing number with `42`

:

```
write = 42 / 100 # => 0
```

And `left`

:

```
left = left - write*100
left = 42 - 0*100
left = 42
```

Since there are no hundreds left (`write = 0`

), the algorithm moves to checking how many tens are left to write out. With `42`

:

```
write = number/10
write = 42/10
write = 4
left = left - write*10
left = 42 - 40
left = 2
```

That value for `write`

will hit the case at `elsif write == 4`

and add `forty`

to the end of `numString`

. `left`

, being `2`

, will be evaluated and added to `numString`

at the next check of `write`

and `left`

, below.

With `englishNumber 101`

, the method will hit the second condition and determine that `number`

is greater than `0`

, returning `'Please enter a number 100 or lesser.'`

.

Source (Stackoverflow)

Comments