Forum: Ruby A small query

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
srinivas.j (Guest)
on 2005-12-08 09:08
(Received via mailing list)
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
dandiebolt (Guest)
on 2005-12-08 09:41
(Received via mailing list)
"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
nobuyoshi.nakada (Guest)
on 2005-12-08 09:45
(Received via mailing list)
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.
srinivas.j (Guest)
on 2005-12-08 10:42
(Received via mailing list)
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
batkins57 (Guest)
on 2005-12-08 10:51
(Received via mailing list)
Isn't that sample the same as obj.instance_eval ?
bob.news (Guest)
on 2005-12-08 14:41
(Received via mailing list)
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
rampant (Guest)
on 2005-12-08 17:25
(Received via mailing list)
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
James G. (Guest)
on 2005-12-08 17:34
(Received via mailing list)
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
James G. (Guest)
on 2005-12-08 18:06
(Received via mailing list)
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
mental (Guest)
on 2005-12-08 18:11
(Received via mailing list)
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
This topic is locked and can not be replied to.