百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
N

naught

> 编程语言
开源

用于在 Ruby 中构建空对象类的工具包

1.1K stars0 点赞3 次浏览
访问官网GitHub

工具介绍

用于在 Ruby 中构建空对象类的工具包

快速介绍 Naught ------------------------ #### 现在这一切是什么呢? Naught 是用于在 Ruby 中构建 空对象 的工具包。 #### 这到底是什么意思? 空对象可以让你的代码更加 自信。 以下是一个不太自信的方法。 ruby class Geordi def make_it_so(logger = nil) logger && logger.info("Reversing the flux phase capacitance!") logger && logger.info("Bounding a tachyon particle beam off of Data's cat!") logger && logger.warn("Warning, bogon levels are rising!") end end 现在,请观察我们如何通过空对象模式为它注入一丝自信! ```ruby class NullLogger def debug(); end def info(); end def warn(); end def error(); end def fatal(*); end end

class Geordi def make_it_so(logger = NullLogger.new) logger.info "Reversing the flux phase capacitance!" logger.info "Bounding a tachyon particle beam off of Data's cat!" logger.warn "Warning, bogon levels are rising!" end end ``` 通过提供一个 NullLogger,该对象实现了 Logger 接口中的 [一些] 为空操作,我们摆脱了那些不雅观的 && 运算符。 #### 这很简单。 我为什么需要为它创建一个库呢? 你不需要!空对象模式在核心上非常简单。

然而,我们现在在这里……

[][gem] [][test] [][lint] [][docs] [][typecheck] [gem]: https://rubygems.org/gems/naught [test]: https://github.com/avdi/naught/actions/workflows/test.yml [lint]: https://github.com/avdi/naught/actions/workflows/lint.yml [docs]: https://github.com/avdi/naught/actions/workflows/docs.yml [typecheck]: https://github.com/avdi/naught/actions/workflows/typecheck.yml

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言