Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue calling method in short example

Tags:

ruby

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
like image 246
gingervitiis Avatar asked Mar 20 '26 14:03

gingervitiis


1 Answers

#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
like image 173
Tau-fan Ar-if Avatar answered Mar 22 '26 12:03

Tau-fan Ar-if