Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instance variables in modules?

Tags:

module

ruby

How is it possible that I can have instance variables in a module even though I cannot create an instance of the module? What would be the purpose of @stack in module Stacklike below?

module Stacklike   def stack     @stack ||= []   end end 
like image 316
Hommer Smith Avatar asked Mar 18 '13 14:03

Hommer Smith


People also ask

Can Ruby modules have instance variables?

Explanation: Yes, Module instance variables are present in the class when you would include them inside the class.

What are instance variables examples?

Instance variables hold values that must be referenced by more than one method, constructor or block, or essential parts of an object's state that must be present throughout the class. Instance variables can be declared at the class level before or after use. Access modifiers can be given for instance variables.

What are instance variables?

An instance variable is a variable which is declared in a class but outside of constructors, methods, or blocks. Instance variables are created when an object is instantiated, and are accessible to all the constructors, methods, or blocks in the class. Access modifiers can be given to the instance variable.

What is instance variable in Python?

What is an Instance Variable in Python? If the value of a variable varies from object to object, then such variables are called instance variables. For every object, a separate copy of the instance variable will be created. Instance variables are not shared by objects.


2 Answers

Think of the instance variable as something which will exist in any class that includes your module, and things make a bit more sense:

module Stacklike   def stack     @stack ||= []   end    def add_to_stack(obj)     stack.push(obj)   end    def take_from_stack     stack.pop   end end  class ClownStack   include Stacklike    def size     @stack.length   end end  cs = ClownStack.new cs.add_to_stack(1) puts cs.size 

will output "1"

like image 162
mcfinnigan Avatar answered Sep 23 '22 17:09

mcfinnigan


See the below:

p RUBY_VERSION module Stacklike   def stack     @stack ||= []   end    def add_to_stack(obj)     stack.push(obj)   end    def take_from_stack     stack.pop   end end  class A include Stacklike end  a = A.new p a.instance_variables #<~~ E p a.instance_variable_defined?(:@stack) #<~~ A a.add_to_stack(10) #<~~ B p a.instance_variable_defined?(:@stack) #<~~ C p a.instance_variables #<~~ D 

Output:

"1.9.3" [] false true [:@stack] 

Explanation: Yes, Module instance variables are present in the class when you would include them inside the class. But you can see that p a.instance_variable_defined?(:@stack) is showing false as @stack is still not defined till A. At point B I defined the instance variable @stack. Thus statement in point C, outputs as true. Means module instance variables are not being created by the module itself,but that can be done by the class instances if the class included that module. Statement in E outputs [] as still that point the instance variable was not defined, but if you see the output for the line D, it is proved the @stack is inside the object a of class A.

Why such design?

This is the design or sometimes come from the requirements. Say you have been asked to write a stack operation code which will be used by two ticket booking companies,Say A and B. Now A are stack policy for their customers to serve but also they have any more formalities with that. B company also using stack policy with their own formalities which is different from A. Thus in case of designing Stack operation inside class A and class B, better idea to write it in a common place,as Both A and B have this functionality common within them. In future if another company C comes to you you can also use that module into their class, without rewriting the same functionality for each A,B and C. There can be more thoughts but hope this will help you to answer your self for your last part of the questions.

That's all about the concept. Hope it helps.

Cheers!!

like image 22
Arup Rakshit Avatar answered Sep 25 '22 17:09

Arup Rakshit