今天看到一段代码
#! cruby 1.9
lam = lambda do |(a,b),c|
#blahblah
end
它看起来等于
lam = lambda do |l,c|
a,b = *l
#blahblah
end
这个语法有“官方名称”吗?
最佳答案
是的,它叫做destructuring .
So what is destructuring? The most concise definition I found is from Common Lisp the Language. Destructuring allows you to bind a set of variables to a corresponding set of values anywhere that you can normally bind a value to a single variable. It is a powerful feature of Clojure that lets you write some very elegant code. For more information about Clojure's features, I recommend you check out Jay Field's blog post on the subject. While destructuring in Ruby is not quite as powerful as Clojure, you can still do some cool stuff.
关于ruby:此语法的名称,它拆分块的位置参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14169605/