Use Proc.new, proc and lamda to create methods : Procs « Method « Ruby






Use Proc.new, proc and lamda to create methods


#!/usr/local/bin/ruby

count = Proc.new { [1,2,3,4,5].each do |i| print i end; puts }
your_proc = lambda { puts "L?" }
my_proc = proc { puts "?" }

# What kind of objects did you just create?
puts count.class, your_proc.class, my_proc.class

# Calling all procs
count.call
your_proc.call
my_proc.call

 








Related examples in the same category

1.A Proc is an object that holds a chunk of code.
2.Proc object picks up the surrounding environment when it is created.
3.A quicker way to create our hello Proc
4.Ruby lets you store procedures or procs, as they're called as objects, complete with their context.
5.coax a method to convert an associated block to a proc on the fly.
6.Use proc to create a function
7.Use Proc.new to create a new method