So, we can rename that to create what you asked for:
class Object
alias_method :with, :instance_eval
end
Then your code would run.
Here's another idea:
[ [:meth1],
[:meth2, param],
[:meth3, param1, param2] ].each do |call_details|
obj.send(*call_details)
end
Or we could switch that to a Hash, which probably makes more sense here:
{ :meth1 => ,
:meth2 => [param],
:meth3 => [param1, param2] }.each do |meth, params|
obj.send(meth, *params)
end
We can wrap that:
class Object
def with( hash_of_calls )
hash_of_calls.each { |meth, params| obj.send(meth, *params) }
end
end
And take advantage of Ruby's auto-hashing parameter syntax:
obj.with :meth1 => ,
:meth2 => [param],
:meth3 => [param1, param2]
Maybe that will give you some fresh ideas.
James Edward Gray II
···
On Dec 8, 2005, at 6:37 AM, Robert Klemme wrote:
Srinivas Jonnalagadda wrote:
Dear all,
Object Pascal (at least as provided by Delphi) has a facility to send
a sequence of messages to the same receiver, using a construct called
'with'. If 'obj' is an object with callable methods 'meth1', 'meth2'
and 'meth3', it is possible to write:
with obj do
meth1();
meth2(param);
meth3(param1, param2)
end
Can a similar thing be accomplished in Ruby?
obj.instance_eval do
meth1
meth2(param)
meth3(param1, param2)
end