Menu

Toshimaru's Blog

[Ruby]How to call Proc

Create Proc

Let’s create Proc in Ruby.

square = Proc.new { |x| x**2 }
# or 
square = lambda { |x| x**2 }
# or 
square = ->(x) { x**2 }

->(x) { ... } looks good to me.

Call Proc

How to call Proc?

square.call(3)  #=> 9
# shorthands:
square.(3)      #=> 9
square[3]       #=> 9

proc.call() is orthodox.

Reference

Load more