Implementing each and <=>

Discussion in 'Ruby' started by Ruby Rabbit, Jan 13, 2009.

  1. Ruby Rabbit

    Ruby Rabbit Guest

    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/.
     
    Ruby Rabbit, Jan 13, 2009
    #1
    1. Advertising

  2. Ruby Rabbit

    badboy Guest

    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
     
    badboy, Jan 13, 2009
    #2
    1. Advertising

  3. Ruby Rabbit

    Ruby Rabbit Guest

    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/.
     
    Ruby Rabbit, Jan 13, 2009
    #3
  4. Ruby Rabbit

    badboy Guest

    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
     
    badboy, Jan 13, 2009
    #4
  5. Ruby Rabbit

    Dave Thomas Guest

    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.
     
    Dave Thomas, Jan 13, 2009
    #5
  6. Ruby Rabbit

    Ruby Rabbit Guest

    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/.
     
    Ruby Rabbit, Jan 13, 2009
    #6
  7. Ruby Rabbit

    Ken Bloom Guest

    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/
     
    Ken Bloom, Jan 14, 2009
    #7
  8. Ruby Rabbit

    Ken Bloom Guest

    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/
     
    Ken Bloom, Jan 14, 2009
    #8
  9. Ruby Rabbit

    Ruby Rabbit Guest


    > 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/.
     
    Ruby Rabbit, Jan 14, 2009
    #9
  10. 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/.
     
    Brian Candler, Jan 14, 2009
    #10
  11. Ruby Rabbit

    Ruby Rabbit Guest

    Brian Candler wrote:
    > 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.

    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.

    --
    Posted via http://www.ruby-forum.com/.
     
    Ruby Rabbit, Jan 14, 2009
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Blair
    Replies:
    1
    Views:
    436
    Eliyahu Goldin
    Aug 3, 2005
  2. Tjerk Wolterink

    xsl:for-each for each 3 elements problem

    Tjerk Wolterink, Nov 3, 2004, in forum: XML
    Replies:
    3
    Views:
    459
    Tjerk Wolterink
    Nov 3, 2004
  3. Philipp Huber
    Replies:
    11
    Views:
    208
    Daryl Richter
    Sep 1, 2005
  4. Pat Maddox
    Replies:
    6
    Views:
    175
    Marcin Mielżyński
    Jan 20, 2006
  5. Igor Nn
    Replies:
    7
    Views:
    469
    Johnny Morrice
    May 28, 2011
Loading...

Share This Page