Getting the source code of a block

P

Peter Szinek

Hello,

Is the following thing possible somehow?

def foo(&bar)
do_something_with(bar)
end

baz = foo { puts 'hello'; i +=1; fluff(ork) }

now, baz should contain the string

"puts 'hello'; i +=1; fluff(ork)"

since I need to do this in the same file where I am defining the block,
I could get it using __FILE__ + some regexps - just asking if there is
some more elegant way...

TIA,
Peter
__
http://www.rubyrailways.com
 
P

Peter Szinek

Where do you need that for?

I am setting up a complicated object (similar to a tree) with this code:

crap = define_my_object do
foo do
bar
baz do
fluff :something => :hairy
ork :something => :eek:ther
end
end
end

now, I would like to call a few methods on 'crap' which will alter the
state of the object and eventually call a function which spits out crap as:

crap = define_my_object do
foo do
bar :some => :new_param_here
baz do
fluff :now => :here, :are => :some, :eek:ther => things
ork :and => :even, :more => nil
end
end
end

I have been thinking about some kind of serialization which would be
certainly possible just by knowing crap - but since everything remains
the same except a few parameters, it seemed easier to me to
sub! (':something => :hairy') {':now => :here, :are => :some, :eek:ther =>
things'} etc. - but for this I need the string of the original block of
course.

bw,
Peter

__
http://www.rubyrailways.com
 
R

Robert Klemme

I am setting up a complicated object (similar to a tree) with this code:

crap = define_my_object do
foo do
bar
baz do
fluff :something => :hairy
ork :something => :eek:ther
end
end
end

now, I would like to call a few methods on 'crap' which will alter the
state of the object and eventually call a function which spits out crap as:

crap = define_my_object do
foo do
bar :some => :new_param_here
baz do
fluff :now => :here, :are => :some, :eek:ther => things
ork :and => :even, :more => nil
end
end
end

So you are modifying the state of an object and want to emit code that
will recreate this state?
I have been thinking about some kind of serialization which would be
certainly possible just by knowing crap - but since everything remains
the same except a few parameters, it seemed easier to me to
sub! (':something => :hairy') {':now => :here, :are => :some, :eek:ther =>
things'} etc. - but for this I need the string of the original block of
course.

I would rather go serialization - if you use YAML the serialized state
is actually human readable (sort of). Marshal is binary but also faster
IIRC. I would *definitively* go serialization if you want to store
configuration state between script executions.

Kind regards

robert
 
P

Peter Szinek

I would rather go serialization - if you use YAML the serialized state
is actually human readable (sort of). Marshal is binary but also faster
IIRC. I would *definitively* go serialization if you want to store
configuration state between script executions.

Thanks for the answer. I guess to use yaml and maybe modify it a bit to
be even more human readable could be a meaningful compromise.

Cheers,
Peter

__
http://www.rubyrailways.com
 
E

Eric Hodel

Hello,

Is the following thing possible somehow?

def foo(&bar)
do_something_with(bar)
end

baz = foo { puts 'hello'; i +=1; fluff(ork) }

now, baz should contain the string

"puts 'hello'; i +=1; fluff(ork)"

$ cat x.rb
require 'rubygems'
require 'ruby2ruby'

puts proc { |hack| puts 'hello'; i +=1; fluff(ork) }.to_ruby

$ ruby x.rb
proc { |hack|
i
puts("hello")
i=(i + 1)
fluff(ork)
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top