Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to see the ruby code in a proc?

Tags:

ruby

proc

block

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

like image 937
B Seven Avatar asked Feb 22 '13 12:02

B Seven


People also ask

Is proc a Ruby?

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.

What does Proc mean in Ruby?

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.

What is the difference between procs and blocks?

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.

Does End block Ruby?

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.


1 Answers

Take a look at the sourcify gem:

proc { x + y }.to_source
# >> "proc { (x + y) }"
like image 102
Stefan Avatar answered Nov 09 '22 01:11

Stefan