How do you access variables which are defined in an included file?
# inc.rb
foo = "bar";
# main.rb
require 'inc.rb'
puts foo
# NameError: undefined local variable or method `foo' for main:Object
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can’t access a local outside of the scope it was defined in — the file in this case. If you want variables that cross file boundaries, make them anything but locals.
$foo,Fooand@foowill all work.If you just really don’t want to put any sort of decoration on the symbol (because you don’t like the way it reads, maybe), a common hack is just to define it as a method:
def foo() "bar" end.