- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
所以,我刚刚了解到,而不是写这样的东西:
[1,2,3,4,5].inject {|x,y| x + y} => 15
我会写
[1,2,3,4,5].inject(:+) => 15
我还了解到,而不是写作
[1,2,3,4,5].select {|x| x.even?} => [2,4]
我会写
[1,2,3,4,5].select(&:even?) => [2,4]
我的问题是为什么一个(选择)使用 &
而另一个(注入(inject))不使用。我确定 :
是因为 even?
和 +
被处理为符号,但我很想澄清为什么 &
用于一个以及为什么使用 :
。
此外,我知道我可以在 inject
和 select
之外做这些符号。
非常感谢!
最佳答案
&
运算符在这种情况下将符号转换为过程。所以代码在后台执行此操作:
[1,2,3,4,5].select {|elem| elem.send :even? } # => [2, 4]
inject
方法的实现认识到了对这些快捷方法规范的需求,并在符号作为参数传递时添加了特殊处理。因此,在这种情况下,它基本上执行 &
运算符的操作。
why one (select) uses the & and the other one (inject) doesn't
因为没有人以这种方式实现select
。 Ruby 是开源的,他们甚至可以接受你的补丁。去解决这个问题:)
P.S.:但如果由我决定,我会删除注入(inject)的特殊处理。在 Symbol#to_proc
(&
运算符使用的)的存在下感觉有点多余和困惑。
关于Ruby 试图掌握一种新的表示法。 (注入(inject)(: ) vs select(&:even?);为什么有&?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16605597/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 6年前关闭。 Improve this qu
我有实体: @Entity @Table(name = "CARDS") public class Card { @ManyToOne @JoinColumn(name = "PERSON_I
我正在尝试计算二维多边形的表面法线。我正在使用 OpenGL wiki 中的 Newell 方法来计算表面法线。 https://www.opengl.org/wiki/Calculating_a_S
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 7 年前。 Improve
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 9 年前。 Improve this
我这里有以下 XML: Visa, Mastercard, , , , 0, Discover, American Express siteonly, Buyer Pay
即将发生的 Google 政策变更迫使我们实现一个对话框,以通知欧盟用户有关 Cookie/设备标识符用于广告和分析的情况。我只想向欧盟用户显示此对话框。我不想使用额外的权限(例如 android.p
本文分享自华为云社区《华为大咖说 | 企业应用AI大模型的“道、法、术” ——道:认知篇》,作者:华为云PaaS服务小智。 本期核心观点 上车:AGI是未来5~10年内,每个人都无法回避的技
我有一个与酒精相关的网站,需要先验证年龄,然后才能让他们进入该网站。我使用 HttpModule 来执行此操作,该模块检查 cookie,如果未设置,我会将它们重定向到验证页面。我验证他们的年龄并存储
在欧盟,我们有一项法律,要求网页请求存储 cookie 的许可。我们大多数人都了解 cookie 并同意它们,但仍然被迫在任何地方明确接受它们。所以我计划编写这个附加组件(ff & chrome),它
以下在 C 和/或 C++ 中是否合法? void fn(); inline void fn() { /*Do something here*/ } 让我担心的是,第一个声明看起来暗示函数将被定义
我是一名优秀的程序员,十分优秀!