Hey Richard,

So you have it mostly correct: roman =3D '' initializes the variable 'roman=
'
with the value of an empty String.  If you had code like so:

roman =3D ''
roman.class

It would return String, as in the String class.  That prevents the next
line, "roman + 'M'" from raising an error.

Methods return the last thing they evaluate.  That's why the last line of
the roman function has the roman variable.  It ensures the return value of
the roman function is the variable "roman".  In this case, you get the
same results, sure, but it's possible in some functions the last operation
does not return the thing you want.  By having the last line explicitly as
'roman' the reader knows the return value of the function is this
variable.  The line might as well be:

return roman

Except, often in Ruby, it is considered bad practice to use explicit
return statements unless absolutely necessary.

Best,
-Troy=20




On 12-10-07 10:39 PM, "Richard D." <lists / ruby-forum.com> wrote:

>I think my question is more about general syntax, than more about
>defining/creating methods.  In doing one of the excerises in Chris
>Pine's Learning to Program for Chapter 9, Creating Own Methods, one is
>to create roman number converter.  Below is the program.  I added the
>gets section.
>
># roman number converter
>
># I =3D 1, V =3D 5, X =3D 10, L =3D 50, C =3D100, D =3D 500, M =3D 1000
>puts 'pick a number to convert'
>num =3D gets.to_i
>
>def roman num
>  roman =3D '' #assuming creating roman variable that's empty
>  roman =3D roman + 'M' * (num/1000) #adding Ms to roman variable
>  roman =3D roman + 'D' * (num%1000/500)
>  roman =3D roman + 'C' * (num%500/100)
>  roman =3D roman + 'L' * (num%100/50)
>  roman =3D roman + 'X' * (num%50/10)
>  roman =3D roman + 'V' * (num%10/5)
>  roman =3D roman + 'I' * (num%5/1)
>
>  roman # don't understand purpose of line
>end
>
>puts roman num
>
>I think I get the defining method.  I just not clear on what's inside.
>Specially, the "roman =3D ''" and the other lines "roman =3D roman + ..." =
 I
>think the prior is creating a variable roman that's empty.  The later is
>stating to add how ever many roman number characters is created by
>dividing num by whatever; like a push for arrays.
>
>I don't know what the "roman" at the end of the method does.  When
>removed, I get the same results.
>
>Thanks for the help in advance.
>
>--=20
>Posted via http://www.ruby-forum.com/.
>