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. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 -- John Feminella Principal Consultant, BitsBuilder LI: http://www.linkedin.com/in/johnxf SO: http://stackoverflow.com/users/75170/ On Tue, Sep 6, 2011 at 07:52, felix <guofuchunlh-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sep 6, 1:47 pm, John Feminella <jo...-u89qwezJ71hz+5FpPkU+UQ@public.gmane.org> wrote:> > 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> > ~ jf > -- > John Feminella > Principal Consultant, BitsBuilder > LI:http://www.linkedin.com/in/johnxf > SO:http://stackoverflow.com/users/75170/ > > > > > > > > On Tue, Sep 6, 2011 at 07:52, felix <guofuchu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > 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. > > > -- > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.