Inspect...and modify a proc object?

I’m trying to understand proc objects.

Can you inspect them? Is there anyway to walk through the original
context? Is there anyway to identify the objects or classes involved,
and what method is being invoked, with what parameters?

Currently, when I “puts myproc.inspect”, the result is not very
useful…something like:

#<Proc:[email protected]

Thanks,

-Kelly

myproc.methods

2007/1/17, littleears [email protected]:

myproc.methods returns the methods of the proc object itself. The proc
object, I guess, is a container for the original block. I’m interested
in the contents of the original block.

Any suggestions for exposing details about the original block?

Thanks,

-Kelly

On 1/21/07, Gregory B. [email protected] wrote:

On 1/21/07, kelly [email protected] wrote:

myproc.methods returns the methods of the proc object itself. The proc
object, I guess, is a container for the original block. I’m interested
in the contents of the original block.

Any suggestions for exposing details about the original block?

this seems just plain evil, so please think about why you’d ever do
it, and also, look for better solutions! :slight_smile:

Actually, sorry that doesn’t do the trick. It’ll give you the
enclosing scope, but not access to the block local scope.

b = lambda { |x| n=3; x + a }
=> #Proc:[email protected]:10(irb)

b.eval_with_binding(‘n’)
NameError: undefined local variable or method `n’ for main:Object
from (irb):10
from (irb):10

b[10]
=> 20

b.eval_with_binding(‘n’)
NameError: undefined local variable or method `n’ for main:Object
from (irb):10
from (irb):10

On 1/21/07, Gregory B. [email protected] wrote:

Any suggestions for exposing details about the original block?

this seems just plain evil, so please think about why you’d ever do
it, and also, look for better solutions! :slight_smile:

I think Ruby tries not to be judgmental about such things – it is an
open and trusting language:

  • Want to add features to an existing class? Please be my guest…
  • Want to change private values in an object? By all means…
  • Want to change the value of a constant? Why not?

Please trust that I’m not evil…

MUUU HHHHHAAAAAA HA Ha ha ha ha

On 1/21/07, kelly [email protected] wrote:

myproc.methods returns the methods of the proc object itself. The proc
object, I guess, is a container for the original block. I’m interested
in the contents of the original block.

Any suggestions for exposing details about the original block?

this seems just plain evil, so please think about why you’d ever do
it, and also, look for better solutions! :slight_smile:

seltzer:~ sandal$ irb

class Proc
def eval_with_binding(string)
eval string, binding
end
end
=> nil

b = 20
=> 20

a = lambda { |x| x + b }
=> #Proc:[email protected]:7(irb)

a.eval_with_binding(“local_variables”)
=> ["_", “__”, “b”, “a”]

class B
def local_vars_for_proc§
p.eval_with_binding(“local_variables”)
end
end
=> nil

c = B.new
=> #<B:0x31b3dc>

c.local_vars_for_proc(a)
=> ["_", “__”, “b”, “a”, “c”]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs