In the following how should I call this method properly?
Here's the exmaple where I try to call randomMethod() and an error throws saying it is undefined.
def randomMethod()
rand1 = rand(2)
if rand1 == 1
rand2 = rand(1..25)
puts rand2
puts ""
else
rand2 = 0
puts rand2
puts ""
end
end
x = 99
while x > 0
randomMethod()
x - rand2
end
#use this kind of convention about name of function
def random_method()
#always initialize your varialble first before implement to avoid errors
rand1 = rand(2)
rand2 = 0
if rand1 == 1
rand2 = rand(1..25)
end
#don't repeat yourself
puts rand2
puts ""
#pick a return just to make sure you do not mistaken
return rand2
end
x = 99
while x > 0
rand2=random_method()
x - rand2
x = x - 1
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With