Spaceship operator in RGSS3

Discussion in 'Learning Ruby and RGSSx' started by toto8080, Mar 4, 2018.

  1. toto8080

    toto8080 Villager Member

    Messages:
    15
    Likes Received:
    0
    First Language:
    German
    Primarily Uses:
    RMVXA
    So the other day I wondered, that is it possible to use the combined comparison operator (alias the spaceship operator <=> ) in my RGSS3 script without including the Comparable module?

    I ran a basic experiment and it turned out that I could indeed run my game without including it, there was no error messages and the script did what it should, but I'm not completely sure how it affects my program later on.

    Can somebody explain me, how does this operator works in RPG Maker?
     
    #1
  2. Another Fen

    Another Fen Veteran Veteran

    Messages:
    526
    Likes Received:
    239
    First Language:
    German
    Hey there,

    The <=> isn't strictly bound to the Comparable module, you can define that operator for any type of object in theory (if it makes sense to do so would be a different topic of course). By default, the Comparable module actually just adds methods for the other comparators (>, <, >=, <=, ==, between?) based on the result of <=>. See Ruby Doc.

    Including a module into a class (or another module) will establish the module as a supertype of your class, allowing the class to copy the modules methods (like inheriting from another class). It does not affect which operations you can use with your other objects.

    By default, Strings and many Numeric types already include Comparable, so you can compare those with <=> without any prerequisites. You only need to include Comparable when building your own custom type of comparable objects, as for example:
    Code:
    class Argument
      include Comparable
     
      def initialize(strength)
        @strength = strength
      end
    
      def strength_rating
        case @strength
        when :strong       then return 3
        when :considerable then return 2
        when :weak         then return 1
        when :petty        then return 0
        else return -1
        end
      end
    
      def <=>(other_argument)
        return self.strength_rating <=> other_argument.strength_rating
      end
    end
    
    a1 = Argument.new(:strong)
    a2 = Argument.new(:weak)
    winner = (a1 > a2) ? "The first" : (a1 < a2) ? "The second" : "Neither"
    print "#{winner} argument has convinced me!"

    Hope this helps after all, I'm not sure if I understood your problem correctly. :)
     
    Last edited: Mar 4, 2018
    #2
  3. toto8080

    toto8080 Villager Member

    Messages:
    15
    Likes Received:
    0
    First Language:
    German
    Primarily Uses:
    RMVXA
    I've made my own custom type of comparable objects, where I use the <=> operator. My class looks something like this:

    Code:
    class myClass
    
      include Comparable
    
      attr_accessor :x
      attr_accessor :y
    
      def initialize(x,y)
        @x = x
        @y = y
      end
    
      def value_3
        value_1 + value_2
      end
      
      def <=>(other)
        value_3 <=> other.value_3
      end
    
      def ==(other)  
        self.x == other.x && self.y == other.y
      end
    
    end
    


    The interesting part comes, when I remove the include from the beginning. The program will run without any errors, moreover, it will use my methods.
     
    #3
  4. Another Fen

    Another Fen Veteran Veteran

    Messages:
    526
    Likes Received:
    239
    First Language:
    German
    You can compare these objects using <=> as you specifically defined this operator. Without Comparable, you just can't use the other operators listed above without also specifically defining them:

    print myClass.new(4, 5) <=> myClass.new(5, 7) # => -1
    print myClass.new(4, 5) >= myClass.new(5, 7) # => <NoMethodError> or false if Comparable is included



    I know this is just an example, but you might want to make sure that two different objects do not have the same order, since this might throw some implementations off:

    a = myClass.new(4, 4)
    b = myClass.new(3, 5)
    print a > b # false
    print a == b # false
    print a >= b # true
     
    Last edited: Mar 4, 2018
    #4
    toto8080 likes this.
  5. toto8080

    toto8080 Villager Member

    Messages:
    15
    Likes Received:
    0
    First Language:
    German
    Primarily Uses:
    RMVXA
    Oh, I see. Thank you!
     
    #5

Share This Page