[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Getting the source code of a block

Peter Szinek

12/4/2006 11:21:00 AM

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.rubyra...

7 Answers

Robert Klemme

12/4/2006 11:37:00 AM

0

On 04.12.2006 12:20, Peter Szinek wrote:
> 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...

No. Alternatively you could cook your own

irb(main):001:0> def pproc(code)
irb(main):002:1> block = eval("lambda {#{code}}")
irb(main):003:1> class <<block;self;end.send(:define_method,
:source_code) { code }
irb(main):004:1> block
irb(main):005:1> end
=> nil
irb(main):006:0> x = pproc '|a,b| a+b'
=> #<Proc:0x003ba8bc@(eval):1>
irb(main):007:0> x[10,20]
=> 30
irb(main):008:0> x.source_code
=> "|a,b| a+b"

Where do you need that for?

Kind regards

robert

Peter Szinek

12/4/2006 12:07:00 PM

0

> 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 => :other
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, :other => 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, :other =>
things'} etc. - but for this I need the string of the original block of
course.

bw,
Peter

__
http://www.rubyra...





Robert Klemme

12/4/2006 1:09:00 PM

0

On 04.12.2006 13:06, Peter Szinek wrote:
>> 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 => :other
> 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, :other => 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, :other =>
> 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

James Gray

12/4/2006 1:55:00 PM

0

On Dec 4, 2006, at 5:20 AM, Peter Szinek wrote:

> 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)"

This was a Ruby Quiz a while back:

http://www.rubyquiz.com/q...

James Edward Gray II

Peter Szinek

12/4/2006 2:18:00 PM

0

> 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.rubyra...


Peter Szinek

12/4/2006 2:19:00 PM

0

Eric Hodel

12/4/2006 6:53:00 PM

0

On Dec 4, 2006, at 03:20 , Peter Szinek wrote:

> 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)
}

--
Eric Hodel - drbrain@segment7.net - http://blog.se...

I LIT YOUR GEM ON FIRE!