SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Enthusiast zfade3's Avatar
    Join Date
    Jul 2005
    Location
    Endor
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question What does def <=>( b ) mean?

    I'm checking out a class that has the following method definition:

    Code:
    def <=>(b)
    I've never seen a
    Code:
    <=>
    definition before. what does it mean?

    Thanks
    --
    Jimmy Z

  2. #2
    SitePoint Zealot tconley79's Avatar
    Join Date
    Feb 2004
    Location
    Miami
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Spaceship

    Hi.

    This nifty little syntax is called the general comparison operator. Or more affectionately, the "spaceship" operator.

    The Pragmatic Programmer's guide defines it as:
    ...the spaceship operator, <=> compares two values, returning -1, 0, or +1 depending on whether the first is less than, equal to, or greater than the second.
    The use of this operator is that you can teach ruby how to compare two objects with values that are not intuitively apparent. Suppose you have a Building object, and you want to define one object's superiority over another in terms of their height--without having to expose their instance variables. To set this up, you would first include the module "Comparable" in your class declaration and override it's <=> function:

    Code:
    class Building
      include Comparable
    
      def <=> (other_building)
        self.height <=> other.height  
        #since "height" is presumably an integer, just call this method again
        #because integer objects already know how to compare themselves.
      end
      
      #or if you need a more custom operation...
    
      def <=> (other)
        if self.height < other.height-50
           -1
        elsif self.height > other.height+50
          1
        else
          0
        end
        # this would find two buildings equal if they were within 50 units of 
        # each other in height. Otherwise it would return the proper result if
        #one is more or less than 50 units in height from the other.
      end
    
    
    end
    
    
    #then you use could this to compare any Building object using standard comparison operators.
    
    if my_building > your_building
      puts "My building is taller"
    end

  3. #3
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can rewrite the alphabet to put u and i together.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  4. #4
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    813
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I try to use
    Code:
    def +
    # .... addition routine
    end
    but it seems doesn't work. should I use alias

  5. #5
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are defining your own + method it will need a parameter (the object you are adding).

  6. #6
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Otherwise use +@.

    Code:
    class Something
      def +@
        "hi"
      end
    end
    
    +Something.new  # => "hi"
    This is handy for DSLs.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •