[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

A small query

Srinivas Jonnalagadda

12/8/2005 7:06:00 AM

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


11 Answers

Dan Diebolt

12/8/2005 7:39:00 AM

0

"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=...

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

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

Nakada, Nobuyoshi

12/8/2005 7:41:00 AM

0

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


Bill Atkins

12/8/2005 8:05:00 AM

0

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

Srinivas Jonnalagadda

12/8/2005 8:40:00 AM

0

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=...
>
> http://frottage.org/rjp/ruby...
>
> 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



Robert Klemme

12/8/2005 12:34:00 PM

0

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

Douglas Livingstone

12/8/2005 3:23:00 PM

0

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
>

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 Gray

12/8/2005 3:32:00 PM

0

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

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



James Gray

12/8/2005 3:36:00 PM

0

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.

James Edward Gray II


MenTaLguY

12/8/2005 3:49:00 PM

0

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


The PHANTOM

12/22/2011 1:59:00 PM

0

On Dec 22, 7:30 am, emoneyjoe <emoney...@iglou.com> wrote:
> On 22 Dec 2011 08:32:05 GMT, rfisc...@sonic.net (Ray Fischer) wrote:
>
>
>
>
>
> >emoneyjoe  <emoney...@iglou.com> wrote:
> >>On Wed, 21 Dec 2011 02:35:23 -0800, Stars and Midnight China Blue
> >>> emoneyjoe <emoney...@iglou.com> wrote:
> >>>> On Tue, 20 Dec 2011 23:08:57 -0800, Stars and Midnight China Blue
> >>>> <chine.b...@yahoo.com> wrote:
>
> >>>> >In article <g9u2f7t9s2hn0eh966i95piipudh5g6...@4ax.com>,
> >>>> > emoneyjoe <emoney...@iglou.com> wrote:
>
> >>>> >>       When the senate passes a bill and tells the house
> >>>> >> "that's all there is", it is the same as giving a command
> >>>> >> on how to vote.
>
> >>>> >Why didn't Boehner warn McConnell this would blow up in the House?
>
> >>>>        McConnell is not the senate majority leader,
> >>>> when a bill gets passed with a 234 to 193 majority
> >>>> in the house, it deserves a floor vote in the senate.
>
> >>>So you want the House how to tell the Senate to vote.
>
> >>       No, silly, just vote on the bill from the house,
>
> >Which bill?
>
>       All of them, that is the only thing they get paid to do.
>
> >> if it
> >>goes down, fine, the senate has done it's job.
>
> >It looks like they did just that.  They tried to use the tax cut to
> >extort a laundry list of other items that they wanted and got shot
> >down.
>
>       Doesn't matter, just vote it down, by leadership
> selecting which bills to debate and vote on, it is
> a dictatorship.
>
>       Notice I did not mention party affiliation, just
> that every bill passed by the house should be
> voted up or down by the senate.
>
> >They overplayed their hand and lost.  Now they have to face the fact
> >that they raised taxes for middle-class America.
>
>       They haven't lost anything, why would the senate
> have the right to not vote on house bills, but the house
> would not have the same right to ignore senate bills.
>
>       Maybe if the senate would have voted on the
> house bill, the house would vote on the senate bill.
>
>       The president is the loser here, no matter what
> doesn't get done, he will be blamed, every elected
> official only has to answer to his constituents, and
> house members have less constituents than the
> senators or the president.
>
>       Why would you even think public opinion has
> any effect on a house member?- Hide quoted text -
>
> - Show quoted text -

I beg to differ. Obama,with his space age Teflon/Kevlar/Ceramic suit
will not be blamed for or accept blame for ANYTHING unless there's a
chance it might make him look "Presidential". He'll get a complete
pass on this fiasco and before this mess is over with the left,with
the help of the Official Obama Bureau of Lies, Misinformation and
Obfuscation will be blaming GW Bush and Halliburton.