0

What's the scope of a variable in a piece of code that has been require'd?

e.g. say I have a piece of code like this in a file called users.rb:

users = ...

and this line is not in a class or in a method. I then require this file. E.g. in app.rb

require './users.rb'

What's the scope of that variable (users) and how would I access it?

Snowcrash
  • 80,579
  • 89
  • 266
  • 376

1 Answers1

1

Variables which start with a lower case letter are local variables. Local variables are called local variables because they are local to the scope they are defined in and cannot be accessed from a different scope.

What's the scope of a variable in a piece of code that has been require'd?

In your particular case, the scope is the script body of users.rb.

how would I access it?

You can't. That's the whole purpose of local variables.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653