p = Proc.new{ puts 'ok' }
Is is possible to see the ruby code in the proc?
inspect
returns the memory location:
puts p.inspect
#<Proc:0x007f9e42980b88@(irb):2>
Ruby 1.9.3
As opposed to a block, a proc is a Ruby object which can be stored in a variable and therefore reused many times throughout a program. # A proc is defined by calling Proc. new followed by a block. # When passing a proc to a method, an & is used to convert the proc into a block.
A Proc object is an encapsulation of a block of code, which can be stored in a local variable, passed to a method or another Proc, and can be called. Proc is an essential concept in Ruby and a core of its functional programming features.
When using parameters prefixed with ampersands, passing a block to a method results in a proc in the method's context. Procs behave like blocks, but they can be stored in a variable. Lambdas are procs that behave like methods, meaning they enforce arity and return as methods instead of in their parent scope.
There are two ways of defining a block in Ruby: The first is using the do.. end keyword, the other is using a pair of curly braces. Do.. end block is mainly used when defining a block of code that spans multiple lines, while curly braces {} are used when defining a block of code that spans a single line.
Take a look at the sourcify gem:
proc { x + y }.to_source
# >> "proc { (x + y) }"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With