How to create procedure and passing argument as code block ??

In Ruby a Proc is an object that holds a chunk of code. We can create Proc by ” lambda( )” method. To call the code block we have to use call method of Proc.

And if we want this Proc object to pass as code block argument then we have to append an ‘&’ with Proc name.

One thing to remember that we have to associate an ‘&’  with the parametername to pick up any code block in the method definition. The whole thing is as follows —-

class Report

def output_report (&Outputblock)
Outputblock.call(  )
end

end

# code-block here with Proc

hello = lambda do

puts('Hello')
puts('I am inside a proc')
end

report = Report .new

report.output_report( &hello)

————————————————————-

 

In the above code “hello” is the created procedure.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: