0

Is it possible to get/set instance variables of an object from inside a module which was included to the object class?

1 Answer 1

1

Do you mean:

module Foo
  def bar
    @bar ||= 0
    @bar += 1
  end
end

class Tester
  include Foo
  def baz
    @bar ||= 0
    @bar += 500
  end
end

t = Tester.new
t.bar  #=> 1
t.baz  #=> 501
t.bar  #=> 502
t.bar  #=> 503
t.baz  #=> 1003

If so, then yes. On a somewhat related note, you might also find an explanation of difference between include and extend useful.

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

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.