1

In the book Programming Ruby: The Pragmatic Programmers Guide by Dave Thomas with Chad Fowler and Andy Hunt, regarding the creation of Procs there is a footnote that states:

"There’s actually a third, proc, but it is effectively deprecated."

I could not find which way is this. I am aware of the following ways to create a Proc:

1

b = lambda { | msg | puts "msg: #{msg}" }
b.call("hi")

2

def create_block_object(&block)
  block
end
b = create_block_object{ |msg| puts "msg: #{msg}" }
b.call("hello")

3

b = Proc.new { |msg| puts "msg: #{msg}"}
b.call("hey")

I want to know the fourth way and would be glad if somebody would give me an answer.

Cezar
  • 55,636
  • 19
  • 86
  • 87
Davit
  • 1,394
  • 5
  • 21
  • 47

3 Answers3

1

This is another syntax for lambdas:

b = ->(msg) { puts "msg: #{msg}" }
b.call("hi")
davidrac
  • 10,723
  • 3
  • 39
  • 71
  • thanks, I know that, it is also present since Ruby 1.9 and it is not really deprecated, I guess – Davit Mar 17 '13 at 15:55
1

The book you are referring to is on Ruby 1.8.

In that version of Ruby, lambda and procs are effectively aliases, while Proc is a different beast. This is obviously misleading, which is why it is not recommended that you use proc as in

prc = proc {|x, y| puts x + y}

This syntax is considered deprecated and it is recommended using lambda in this case.

This is no longer valid for later versions of Ruby, starting with 1.9.

Cezar
  • 55,636
  • 19
  • 86
  • 87
  • 1
    thanks, I found out that you were right since book never mentioned a method you provided – Davit Mar 17 '13 at 16:04
  • 1
    Actually, `Kernel#proc` is equivalent to `Kernel#lambda` before Ruby 1.9, and equivalent to `Proc.new` in 1.9 (not sure about 2.0). For this reason Black cautions against using it (since the semantics of lambda and non-lambda `Procs` are different). This is written in a footnote in the section "Blocks, Closures, and Proc Objects" in Chapter 22 ("The Ruby Language"). – echristopherson Mar 18 '13 at 00:46
0

prc = proc {|x| x*x} has been depreciated.

This syntax was process was predominantly used in Ruby-1.8.7 versions.

kapiltekwani
  • 345
  • 3
  • 6