Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Implementing each and <=>

Reply
Thread Tools

Implementing each and <=>

 
 
Ruby Rabbit
Guest
Posts: n/a
 
      01-13-2009
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-forum.com/.

 
Reply With Quote
 
 
 
 
badboy
Guest
Posts: n/a
 
      01-13-2009
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

 
Reply With Quote
 
 
 
 
Ruby Rabbit
Guest
Posts: n/a
 
      01-13-2009
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-forum.com/.

 
Reply With Quote
 
badboy
Guest
Posts: n/a
 
      01-13-2009
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

 
Reply With Quote
 
Dave Thomas
Guest
Posts: n/a
 
      01-13-2009

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.

 
Reply With Quote
 
Ruby Rabbit
Guest
Posts: n/a
 
      01-13-2009
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-forum.com/.

 
Reply With Quote
 
Ken Bloom
Guest
Posts: n/a
 
      01-14-2009
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/~kbloom1/
 
Reply With Quote
 
Ken Bloom
Guest
Posts: n/a
 
      01-14-2009
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/~kbloom1/
 
Reply With Quote
 
Ruby Rabbit
Guest
Posts: n/a
 
      01-14-2009

> 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-forum.com/.

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      01-14-2009
Ruby Rabbit wrote:
> def <=>(other)
> @list <=> other
> end


Perhaps you mean something like this:

include Comparable
def <=>(other)
@list <=> other.list # or some other way to order them
end

Making your objects Comparable is distinct from making them Enumerable.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Transform a 2D color image into 2 images of (R1,G1,B) at each pixelof image 1 and (R2,G2,B) at each pixel of image2 for STEREO visualization 88888 Dihedral C++ 10 12-23-2011 02:28 PM
Hash .each and different action for each key Igor Nn Ruby 7 05-28-2011 12:33 PM
Array#each - getting each element and the index Pat Maddox Ruby 6 01-20-2006 04:04 PM
implementing the "each" method for own classes Philipp Huber Ruby 11 09-01-2005 03:37 PM
how do i? Full scan of each control in each grid row cell John Blair ASP .Net 1 08-03-2005 11:02 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57