how it works with Proc object?

def test
  yield
end

test do
  p "a"
end

i don`t know how yield function does in detail. why did i write yield
and then the p 'a' works? i know that it is a Proc object , but i
don`t know how it works. Is Proc do as a parameter?
test do
  p "a"
end
and is this code which makes the output?
thanks a lot.

All Ruby methods have an invisible implicit block parameter. That
means that the signature of #test really looks like this:

def test(&block)
  # ...
end

When you write "yield", you're saying, "call the block that was passed
to this method", so #test looks like this:

def test(&block)
  call &block and evaluate it
end

That means that if you call #test with { p "a" }, then #test's
implementation is effectively

def test
  p "a"
end

~ jf

That means that if you call #test with { p "a" }, then #test's
implementation is effectively

def test
  p "a"
end

With the difference that the block executes with access to the local
variables, value of self etc. that were present where the block was
defined

Fred

felix wrote in post #1020377:

def test
  yield
end

yield() means: execute the block that is specified in the method call.

test do
  p "a"
end

That syntax calls test() and everything after 'do' is a block, which
ruby automatically passes to the method.

Note that your definition of test() requires that a block be specified
when calling test():

def test
  yield
end

test()

--output:--
ruby.rb:2:in `test': no block given (yield) (LocalJumpError)
  from ruby.rb:5:in `<main>'

Typically test() would be defined like this:

def test
  if block_given?
    yield
  else #do something else
    puts "goodbye"
  end
end

test()

--output:--
goodbye