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?
1 Answer
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
3 Comments
Sascha Manns
Can i also give two variables? Like other_method(var1)(var2)?
Marek Lipka
@saigkill yes, you can,
other_method(var1, var2). I advice you to start from some basics before you start programming in Ruby.tessi
@saigkill Marek is right. I've updated my answer with a link explaining methods in ruby. I hope that helps :)
Scopefrom here with example