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









> 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.

-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to