Spaceship operator in RGSS3

toto8080

Villager
Member
Joined
Feb 26, 2018
Messages
15
Reaction score
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?
 

Another Fen

Veteran
Veteran
Joined
Jan 23, 2013
Messages
564
Reaction score
275
First Language
German
Primarily Uses
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:

toto8080

Villager
Member
Joined
Feb 26, 2018
Messages
15
Reaction score
0
First Language
German
Primarily Uses
RMVXA
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:

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.
 

Another Fen

Veteran
Veteran
Joined
Jan 23, 2013
Messages
564
Reaction score
275
First Language
German
Primarily Uses
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:

toto8080

Villager
Member
Joined
Feb 26, 2018
Messages
15
Reaction score
0
First Language
German
Primarily Uses
RMVXA
Oh, I see. Thank you!
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

How many parameters is 'too many'??
Yay, now back in action Happy Christmas time, coming back!






Back in action to develop the indie game that has been long overdue... Final Fallacy. A game that keeps on giving! The development never ends as the developer thinks to be the smart cookie by coming back and beginning by saying... "Oh bother, this indie game has been long overdue..." How could one resist such? No-one c
So I was playing with filters and this looked interesting...

Versus the normal look...

Kind of gives a very different feel. :LZSexcite:
To whom ever person or persons who re-did the DS/DS+ asset packs for MV (as in, they are all 48x48, and not just x2 the pixel scale) .... THANK-YOU!!!!!!!!! XwwwwX

Forum statistics

Threads
105,845
Messages
1,016,961
Members
137,561
Latest member
JaCrispy85
Top