Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

[Resolved] module Compareable

Not exactly RGSS, but yeah, as if anyone'd care...

What I'd like to see would be the Compareable module Ruby has by default... what I'm specifically looking for is the definition of the comparison operators.
I wasn't able to find it anywhere, so yeah... I'd really aprrechiate it if someone could be as kind and wrap in some neat code boxes in here ^^

Thanks in advance.
 
Something like this?

Code:
module Comparable

  def <(other)
    return (self <=> other) == -1
  end

  def <=(other)
    return (self <=> other) < 1
  end

  def ==(other)
    return (self <=> other) == 0
  end

  def >=(other)
    return (self <=> other) > -1
  end

  def >(other)
    return (self <=> other) == 1
  end

  def between?(min, max)
    return self >= min && self <= max
  end
end

Where <=> is an operator you define in the class that includes Comparable, such that it'd return -1, 0, or 1 depending on whether the receiver is less than, equal to, or greater than the other object. Ruby Documentation already defines the methods in plain English, which isn't hard to translate to Ruby code.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top