How does the `do` ...` end` operator work without a block parameter?

For example, in gemfile Rails:

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

what happens to the operator do... end? And using rspec:

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

are do... end-s creating a block whose information is used elsewhere? How does it work without setting a block parameter?

+4
source share
2 answers

This is called a domain language.

, , DSL, " , ". , . , .

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

group :development, :test gem 'rspec-rails', '~> 2.0'. DSL Bundler:

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

.

DSL , , RSpec.

.describe :

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

Ruby thinkbot.com

+5

. -, |...|, .

def foo &block
  block.call(:foo)
end

foo{|v| puts "I received `:foo', but am not using it."}
foo{puts "I am called with `:foo', but am not receiving it."}

, , - , |...| .

def bar &block
  block.call
end

bar{puts "I am not called with a block variable in the first place."}
+2

All Articles