Sandip Ransing
Sandip Ransing

Reputation: 7733

What is the difference between proc{} and &proc{}

What is difference in below usage

a = proc { puts 'hii' }

def abc(&a)
  a.call
end
abc(&a)

def xyz(c)
  c.call
end
xyz(a)

In below implementation more than one blocks can be passed as arguments -

def pqr(c, &t)
  c.call
  yield
  xyz(c)
  abc(&t)
end   
pqr(a) { puts 'block to method'}

Upvotes: 1

Views: 144

Answers (1)

Linuxios
Linuxios

Reputation: 35803

In the first of the two, the &a parameter will also capture a block passed like this:

abc {puts "Hello world"}

This is the same as:

xyz(proc {puts "Hello world"})

The other of the two only allows the last of the two.

Upvotes: 2

Related Questions