ruby中各种变量作用域的区别 [英] Difference between various variables scopes in ruby

查看:37
本文介绍了ruby中各种变量作用域的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ruby 有 5 个变量作用域:

Ruby has 5 variable scopes:

  1. 局部变量:这些是普通变量,例如x = 25y = gaurish,其中xy 是局部变量.
  2. 实例变量:这些在实际变量名前用@符号表示.主要与类一起使用,以便类的每个实例/对象具有不同/单独的值.例子.@employee.name = 'Alex'
  3. 类变量:在变量名前用@@符号表示.类变量,我认为所有实例/对象都具有相同的值.
  4. 全局变量:它们以 $ 符号开头,在任何地方都可以访问.示例 $LOAD_PATH
  5. 常量:必须以大写字母开头,但按照惯例以ALL_CAPS 编写.虽然它是一个常量,但它的值不是常量并且可以更改(不过 ruby​​ 会发出警告).所以从某种意义上说,这也像一个变量.
  1. Local Variables: these are the normal variables, example x = 25, y = gaurish where x and y are local variables.
  2. Instance Variables: these are denoted with @ symbol infront of the actual variable name. mainly used with classes, so that each instance/object of the class has a different/separate value. example. @employee.name = 'Alex'
  3. Class Variables: denoted with @@ symbols in front of variable name. class variable, I think have same value accos all instances/object.
  4. Global variables: they start with $ symbol and are accessible everywhere. example $LOAD_PATH
  5. Constants: Must start with Capital letter but by convention written in ALL_CAPS. although, it is a constant but its value its not constant and can be changed(ruby will throw a warning, though). so in the sense, this also acts like a variable.

正如您可能注意到的,以上所有变量都是存储某种类型的值并且它们的值可以更改的变量.但是,每个范围的作用都略有不同.有 5 种不同类型的变量范围让我很困惑.主要是,我很难决定在什么情况下,我应该在我的代码中使用特定的范围.所以我心里有一些疑问.请回答:

As you may notice,all of the above are variables which store some value of some type and their value can be changed. But, each scope does something little bit different. Having 5 different types of variable scopes is confuses hell out of me. Mainly, I have difficulty deciding under what case, I should be using a particular scope in my code. so I have some questions in my mind. please answer:

  1. 我注意到局部变量和类变量对于所有对象/实例都保持不变,这与实例变量不同.那么局部变量和类变量有什么区别呢?
  2. 可以使用局部变量代替类变量吗?或相反亦然.如果是,那为什么?如果不是,那为什么不?
  3. ruby 中的全局变量让我想起了 PHP 中邪恶的 global $x 变量.ruby 中的全局变量是否也被认为是邪恶的,因此不应使用.或者,在某些特定情况下,在 ruby​​ 中使用全局变量是有意义的?
  4. 为什么常量不是常量并且允许改变它们的值?根据定义,常量的值应该是常量,对吗?否则,我们可以只使用另一个变量而不改变它的值.这是否等同于 ruby​​ 中的 CONSTANT?
  5. 任何解释 ruby​​ 中 5​​ 个不同变量范围之间差异的页面/资源/链接?我喜欢随身携带一个以供参考.
  6. 在什么用例下,我应该在我的代码中使用特定的变量范围.所以可以用很酷的罐头示例来解释所有 5 个案例,这是我感到困惑的主要原因.
  7. 在java中是否有像public这样的事实上的选择?在大多数用例中,哪个是安全的选择?
  1. I notice that local variables and class variables stay same for all objects/instances, unlike instance variables. so what difference between Local variables and Class variables?
  2. Can local variables be used in place of class variables? or vice-versa. And if yes, then why, and if no, then why not?
  3. Global variables in ruby remind me of the evil global $x variables in PHP. Are global variables in ruby also considered evil and therefore should not be used. OR, there are specific cases where it makes sense to use global variables in ruby?
  4. Why constants are not constants and allow their value to be changed? A constant's value by definition should be constant right? else, we can just use another variable and don't change its value. would that be equivalent to a CONSTANT in ruby?
  5. Any page/resource/link which explains the difference between 5 different variable scopes in ruby? I like to keep one handy for reference.
  6. Under what use-case, I should be using a particular variable scope in my code. so one would explain all 5 cases with can example that would be cool, this is my main reason for confusion.
  7. is there a de facto choice like public in java? Which would be the safe bet in most use-cases?

感谢您抽出时间阅读并回答问题

Thanks for taking time to read and answer question

推荐答案

  1. 所有实例的类变量都是相同的,因为它们是与类相关联的类变量.一切都访问相同的变量,包括每个实例.

  1. Class variables are the same for all instances, because they're class variables–associated with the class. Everything access the same variable, including each instance.

没有.局部变量就是这样——局部.它们可能是函数的局部变量,也可能是类声明的局部变量,这与作为类变量不同.当类声明结束时,类声明中的局部变量超出范围.

No. Local variables are just that–local. They may be local to a function, or local to the class declaration, which is different than being a class variable. Locals in a class declaration go out of scope when the class declaration ends.

那是因为它们完全相同——它们是全球性的.全局状态永远是邪恶的;这不是语言或环境的属性.也就是说,可能需要一些全局状态——它就是这样.当有全局状态时使用全局状态是有意义的.诀窍是正确地使用全局状态,这有时是一项非常重要的工作.

That's because they're exactly the same–they're global. Global state is always evil; this is not a property of the language or environment. That said, some global state may be required–that's just the way it is. It makes sense to use global state when there's global state. The trick is to use global state properly, which is sometimes a non-trivial endeavor.

Ruby 就是这样.

克里斯已经给了一个.

认为这个问题主要是自我回答.当整个世界都需要访问时是全局的.特定于类实例时的实例.当仅在局部范围内需要时为本地(例如,方法、块(注意 1.8 和 1.9 之间关于块范围的差异)等).当变量不应更改时为常量.一个类变量,当它是每个实例都需要的东西,或者如果通过类方法公开,与类紧密相关的东西.

I would think this question would be largely self-answering. Global when the entire world needs access. Instance when it's specific to a class instance. Local when it's only required in a local scope (e.g., a method, a block (note differences between 1.8 and 1.9 with regard to block scope), etc.) Constant when the variable isn't supposed to change. A class variable when it's something that either every instance needs, or if exposed via a class method, something tightly associated with a class.

没有大多数用例",这完全取决于您对变量的处理方式.而 public 并不是 Java 中事实上的选择——它取决于所讨论的实体.默认的 Java 范围是包私有的(方法、属性).在 Ruby 中使用哪个完全取决于用例,请注意,与 Java 一样,在 Ruby 中甚至更容易,事情可以被规避.

There is no "most use-cases", it totally depends on what you're doing with the variable. And public isn't the de facto choice in Java–it depends on the entity in question. Default Java scope is package-private (methods, properties). Which to use in Ruby depends entirely upon the use-case, noting that as with Java, and even more easily in Ruby, things can be circumvented.

这篇关于ruby中各种变量作用域的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆