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
On 04.12.2006 12:20, Peter S. wrote:
“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@:1(eval)
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
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.rubyrailways.com
On 04.12.2006 13:06, Peter S. wrote:
end
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
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
On Dec 4, 2006, at 03:20 , Peter S. wrote:
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 H. - [email protected] - http://blog.segment7.net
I LIT YOUR GEM ON FIRE!
On Dec 4, 2006, at 5:20 AM, Peter S. wrote:
“puts ‘hello’; i +=1; fluff(ork)”
This was a Ruby Q. a while back:
http://www.rubyquiz.com/quiz38.html
James Edward G. II