Ruby 的隐藏特性 [英] Hidden features of Ruby
本文介绍了Ruby 的隐藏特性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
继续......的隐藏特性"模因,让我们分享Ruby编程语言鲜为人知但有用的特性.
Continuing the "Hidden features of ..." meme, let's share the lesser-known but useful features of Ruby programming language.
尽量用核心 Ruby 来限制这个讨论,不要使用任何 Ruby on Rails 的东西.
Try to limit this discussion with core Ruby, without any Ruby on Rails stuff.
另见:
(请,每个答案只一个隐藏功能.)
(Please, just one hidden feature per answer.)
谢谢
推荐答案
从 Ruby 1.9 Proc#=== 是 Proc#call 的别名,这意味着 Proc 对象可以用于 case 语句,如下所示:
From Ruby 1.9 Proc#=== is an alias to Proc#call, which means Proc objects can be used in case statements like so:
def multiple_of(factor)
Proc.new{|product| product.modulo(factor).zero?}
end
case number
when multiple_of(3)
puts "Multiple of 3"
when multiple_of(7)
puts "Multiple of 7"
end
这篇关于Ruby 的隐藏特性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文