A small query


#1

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?

Thanks,

JS


#2

“with” isn’t a reserved word in Ruby so what you want it isn’t native.
However, someone wrote a module for “with”:

http://raa.ruby-lang.org/list.rhtml?name=with-block

http://frottage.org/rjp/ruby/with.html

Srinivas J. removed_email_address@domain.invalid 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?

Thanks,

JS


#3

Hi,

At Thu, 8 Dec 2005 16:06:24 +0900,
Srinivas J. wrote in [ruby-talk:169525]:

Can a similar thing be accomplished in Ruby?

Fortunately, no. On dynamic languages such as Ruby, it would
cause just confusions if you nested it.


#4

Isn’t that sample the same as obj.instance_eval ?


#5

I see that it does take care of nesting. But, since
‘method_missing’ is used, none of Object’s methods can be
called on the object passed to ‘with’.

Interesting nevertheless. Thank you for the URL.

JS


#6

Srinivas J. wrote:

 meth3(param1, param2)

end

Can a similar thing be accomplished in Ruby?

obj.instance_eval do
meth1
meth2(param)
meth3(param1, param2)
end

Kind regards

robert

#7

2005/12/8, Robert K. removed_email_address@domain.invalid:

Can a similar thing be accomplished in Ruby?

obj.instance_eval do
meth1
meth2(param)
meth3(param1, param2)
end

def with(obj, &block)
obj.instance_eval(&block)
end

class Foo; def bar; puts ‘rab’; end; end

foo = Foo.new

with foo do
bar
end

Regards,
Douglas


#8

On Dec 8, 2005, at 9:23 AM, Douglas L. wrote:

end
Ah yes. This actually does what you asked for. Ignore my incorrect
attempt.

James Edward G. II


#9

Quoting James Edward G. II removed_email_address@domain.invalid:

with foo do
bar
end

Ah yes. This actually does what you asked for. Ignore my
incorrect attempt.

In either case, caveat nuby – within the block ‘self’ will refer to
foo, and any instance variables will be foo’s. That’s probably just
a bit more agressive than the OP had in mind for ‘with’.

-mental


#10

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