Andreas Secret
2008-Jan-21 10:04 UTC
What does the '' & '' mean when associated to a block?
I have a function that is defined as such: def rubyfunc( &block ) ... end Its called by "interface.rubyfunc" (Where obviously interface is a class) Can anyone give me an explanation of what the "&"-sign is and in general what the parameter is when I call it from an empty function? -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Keynan Pratt
2008-Jan-21 10:32 UTC
Re: What does the '' & '' mean when associated to a block?
the syntax of ''&'' comes from the c/c++ lang and means that the variable ''block'' is not a parameter in the normal list. ''block'' would take a ruby variable while ''&block'' takes a block of code The function would be called like this rubyfunc do |some_param| puts param.to_s end where the do/end is becomes a Proc object and gets stored in the &block arg & must be used on the last argument in the function definition. An instance of a Proc object can be executed via <var_name>.call(<args>) note that the do/end can be substituted with {} but should only be used on single line blocks ie. rubyfunc{ puts ''hello'' } -K p.s. this is a ruby syntax question and should be posted on the ruby forum in the future. -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---