NilClass、TrueClass 和 FalseClass 的目的是什么

发布于 2024-12-14 04:51:29 字数 295 浏览 1 评论 0原文

NilClassTrueClassFalseClass 各有一个实例,即 niltruefalse,这些都是常量,拥有这些类的目的是什么?为什么它们不能是 Object 类的实例,并且所有相关方法都可以简单地定义为 niltrue上的单例方法假的?一个相关的问题是,为什么这些不被定义为常量?

NilClass, TrueClass and FalseClass having one instance each, namely nil, true and false, which are constants, what is the purpose of having these classes? Why cannot they be instances of the Object class, and all the relevant methods be simply defined as singleton methods on nil, true and false? A related question is, why are these not defined as constants?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

它遵循“一切都是对象”和“对象由它们所属的类来专门化”的思想。

niltruefalse 都是对象(因此是带有方法的类的实例化)。它们是 1) 相应类型的唯一居民,并且是 2) 不可变对象允许实现优化 - 事实上,不是一个 nil 够了吗?

一条有用的错误消息,没有专门针对值:x.class“正常工作”。

> > nil.foo
> => #<NoMethodError: undefined method `foo' for nil:NilClass>

我很高兴它说 NilClass :-)

这种类实例方法还使得重新打开 NilClass ——无论好坏 ——都变得简单并且与它可能的方式一致对其他类型进行。

至少从 Ruby 1.9.2 开始,不可能重新分配 truefalsenil(Python 2.x 允许重新分配) True/False 赋值,但 Python 3.x 中没有)。请注意,由于 true/false/nil 不是常量,因此它们可以优化为 AST(或实现使用的任何内容)作为“文字值”,而无需常量外观-向上。

> > VERSION
> => "1.9.2"
> > true = false­
> => #<SyntaxError: Can't change the value of true>
> > [].each {|tru­e|}
> => #<SyntaxError: Can't change the value of true>

快乐编码。

It keeps with the idea that "everything is an object" and "objects are specialized by the classes they are instances of".

nil, true, and false are all objects (and are thus instantiations of a class with methods). The imposition that they are 1) the sole inhabitants of the respective type and are 2) immutable objects allows for implementation optimizations -- and really, isn't one nil enough?

A helpful error message without specialization for the values: x.class "just works".

> > nil.foo
> => #<NoMethodError: undefined method `foo' for nil:NilClass>

I am glad it said NilClass :-)

This class-instance approach also makes re-opening NilClass -- for better or worse -- as easy and consistent with how it might be done for other types.

At least as of Ruby 1.9.2 it is not possible to re-assign true, false or nil (Python 2.x allowed re-assignment of True/False, but does not in Python 3.x). Note that because true/false/nil are not constants they can be optimized into the AST -- or whatever the implementation uses -- as "literal values" without a constant look-up.

> > VERSION
> => "1.9.2"
> > true = false­
> => #<SyntaxError: Can't change the value of true>
> > [].each {|tru­e|}
> => #<SyntaxError: Can't change the value of true>

Happy coding.

在梵高的星空下 2024-12-21 04:51:29

Ruby 有时采用“只获取一个对象并向其添加一些单例方法”的方法

C:\Documents and Settings\a.grimm>irb
irb(main):001:0> self.methods - Object.new.methods

[:public, :private, :include, :context, :conf, :irb_quit, :exit,
:退出,:irb_print_working_workspace,:irb_cwws,:irb_pwws,:cwws,
:pwws,:irb_current_working_binding,:irb_print_working_binding,
:irb_cwb, :irb_pwb, :irb_chws, :irb_cws, :chws, :cws,
:irb_change_binding, :irb_cb, :cb, :工作空间, :irb_bindings,
:绑定,:irb_pushws,:pushws,:irb_push_binding,:irb_pushb,
:pushb, :irb_popws, :popws, :irb_pop_binding, :irb_popb, :popb,
:源,:工作,:fg,:kill,:帮助,:irb_exit,:irb_context,
:install_alias_method, :irb_current_working_workspace,
:irb_change_workspace,:irb_workspaces,:irb_push_workspace,
:irb_pop_workspace, :irb_load, :irb_require, :irb_source, :irb,
:irb_jobs、:irb_fg、:irb_kill、:irb_help]

我不知道为什么他们不使用 truefalsenil 来执行此方法代码>.也许是因为人们需要理解这些对象(根据 pst 的答案),而人们不需要理解“主要”(?)对象。

Ruby takes the approach of "just take an object and add some singleton methods to it" some of the time:

C:\Documents and Settings\a.grimm>irb
irb(main):001:0> self.methods - Object.new.methods

gives

[:public, :private, :include, :context, :conf, :irb_quit, :exit,
:quit, :irb_print_working_workspace, :irb_cwws, :irb_pwws, :cwws,
:pwws, :irb_current_working_binding, :irb_print_working_binding,
:irb_cwb, :irb_pwb, :irb_chws, :irb_cws, :chws, :cws,
:irb_change_binding, :irb_cb, :cb, :workspaces, :irb_bindings,
:bindings, :irb_pushws, :pushws, :irb_push_binding, :irb_pushb,
:pushb, :irb_popws, :popws, :irb_pop_binding, :irb_popb, :popb,
:source, :jobs, :fg, :kill, :help, :irb_exit, :irb_context,
:install_alias_method, :irb_current_working_workspace,
:irb_change_workspace, :irb_workspaces, :irb_push_workspace,
:irb_pop_workspace, :irb_load, :irb_require, :irb_source, :irb,
:irb_jobs, :irb_fg, :irb_kill, :irb_help]

I don't know why they don't do this approach with true, false or nil. Maybe because people need to understand these objects (as per pst's answer), whereas people don't need to understand the "main" (?) object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文