0

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
  • Learn about Scope from here with example Commented Apr 22, 2015 at 9:31

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Can i also give two variables? Like other_method(var1)(var2)?
@saigkill yes, you can, other_method(var1, var2). I advice you to start from some basics before you start programming in Ruby.
@saigkill Marek is right. I've updated my answer with a link explaining methods in ruby. I hope that helps :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.