Ruby 常数是什么意思?
Ruby 常量的真正含义是什么?以下代码不显示任何“constant”属性。警告是存在的,但我仍然可以更改 A 所指的内容。
A = 1
puts A # => 1
A = 2 # warning: already initialized constant A
puts A # => 2
或者 Ruby 常量只是一个指示,没有任何强制执行?
What does Ruby constants really mean? The following code doesn't show any 'constant' attribute. The warning is there, but I still get to change what A refers to.
A = 1
puts A # => 1
A = 2 # warning: already initialized constant A
puts A # => 2
Or is Ruby constants are just an indication without any enforcement?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
没错,常量 就像 ruby 中的变量一样,但如果更改它们,您会收到警告。
此外,与单纯的变量有一个区别:即使常量是在另一个类或模块中定义的,您也可以访问常量,例如给出以下代码段:
您可以通过执行
Constants::PI< 来访问
PI
/code> 而Constants::other
将不起作用。That's right, constants are just like variables in ruby, but you get a warning if you change them.
Also, there's one difference with mere variables: You can access constants even if they are defined inside another class or module, for example given this snippet:
You can reach
PI
doingConstants::PI
whileConstants::other
will not work.是的,除了打印该警告之外,不强制使用 Ruby 常量。
Yes, Ruby constants aren't enforced, other than printing that warning.
没错——分配给常量是警告,而不是错误; “常量”只是您应该如何使用某些东西的指示符,而不是您确实以这种方式使用它的规则。
这在静态编程世界中听起来可能很可怕,但它在各种元编程工具中非常有用,并且它可以实现静态语言中完全不可能的事情。
也就是说,如果您确实想确保人们不要把手放在您的引用上,您可以使用
Object#freeze
。改变引用指向的内容仍然可以;您只是无法更改引用本身的内容:但这没关系:
That's right -- assigning to a constant is a warning, not an error; "constants" are just an indicator of how you should use something, not a rule that you do use it that way.
That may sound horrendous coming from a static-programming world, but it's immensely useful in various metaprogramming facilities, and it enables things that would otherwise be completely impossible in static languages.
That said, if you really want to make sure people keep their grubby hands off your references, you can use
Object#freeze
. It's still okay to change what a reference points to with this; you just can't change the contents of the reference itself:But this is okay:
“常量”确实用词不当,Ruby 的“常量”最重要的方面不是它们的不变性,而是它们的查找规则。
请参阅:http://coderrr.wordpress.com /2008/03/11/常量名称解析-in-ruby/
"Constant" is really a misnomer, the most important aspect of Ruby's "Constants" is not their immutability but their lookup rules.
see: http://coderrr.wordpress.com/2008/03/11/constant-name-resolution-in-ruby/
常量用于存储不应更改的值。他们的名字必须以大写字母开头。按照惯例,大多数常量名称均以大写字母书写,并使用下划线作为单词分隔符,例如 SOME_CONSTANT。
类中定义的常量可以被该类的所有方法访问。那些在类外部创建的内容可以全局访问(在任何方法或类中)。
请注意,Ruby 不会阻止我们更改常量的值,它只会发出警告。
在 Ruby 中,所有类和模块名称都是常量,但约定规定它们应该以驼峰式大小写编写,例如 SomeClass。
可以使用 ::(双冒号)运算符从类外部访问常量,甚至可以在另一个类内访问常量。要从 Car 类外部访问 WHEELS 常量,我们将使用 Car::WHEELS。 :: 运算符允许从定义它们的类或模块外部访问常量、公共实例方法和类方法。
一个名为 private_constant 的内置方法使常量成为私有的(只能在创建它们的类中访问)。语法如下:
Constants are used to store values that should not be changed. Their names must start with an uppercase letter. By convention, most constant names are written in all uppercase letters with an underscore as word separator, such as SOME_CONSTANT.
Constants defined within classes can be accessed by all methods of that class. Those created outside a class can be accessed globally (within any method or class).
Note that Ruby does not stop us from changing the value of a constant, it only issues a warning.
In Ruby, all class and module names are constants, but convention dictates they should be written in camel case, such as SomeClass.
Constants can be accessed from outside the class, even within another class, by using the :: (double colon) operator. To access the WHEELS constant from outside the Car class, we would use Car::WHEELS. The :: operator allows constants, public instance methods and class methods to be accessed from outside the class or module on which they are defined.
A built-in method called private_constant makes constants private (accessible only within the class on which they were created). The syntax is as follows:
如果您来自其他编程语言,Ruby 处理常量的方式可能与您习惯的不同。一般来说,常量采用在整个应用程序中不会改变的值。语法是在命名常量时使用全部大写字母,以便应用程序知道如何处理它。例如,要设置一个常量来容纳一支棒球队,您可以这样声明:
我知道您知道这么多,请耐心听我说。通常,其他编程语言不允许您更改 TEAM 的值。但是,Ruby 不会阻止您,它会采用分配给常量的最后一个值。在上面的示例中,我可以将其值更改为:
其他编程语言会抛出错误或打印 Angels 的值。但是,Ruby 会打印值 Athletics,因为这是分配给变量 TEAM 的最后一个值。此外,它还会给出一条警告消息,指出该常量已初始化并已更改,因为更改常量被认为是不良的编程实践。但是,它仍然允许您进行更改并遵循 Ruby 约定,即相信开发人员能够做出正确的编程决策。因此,在 Ruby 中使用常量时要小心,因为它们可能会被覆盖。
If you're coming from other programming languages, Ruby handles constants differently than what you may be used to. Constants, in general, take values that do not change through the entire application. The syntax is to use all capital letters while naming your constant so that the application knows how to handle it. For example, to set a constant to hold a baseball team you would declare it this way:
I know you know this much, bear with me here. Typically, other programming languages will not allow you to change the value of TEAM. However, Ruby does not hold you back and takes the last value assigned to the constant. In the above example, I can change its value to:
Other programming languages would either throw an error or would print the value of Angels. However, Ruby prints the value Athletics because that is the last value assigned to the variable TEAM. Also, it gives a warning message that says that the constant was already initialized and was changed because changing a constant is considered a poor programming practice. But, it still allows you to make the change and follows the Ruby convention of trusting the developer to make the right programming decision. So, be careful while using constants in Ruby since they can be overridden.