Search code examples
ruby-on-railsrubymodulemixinsinstance-variables

Ruby mixins working with instance variables


One of classes in an application is grown too much and I would like to group it's methods in some way.

One of ways could be to split Ruby class source into multiple files.

Ruby has a native mechanism to do this called mixins, but mixins are just isolated pieces of code which could be linked to any class. Therefore here is the question:

What is a drawback of working from mixins with instance-wide variables (for example @socket in my case)? Can mixins freely work with these variables?


Solution

  • Since ruby's variables are not declared, but are implicit, and ruby as a whole is duck-typed, there is no problem for a class/module to assume that there is a variable names @something, and it will work correctly, as long as it is there.

    So the answer to your question is - yes, mixins freely work with shared instance variables.