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.

operaters and their uses: a question

ok, i know when you're scripting a conditional statement "==" means "is equal to" and "!=" means "is not equal to". I'm assuming "<=" means "is less than or equal to" and ">=" means "is greater than or equal to" but is there a way to use it in such a way as to say:

if 32 <= variable.x <= 40 #cuts down on amount of redundant typing

rather than

if variable.x >= 32 and variable.x <= 40 #had to type "variable.x" twice, seems redundant

or is there another (easier) way to check if variable.x is between 31 and 41

my appologies if this is in the help file already, It's just I'm at work when this popped in my head and i really would like it answered since i do alot of my game planning while at work with no rmxp in sight.
 
Yes it is in module Comparable you will find the between? method

1.between?(0, 2) #=> true

between?(min, max)
Returns TRUE when self falls within a range from min to max, inclusive.

When self <=> min or self <=> max returns nil, throws an ArgumentError exception.
 
ah ha. thanks. i thought there might be something like that, or else a way to define that. I started thinking of a way to define it....

wait, if it equals the min or max you get an ArgumentError exception? Will that crash the game?
 
no if these two expressions self <=> min or self <=> max return nil you will get an error

self <=> other is the general comparision method that returns -1 if self is less than other 0 if they are equal and 1 if self is greater than other and it is the method that is required to include module Comparable as a mixin

if that method returns nil then it will error since the methods module Comparable includes depend on that method

self == other
Returns TRUE when self and other are equal.

When <=> returns nil, returns nil.

self > other
Returns TRUE when self is greater than other.

When <=> returns nil, throws an ArgumentError exception.

self >= other
Returns TRUE when self is greater than or equal to other.

When <=> returns nil, throws an ArgumentError exception.

self < other
Returns TRUE when self is less than other.

When <=> returns nil, throws an ArgumentError exception.

self <= other
Returns TRUE when self is less than or equal to other.

When <=> returns nil, throws an ArgumentError exception.

between?(min, max)
Returns TRUE when self falls within a range from min to max, inclusive.

When self <=> min or self <=> max returns nil, throws an ArgumentError exception.
 
I said it in my last post

Its the general comparison method which < > and == are based upon

well off the top of my head
Code:
class Number
  include Comparable
  attr_reader :number
  def initialize(number)
    @number = number
  end
  def <=>(other)
    return nil
  end
end

p Number.new(4) < Number.new(2)

Try running that you will get a comparision between <Class> and <Class> failed error
 
ok. I'm just a little slow right now, I'm sure it'll all click in a little bit when i stop trying so hard to think.

edit:
Code:
class Number
  include Comparable
  attr_reader :number
  def initialize(number)
    @number = number
  end
  def <=>(other)
    return nil
  end
end

p Number.new("anything") == Number.new("anything else")

returns nil but if "==" is changed to "!=", "<", ">", "<=", ">=", it returns "Error on line 13: comparison of Number with Number failed"

So, if I compare a class to itself with anything other than == it gets the error?
 

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