Sascha Manns
Sascha Manns

Reputation: 2510

ruby: Using local variable in methods

I have a local variable in my main program. Now i would llike to give it to a method so it can use it for it's work. What is the best way to do it? A Instance variable @test?

Upvotes: 0

Views: 336

Answers (1)

tessi
tessi

Reputation: 13574

You can give that variable as a method parameter, as shown in the example below:

def main_program
  my_variable = 3
  other_method(my_variable)
end

def other_method(special_variable)
  # do something with the special-variable
end

A nice read on how to define and call methods can be found in the Ruby programming wiki

Upvotes: 1

Related Questions