Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #3325

Re: Using the spaceship operator

From Brian Candler <b.candler@pobox.com>
Newsgroups comp.lang.ruby
Subject Re: Using the spaceship operator
Date 2011-04-21 12:26 -0500
Organization Service de news de lacave.net
Message-ID <b364c46c94653d34da0a4f4b0f184623@ruby-forum.com> (permalink)
References <084e58ba-184f-470c-a341-68bfecc192a9@w36g2000vbi.googlegroups.com> <1e803fee-d137-48bc-9d7e-5e3ff93089a8@a26g2000vbo.googlegroups.com>

Show all headers | View raw


RichardOnRails wrote in post #994287:
> Brian: Thanks for
> 1. addressing my key issue about how to make "self" be my array, so to
> speak.  Question,  would that redefinition of <=> contaminate uses of
> "sort" in other top-level classes?

No. I only redefined <=> within the class MyElement, so it only affects 
what happens when you call <=> on an object of class MyElement.

When you call sort on an Array (that is, your outer array which includes 
the pairs), internally it does a quicksort. The quicksort calls a.<=>(b) 
for various pairs of elements a and b within the array.

So what matters is that the elements *within* the array implement <=> 
for comparing themselves against any other element.

> 2. using "delegation", which I've seen in those textbooks but not yet
> internalized.

Basically it means passing through method calls to the underlying 
object. You can do this explicitly for each method of interest:

class MyElement
  def initialize(a)
    @a = a
  end
  def size
    @a.size
  end
  def [](index)
    @a[index]
  end
  def []=(index,val)
    @a[index] = val
  end
  ... etc
end

This gives you an opportunity to customise the behaviour in any way you 
like, or have one method call combine the results from invoking multiple 
underlying objects, or whatever you like.

If you find it tedious to repeat lots of method definitions, then you 
can look at method_missing, or delegate.rb in the Ruby standard library. 
There are examples in the source code. In this case:

require 'delegate'
class MyElement < DelegateClass(Array)
  def <=>(other)
    self[1] <=> other[1]
  end
end

This creates a new class which passes all unknown methods to the 
underlying object.

a = MyElement.new([:x, :y])
puts a[1]

You have called the '[]' method on your object of class MyElement, and 
it's automatically passed through to the '[]' method on the underlying 
Array object.

-- 
Posted via http://www.ruby-forum.com/.

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Using the spaceship operator RichardOnRails <RichardDummyMailbox58407@USComputerGurus.com> - 2011-04-20 21:13 -0700
  Re: Using the spaceship operator John Feminella <johnf@bitsbuilder.com> - 2011-04-20 23:21 -0500
  Re: Using the spaceship operator Brian Candler <b.candler@pobox.com> - 2011-04-21 04:41 -0500
    Re: Using the spaceship operator 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-21 12:07 -0500
  Re: Using the spaceship operator RichardOnRails <RichardDummyMailbox58407@USComputerGurus.com> - 2011-04-21 06:48 -0700
    Re: Using the spaceship operator Brian Candler <b.candler@pobox.com> - 2011-04-21 12:26 -0500
  Re: Using the spaceship operator RichardOnRails <RichardDummyMailbox58407@USComputerGurus.com> - 2011-04-21 16:56 -0700
    Re: Using the spaceship operator Brian Candler <b.candler@pobox.com> - 2011-04-26 05:08 -0500

csiph-web