加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 如果没有块参数,`do` …`end`语句如何工作?

发布时间:2020-12-17 02:48:03 所属栏目:百科 来源:网络整理
导读:例如,在Rails gemfile中: group :development,:test do gem 'rspec-rails','~ 2.0'end do … end声明怎么回事?并与rspec: describe "Foo" do context "bar" do expect... endend do … end-s创建一个块,其间的信息正在其他地方使用?如果没有设置块参数,
例如,在Rails gemfile中:

group :development,:test do
  gem 'rspec-rails','~> 2.0'
end

do … end声明怎么回事?并与rspec:

describe "Foo" do
  context "bar" do
     expect...
  end
end

do … end-s创建一个块,其间的信息正在其他地方使用?如果没有设置块参数,它如何工作?

解决方法

这称为域特定语言

A Domain-Specific Language,or DSL,is “a programming language of limited expressiveness focused on a particular domain”. It makes tasks in its domain easier by removing extraneous code for a particular task and allowing you to focus on the specific task at hand. It also helps other people read the code,because the purpose of the code is so clear.

基本上这个

group :development,'~> 2.0'
end

只是调用方法组的参数:development,:test和block gem’rspec-rails’,’?> 2.0′ . Which is defined in this way in Bundler DSL:

def group(*args,&blk)
  ...
end

第二个例子也是如此.

DSL defines methods to group examples,most notably describe,and exposes them as class methods of RSpec.

.describe is being implemented in this way:

def describe(doc_string,*metadata_keys,metadata = {},&example_implementation)
  ...
end

您可以在Ruby in this thoughtbot.com article中阅读有关编写特定于域的语言的更多信息

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读