[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Implementing each and <=>

Nit Khair

1/13/2009 7:08:00 PM

I have a data model and would like to let it be Enumerable. I want to
confirm if this is the way of implementing each and <=> :

def each
@list.each { |item| yield item }
end

def <=>(other)
@list <=> other
end

(let's just assume that @list is an array)

Thanks.
--
Posted via http://www.ruby-....

12 Answers

Johnny McVietcong

4/15/2008 4:33:00 PM

0


"John B." <johnb505@gmail.com> wrote in message
news:ef87bf0d-1c1f-4446-926c-6e7b16e0fa72@a22g2000hsc.googlegroups.com...
On Apr 15, 9:18 am, Mitchell Holman <Noem...@comcast.com> wrote:
> Harry Hope <riv...@ix.netcom.com> wrote
> innews:ba8904p8lpbg5rrpk0cms77vs3cbmsdvso@4ax.com:
>
>
>
>
>
>
>
> >http://www.huffingtonpost.com/2008/04/15/army-times-catches......
> > 4.html
>
> > April 15, 2008
>
> > Army Times Catches Another McCain Gaffe On Petraeus
>
> > The Army Times highlight's John McCain's latest national security
> > gaffe:
> >http://www.armytimes.com/news/2008/04/military_mccain_petraeu...
>
> > Republican presidential candidate Sen. John McCain of Arizona may not
> > have been paying the closest of attention last week during hearings on
> > the Bush administration's Iraq policy.
>
> > Speaking Monday at the annual meeting of the Associated Press, McCain
> > was asked whether he, if elected, would shift combat troops from Iraq
> > to Afghanistan to intensify the search for al-Qaida leader Osama bin
> > Laden.
>
> > "I would not do that unless Gen. [David] Petraeus said that he felt
> > that the situation called for that," McCain said, referring to the top
> > U.S. commander in Iraq.
>
> > Petraeus, however, made clear last week that he has nothing to do with
> > the decision.
>
> > Testifying last week before four congressional committees, including
> > the Senate Armed Services Committee on which McCain is the ranking
> > Republican, Petraeus said the decision about whether troops could be
> > shifted from Iraq to Afghanistan was not his responsibility because
> > his portfolio is limited to the multi-national force in Iraq.
>
> > Decisions about Afghanistan would be made by others, he said.
>
> > "I've been sort of focused on another task," Petraeus said when
> > pressed about whether more troops should be diverted to Afghanistan
> > rather than Iraq.
>
> > ___________________________________________________
>
> > Ya get the feeling that Johnny's having problems staying awake?
>
> > Harry
>
> Looks like economics isn't the only thing he needs to brush up on.
>
> Mitchell Holman
>
> "The issue of economics is not something I've understood
> as well as I should....... I've got Greenspan's book."
> John McCain, Dec 18, 2007- Hide quoted text -
>
> - Show quoted text -

I can't wait until he goes head-to-head with Obama in debates. He's
going to
get his ass handed to him.
========

He'll shuck & jive and do his old Reagan 'grandpa' act, "Well, well...".


littleoleme

4/15/2008 11:30:00 PM

0


"John B." <johnb505@gmail.com> wrote in message
news:ef87bf0d-1c1f-4446-926c-6e7b16e0fa72@a22g2000hsc.googlegroups.com...
On Apr 15, 9:18 am, Mitchell Holman <Noem...@comcast.com> wrote:
> Harry Hope <riv...@ix.netcom.com> wrote
innews:ba8904p8lpbg5rrpk0cms77vs3cbmsdvso@4ax.com:
>
>
>
>
>
>
>
> >http://www.huffingtonpost.com/2008/04/15/army-times-catches......
> > 4.html
>
> > April 15, 2008
>
> > Army Times Catches Another McCain Gaffe On Petraeus
>
> > The Army Times highlight's John McCain's latest national security
> > gaffe:
> >http://www.armytimes.com/news/2008/04/military_mccain_petraeu...
>
> > Republican presidential candidate Sen. John McCain of Arizona may not
> > have been paying the closest of attention last week during hearings on
> > the Bush administration's Iraq policy.
>
> > Speaking Monday at the annual meeting of the Associated Press, McCain
> > was asked whether he, if elected, would shift combat troops from Iraq
> > to Afghanistan to intensify the search for al-Qaida leader Osama bin
> > Laden.
>
> > "I would not do that unless Gen. [David] Petraeus said that he felt
> > that the situation called for that," McCain said, referring to the top
> > U.S. commander in Iraq.
>
> > Petraeus, however, made clear last week that he has nothing to do with
> > the decision.
>
> > Testifying last week before four congressional committees, including
> > the Senate Armed Services Committee on which McCain is the ranking
> > Republican, Petraeus said the decision about whether troops could be
> > shifted from Iraq to Afghanistan was not his responsibility because
> > his portfolio is limited to the multi-national force in Iraq.
>
> > Decisions about Afghanistan would be made by others, he said.
>
> > "I've been sort of focused on another task," Petraeus said when
> > pressed about whether more troops should be diverted to Afghanistan
> > rather than Iraq.
>
> > ___________________________________________________
>
> > Ya get the feeling that Johnny's having problems staying awake?
>
> > Harry
>
> Looks like economics isn't the only thing he needs to brush up on.
>
> Mitchell Holman
>
> "The issue of economics is not something I've understood
> as well as I should....... I've got Greenspan's book."
> John McCain, Dec 18, 2007- Hide quoted text -
>
> - Show quoted text -

I can't wait until he goes head-to-head with Obama in debates. He's
going to
get his ass handed to him.
----------------------------------------------
Just like Ron Paul did.
And Insane Mccain will just put on his usual shit-eating grin and come up
with one of his trademark wise-ass retorts because he is so out of touch
with the facts. All he can hope for is to bullshit his way to the top.

Host.


Jan-Erik R.

1/13/2009 7:11:00 PM

0

Ruby Rabbit schrieb:
> I have a data model and would like to let it be Enumerable. I want to
> confirm if this is the way of implementing each and <=> :
>
> def each
> @list.each { |item| yield item }
> end
>
> def <=>(other)
> @list <=> other
> end
>
> (let's just assume that @list is an array)
>
> Thanks.
i would define each like this:
def each(&blk)
@list.each(&blk)
end

Nit Khair

1/13/2009 7:16:00 PM

0

badboy wrote:
> Ruby Rabbit schrieb:
>>
>> (let's just assume that @list is an array)
>>
>> Thanks.
> i would define each like this:
> def each(&blk)
> @list.each(&blk)
> end

okay, i forgot to mention that I was wondering whether the
easiest/cleanest way was to use a send somehow?

def each
@list.send
end

Actually, since the ruby source is in 'C' one can't check it to see
examples. I searched a lot but came up with nothing.

Thanks.
--
Posted via http://www.ruby-....

Jan-Erik R.

1/13/2009 7:29:00 PM

0

Ruby Rabbit schrieb:
> badboy wrote:
>> Ruby Rabbit schrieb:
>>> (let's just assume that @list is an array)
>>>
>>> Thanks.
>> i would define each like this:
>> def each(&blk)
>> @list.each(&blk)
>> end
>
> okay, i forgot to mention that I was wondering whether the
> easiest/cleanest way was to use a send somehow?
>
> def each
> @list.send
> end
>
> Actually, since the ruby source is in 'C' one can't check it to see
> examples. I searched a lot but came up with nothing.
>
> Thanks.
I don't think that using send is easier/cleaner
using @list.each(&blk) is nothing else than sending the message "each"
to @list with blk as a block parameter

Dave Thomas

1/13/2009 8:16:00 PM

0


On Jan 13, 2009, at 1:08 PM, Ruby Rabbit wrote:

> I have a data model and would like to let it be Enumerable. I want to
> confirm if this is the way of implementing each and <=> :

> ...

> def <=>(other)
> @list <=> other
> end

<=> would normally apply to items in your list, not to the container
itself.

Nit Khair

1/13/2009 8:30:00 PM

0

Dave Thomas wrote:
> On Jan 13, 2009, at 1:08 PM, Ruby Rabbit wrote:
>
>> I have a data model and would like to let it be Enumerable. I want to
>> confirm if this is the way of implementing each and <=> :
>
>> ...
>
>> def <=>(other)
>> @list <=> other
>> end
>
> <=> would normally apply to items in your list, not to the container
> itself.

That's whats confusing me. How would i put it?

I am putting in the <=> so this class might be more useful to others. I
haven't got around to using it myself. (I have a base class
ListDataModel with minimal methods and wanted to make it as useful as
possible).
--
Posted via http://www.ruby-....

Ken Bloom

1/14/2009 1:32:00 AM

0

On Tue, 13 Jan 2009 14:15:58 -0500, Ruby Rabbit wrote:

> badboy wrote:
>> Ruby Rabbit schrieb:
>>>
>>> (let's just assume that @list is an array)
>>>
>>> Thanks.
>> i would define each like this:
>> def each(&blk)
>> @list.each(&blk)
>> end
>
> okay, i forgot to mention that I was wondering whether the
> easiest/cleanest way was to use a send somehow?
>
> def each
> @list.send
> end

No. This won't work. Send is just a method for calling methods, and
unless you pass it a method name and all relevant arguments, it won't
know what message to send.

Thus, you'll be at

def each &blk
@list.send :each, &blk
end

and it's easier to do

def each &blk
@list.each &blk
end

--Ken


--
Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...

Ken Bloom

1/14/2009 1:34:00 AM

0

On Tue, 13 Jan 2009 15:30:20 -0500, Ruby Rabbit wrote:
> I am putting in the <=> so this class might be more useful to others. I
> haven't got around to using it myself. (I have a base class
> ListDataModel with minimal methods and wanted to make it as useful as
> possible).

Is there sume useful comparison for comparing two different data models
with each other? If not, then don't define <=>.

--Ken

--
Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...

Nit Khair

1/14/2009 5:12:00 AM

0


> Thus, you'll be at
>
> def each &blk
> @list.send :each, &blk
> end
>
> and it's easier to do
>
> def each &blk
> @list.each &blk
> end
>
> --Ken

thanks, (I did forget to put the :each in the lines i put)

> Is there sume useful comparison for comparing two different data models
> with each other? If not, then don't define <=>.

No, not at all. Thanks again.
--
Posted via http://www.ruby-....