문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

This is another syntax for lambdas:

b = ->(msg) { puts "msg: #{msg}" }
b.call("hi")

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

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top