1
module Mod
  var = 2 
end

class Person
  include Mod
  puts var
end

Simple question. Why can't i access a local variable imported from a module?

2 Answers 2

5

Because it's a local variable. That's what they're there for. Local variables are local to the lexical scope they are defined in. That's why they are called local variables.

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

Comments

3

When you are including a module inside another module or class, Ruby’s default implementation is to add the constants, methods, and module variables of this module to mod(where you are including the module) if this module has not already been added to mod or one of its ancestors.

In your case var is a local variable of module Mod, so it can't be included inside the class Person, by the line include Mod.

Comments

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.