问题描述
我喜欢ruby的一件事是,它主要是一种易读的语言(这对于自记录代码非常有用)
One thing I love about ruby is that mostly it is a very readable language (which is great for self-documenting code)
但是,受到以下问题的启发:解释了Ruby代码以及 || =
在ruby中如何工作的描述,我在考虑我不使用的ruby习语,坦率地说,我没有完全理解它们.
However, inspired by this question: Ruby Code explainedand the description of how ||=
works in ruby, I was thinking about the ruby idioms I don't use, as frankly, I don't fully grok them.
因此,我的问题是,与所引用问题的示例类似,要成为一名真正的红宝石程序员,我需要知道哪些常见但不明显的红宝石习语?
So my question is, similar to the example from the referenced question, what common, but not obvious, ruby idioms do I need to be aware of to be a truly proficient ruby programmer?
顺便说一下,从引用的问题开始
By the way, from the referenced question
a ||= b
等同于
if a == nil || a == false
a = b
end
(感谢Ian Terrell的更正)
(Thanks to Ian Terrell for the correction)
事实证明,这一点并非完全没有争议.实际上,正确的扩展是
It turns out this point is not totally uncontroversial. The correct expansion is in fact
(a || (a = (b)))
请查看以下链接以了解原因:
See these links for why:
- http://DABlog.RubyPAL.Com/2008/3/25/a-short-circuit-edge-case/
- http://DABlog.RubyPAL.Com/2008/3/26/short-circuit-post-correction/
- http://ProcNew.Com/ruby-short-circuit-edge-case-response.html
感谢JörgW Mittag指出了这一点.
Thanks to Jörg W Mittag for pointing this out.
推荐答案
神奇的if子句,可将同一文件用作库或脚本:
The magic if clause that lets the same file serve as a library or a script:
if __FILE__ == $0
# this library may be run as a standalone script
end
打包数组:
# put the first two words in a and b and the rest in arr
a,b,*arr = *%w{a dog was following me, but then he decided to chase bob}
# this holds for method definitions to
def catall(first, *rest)
rest.map { |word| first + word }
end
catall( 'franken', 'stein', 'berry', 'sense' ) #=> [ 'frankenstein', 'frankenberry', 'frankensense' ]
哈希的合成糖作为方法参数
The syntatical sugar for hashes as method arguments
this(:is => :the, :same => :as)
this({:is => :the, :same => :as})
哈希初始化程序:
# this
animals = Hash.new { [] }
animals[:dogs] << :Scooby
animals[:dogs] << :Scrappy
animals[:dogs] << :DynoMutt
animals[:squirrels] << :Rocket
animals[:squirrels] << :Secret
animals #=> {}
# is not the same as this
animals = Hash.new { |_animals, type| _animals[type] = [] }
animals[:dogs] << :Scooby
animals[:dogs] << :Scrappy
animals[:dogs] << :DynoMutt
animals[:squirrels] << :Rocket
animals[:squirrels] << :Secret
animals #=> {:squirrels=>[:Rocket, :Secret], :dogs=>[:Scooby, :Scrappy, :DynoMutt]}
元类语法
x = Array.new
y = Array.new
class << x
# this acts like a class definition, but only applies to x
def custom_method
:pow
end
end
x.custom_method #=> :pow
y.custom_method # raises NoMethodError
类实例变量
class Ticket
@remaining = 3
def self.new
if @remaining > 0
@remaining -= 1
super
else
"IOU"
end
end
end
Ticket.new #=> Ticket
Ticket.new #=> Ticket
Ticket.new #=> Ticket
Ticket.new #=> "IOU"
块,proc和lambda.活着呼吸.
Blocks, procs, and lambdas. Live and breathe them.
# know how to pack them into an object
block = lambda { |e| puts e }
# unpack them for a method
%w{ and then what? }.each(&block)
# create them as needed
%w{ I saw a ghost! }.each { |w| puts w.upcase }
# and from the method side, how to call them
def ok
yield :ok
end
# or pack them into a block to give to someone else
def ok_dokey_ok(&block)
ok(&block)
block[:dokey] # same as block.call(:dokey)
ok(&block)
end
# know where the parentheses go when a method takes arguments and a block.
%w{ a bunch of words }.inject(0) { |size,w| size + 1 } #=> 4
pusher = lambda { |array, word| array.unshift(word) }
%w{ eat more fish }.inject([], &pusher) #=> ['fish', 'more', 'eat' ]
这篇关于常见的Ruby成语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!