A small query

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

"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 Jonnalagadda <srinivas.j@siritech.com> 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

---------------------------------
Yahoo! Shopping
Find Great Deals on Holiday Gifts at Yahoo! Shopping

Hi,

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

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?

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

···

--
Nobu Nakada

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

Kind regards

    robert

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

nobuyoshi nakada wrote:

···

Hi,

At Thu, 8 Dec 2005 16:06:24 +0900,
Srinivas Jonnalagadda wrote in [ruby-talk:169525]:
> 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?

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

--
Nobu Nakada

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

Dan Diebolt wrote:

···

"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 Jonnalagadda <srinivas.j@siritech.com> 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

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

···

2005/12/8, Robert Klemme <bob.news@gmx.net>:

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

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

James Edward Gray II

···

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

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

Quoting James Edward Gray II <james@grayproductions.net>:

···

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

> 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

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