简体   繁体   English

Ruby Mixins使用实例变量

[英]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类源分割成多个文件。

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. Ruby有一个本机机制来执行此操作,称为mixin,但是mixin只是可以链接到任何类的孤立的代码段。 Therefore here is the question: 因此,这是一个问题:

What is a drawback of working from mixins with instance-wide variables (for example @socket in my case)? 使用具有实例范围变量(例如,在我的情况下为@socket )的mixins进行工作的缺点是什么? Can mixins freely work with these variables? mixins可以自由使用这些变量吗?

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. 由于ruby的变量没有声明,而是隐式的,并且ruby整体上是鸭子类型的,因此类/模块假定存在变量名@something并没有问题,只要它能够正常工作它在那里。

So the answer to your question is - yes, mixins freely work with shared instance variables. 因此,您的问题的答案是-是的,mixin与共享实例变量一起自由工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM