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.
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.
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.
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.
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…
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.