ruby:此语法的名称,它拆分块的位置参数

标签 ruby syntax

今天看到一段代码

#! 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/

相关文章:

RubyMine 无法识别我的 Ruby 安装路径

ruby - rspec 中 use_transactional_fixtures 的默认值是多少?

MySQL更新错误

C结构语法问题

PostgreSQL:获取输入参数作为行值

ruby - 无法安装 Homebrew 软件

ruby-on-rails - 如何解决 "ruby installation is missing psych"错误?

Ruby:变量是 ruby​​ 中的对象吗?

sql - 为什么给别名SQL表使用“AS”?

objective-c - 如何在 Swift 中使用 + 号连接 ObjC 文件?