I am familiar with modules giving a class access to instance methods included in the module, but I have not seen modules giving classes access to local variables.
Here is file A
:
module SequelPlayground
class Article
attr_reader :title, :body, :author_id, :id
def initialize(attributes)
@title = attributes[:title]
@body = attributes[:body]
@author_id = attributes[:author_id]
@id = attributes[:id]
end
def self.next_id
table.count + 1
end
def self.table
DB.from(:articles) #SELECT * FROM articles
end
end
end
Here is file B
:
module SequelPlayground
DB = Sequel.postgres("sequel-playground")
class Server < Sinatra::Base
get '/' do
erb :index
end
end
end
Why does file A
have access to the local variable DB
? Are anything within a module in the same namespace even across files?