Spaceship operator in RGSS3

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?
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:
Spoiler
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. 🙂
You only need to include Comparable when building your own custom type of comparable objects, as for example:

I've made my own custom type of comparable objects, where I use the <=> operator. My class looks something like this:

Spoiler
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.
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
Oh, I see. Thank you!

Original thread: https://forums.rpgmakerweb.com/threads/spaceship-operator-in-rgss3.92160/