On Dec 8, 2005, at 6:37 AM, Robert K. wrote:
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
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 G. II