General Syntax question, Definiting Methods

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 = 1, V = 5, X = 10, L = 50, C =100, D = 500, M = 1000

puts ‘pick a number to convert’
num = gets.to_i

def roman num
roman = ‘’ #assuming creating roman variable that’s empty
roman = roman + ‘M’ * (num/1000) #adding Ms to roman variable
roman = roman + ‘D’ * (num%1000/500)
roman = roman + ‘C’ * (num%500/100)
roman = roman + ‘L’ * (num%100/50)
roman = roman + ‘X’ * (num%50/10)
roman = roman + ‘V’ * (num%10/5)
roman = 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 = ‘’” and the other lines “roman = 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.

Hey Richard,

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

roman = ‘’
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

Richard D. wrote in post #1078909:

def roman num
roman = ‘’ #assuming creating roman variable that’s empty
roman = roman + ‘M’ * (num/1000) #adding Ms to roman variable
roman = roman + ‘D’ * (num%1000/500)
roman = roman + ‘C’ * (num%500/100)
roman = roman + ‘L’ * (num%100/50)
roman = roman + ‘X’ * (num%50/10)
roman = roman + ‘V’ * (num%10/5)
roman = roman + ‘I’ * (num%5/1)

roman # don’t understand purpose of line
endhanks for the help in advance.

Hi,

By the way, I wouldn’t use a local variable with the same name as the
method. That’s quite confusing and forces you to use paranthesis or an
explicit receiver when you actually do want to call the method.

And I would replace the pattern

sum = sum + …

with the short form

sum += …

That’s shorter and (in my opionion) more readable.

On Sun, Oct 7, 2012 at 9:39 PM, Richard D. [email protected] wrote:

roman # don’t understand purpose of line
end

[snip]

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.

The method returns the result of the last statement. So, in your case,
you could leave it out. But you want to get in the habit of
specifically returning what you want, because some statements do not
return what you expect…

def remove_from object_array object
object_array.delete(object)
end

…returns the object deleted, not the new array.

def remove_from object_array object
object_array.delete(object)
object_array
end

…returns the new array

Putting object where object_array is to return the object and not the
array may seem redundant, but sacrifices little to no overhead or
brevity for clarity.

Todd B. wrote in post #1078942:

The method returns the result of the last statement. So, in your case,
you could leave it out. But you want to get in the habit of
specifically returning what you want, because some statements do not
return what you expect…

Isn’t that exactly what Troy already said?

On Mon, Oct 8, 2012 at 8:25 AM, Jan E. [email protected] wrote:

Todd B. wrote in post #1078942:

The method returns the result of the last statement. So, in your case,
you could leave it out. But you want to get in the habit of
specifically returning what you want, because some statements do not
return what you expect…

Isn’t that exactly what Troy already said?

Yes, I guess I glossed over that sentence in Troy’s reply. In any
case, it doesn’t hurt to give an example.

cheers,
Todd

Todd B. wrote in post #1078942:

def remove_from object_array object
object_array.delete(object)
end

…returns the object deleted, not the new array.

def remove_from object_array object
object_array.delete(object)
object_array
end

…returns the new array

Putting object where object_array is to return the object and not the
array may seem redundant

Actually it’s not the same; consider the case where the object being
deleted doesn’t exist in the array.

[1,2,3].delete(4)
=> nil