Forum: Ruby Implementing each and <=>

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.
R. K. (Guest)
on 2009-01-13 21:08
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.
badboy (Guest)
on 2009-01-13 21:12
(Received via mailing list)
Ruby R. schrieb:
>
> (let's just assume that @list is an array)
>
> Thanks.
i would define each like this:
   def each(&blk)
     @list.each(&blk)
   end
R. K. (Guest)
on 2009-01-13 21:16
badboy wrote:
> Ruby R. 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.
badboy (Guest)
on 2009-01-13 21:31
(Received via mailing list)
Ruby R. schrieb:
> 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 T. (Guest)
on 2009-01-13 22:17
(Received via mailing list)
On Jan 13, 2009, at 1:08 PM, Ruby R. 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.
R. K. (Guest)
on 2009-01-13 22:31
Dave T. wrote:
> On Jan 13, 2009, at 1:08 PM, Ruby R. 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).
Ken B. (Guest)
on 2009-01-14 03:40
(Received via mailing list)
On Tue, 13 Jan 2009 15:30:20 -0500, Ruby R. 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
Ken B. (Guest)
on 2009-01-14 03:41
(Received via mailing list)
On Tue, 13 Jan 2009 14:15:58 -0500, Ruby R. wrote:

>
> 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
R. K. (Guest)
on 2009-01-14 07:12
> 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.
Brian C. (Guest)
on 2009-01-14 11:25
Ruby R. 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.
R. K. (Guest)
on 2009-01-14 11:33
Brian C. wrote:
> Ruby R. 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.
I was thinking that including Comparable would give users the ability to
sort the existing model in various ways.

I was wrong there, since it clearly is a comparison with another model.
That is not something likely. My bad. Thanks, Brian.
This topic is locked and can not be replied to.