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?

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()

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs