Understanding an error

So, I am scripting a little mail system in my game. To populate the lists of "read letters" and "all letters", I use this bit of code.

Code:
def make_item_list
    case @mode
    
      when :all
        self.index = 0
        @data = []
        @letter_size = -1
        
        until @letter_size == $received_letter.size
          @letter_size = @letter_size + 1
          if $received_letter[@letter_size] > 0
            @data.push(@arr.keys[@letter_size])
          end
        end
      
 
       when :new
         self.index = 0
         @data = []
         @letter_size = -1
        
         until @letter_size == $received_letter.size
           @letter_size = @letter_size + 1
           if $received_letter[@letter_size] == 1
             @data.push(@arr.keys[@letter_size])
           end
          end
        
     end
      
  end

Why is it that when I use "$received_letter[@letter_size] == 1" I get no errors, but if use operators other than "==" I get the error "underfined method '>' for nil:NilClass?

I've moved this thread to Script Support. Thank you.

Moved to: Script Support

Ooops! Sorry for the mistake. I was sure I had picked the right forum. :blush:
To answer your question directly...
Even if the variable is nil, it can be checked for equality, but a nil value can not be higher or lower than a number, that check is only possible if two numbers are in question.

I don't know what is that code supposed to do exactly (it is written in such a convoluted way that I gave up on figuring it out, sorry 😀), but I am pretty sure that you just want to iterate an array or hash, check the elements, and if the conditions are met, put the elements on a list.
If so, there is no need for until loops, you can simply iterate the array/hash itself. Much more readable that way, much shorter, much more efficient, and way less prone to errors.

To iterate an array:
Spoiler
Code:
# Method 1:
ary.each do |element|
  # Do whatever you want here with each element.
end

# Method 2:
ary.each_with_index do |element,index|
  # Same as above, but this one got the index of the elements too.
end

To iterate a hash:
Spoiler
Code:
# Method 1:
hashy.each do |key,value|
  # Do your thing here with the keys/values.
end

# Method 2:
hashy.each_value do |value|
  # When you only need the values from the hash.
end

# Method 3:
hashy.each_key do |key|
  # When you only need the keys from the hash.
end

I think your code could look like this, for example:
Spoiler
Code:
  def make_item_list
    @data = []
    case @mode
    when :all
      $received_letter.each_with_index do |letter,i|
        @data << @arr.keys[i] if letter && letter > 0
      end
    when :new
      $received_letter.each_with_index do |letter,i|
        @data << @arr.keys[i] if letter && letter == 1
      end
    end
  end
But I'm just guessing what you wanted to do here, no idea if your global variable is an array or hash, or what that @arr variable is.
Note that I first checked if there is actually an element to compare before I compared it with a number.
Simply using letter in this case will do the same as if I would have used !letter.nil?.
Using @data << value is the same as using @data.push(value), in case you didn't see that way yet. It's just a matter of preference which one to use, unless you want to add multiple elements to the array at once, in which case you can only use the latter (by adding more value arguments in the brackets).

Point is, if you have an array or hash that you want to iterate, use the built-in methods for iterating them, that is why those are made after all, and that is one of the best part of Ruby (tons of handy iteration methods with easy to learn syntax).
Iterations like until, while, etc are not needed for these purposes (for a one-time iteration of an array or hash).

Ohh, and you can increment a variable by simply doing this:
Code:
var += 1 # Adds 1 to the variable var.
var += 2 # Adds 2 to the variable var.
And so on. No need to do the whole var = var + 1 thing.
To answer your question directly...
Even if the variable is nil, it can be checked for equality, but a nil value can not be higher or lower than a number, that check is only possible if two numbers are in question.

I don't know what is that code supposed to do exactly (it is written in such a convoluted way that I gave up on figuring it out, sorry 😀), but I am pretty sure that you just want to iterate an array or hash, check the elements, and if the conditions are met, put the elements on a list.
If so, there is no need for until loops, you can simply iterate the array/hash itself. Much more readable that way, much shorter, much more efficient, and way less prone to errors.

To iterate an array:
Spoiler
Code:
# Method 1:
ary.each do |element|
  # Do whatever you want here with each element.
end

# Method 2:
ary.each_with_index do |element,index|
  # Same as above, but this one got the index of the elements too.
end

To iterate a hash:
Spoiler
Code:
# Method 1:
hashy.each do |key,value|
  # Do your thing here with the keys/values.
end

# Method 2:
hashy.each_value do |value|
  # When you only need the values from the hash.
end

# Method 3:
hashy.each_key do |key|
  # When you only need the keys from the hash.
end

I think your code could look like this, for example:
Spoiler
Code:
  def make_item_list
    @data = []
    case @mode
    when :all
      $received_letter.each_with_index do |letter,i|
        @data << @arr.keys[i] if letter && letter > 0
      end
    when :new
      $received_letter.each_with_index do |letter,i|
        @data << @arr.keys[i] if letter && letter == 1
      end
    end
  end
But I'm just guessing what you wanted to do here, no idea if your global variable is an array or hash, or what that @arr variable is.
Note that I first checked if there is actually an element to compare before I compared it with a number.
Simply using letter in this case will do the same as if I would have used !letter.nil?.
Using @data << value is the same as using @data.push(value), in case you didn't see that way yet. It's just a matter of preference which one to use, unless you want to add multiple elements to the array at once, in which case you can only use the latter (by adding more value arguments in the brackets).

Point is, if you have an array or hash that you want to iterate, use the built-in methods for iterating them, that is why those are made after all, and that is one of the best part of Ruby (tons of handy iteration methods with easy to learn syntax).
Iterations like until, while, etc are not needed for these purposes (for a one-time iteration of an array or hash).

Ohh, and you can increment a variable by simply doing this:
Code:
var += 1 # Adds 1 to the variable var.
var += 2 # Adds 2 to the variable var.
And so on. No need to do the whole var = var + 1 thing.

But would nil be equal to 1? The result should be false, no? Instead the game recognizes the values correctly (and when I "printed" the variable I check, after turning it into a string, I get the correct value as well), and categorizes the letters in the way I want. I can only think maybe I did the "loop" wrong and it checks for values beyond the actual size of the array (which would be nil values).

You are correct that I did that to iterate an array, and that your example is MUCH better than what I do. It's just supposed to sort the received letters into read ones and unread ones.

I know of the possibility to use << instead of push, but push stuck to my mind, so I default to it very often.
You are right, it does read values beyond the real elements in the array.

You didn't seem to forget that array indexes start from 0, but you forgot to account for this in your until condition. It should have been ary.size - 1, since that is the highest index number in an array.
You are right, it does read values beyond the real elements in the array.

You didn't seem to forget that array indexes start from 0, but you forgot to account for this in your until condition. It should have been ary.size - 1, since that is the highest index number in an array.

So I was on the right path. I changed things around a little, and now everything seems to work fine. I still need to re-write it so that it uses the each do method you showed me, as that is a much cleaner way to handle this, but for now I'll keep it as it is. I am writing this system to substitute a script (it had an issue where it would reset the status of the mail after you quit the game, so I took the opportunity to make some practice and do my own from scratch), so I am not in a hurry. I should start to figure out how to handle arrays and hashes efficiently, though, so that I can make better scripts.

Learning Ruby feels a bit like a waste, since I'll probably only ever use it on one game, and move to MV afterward, so I tend to not commit enough time to it, and then I make a mess everywhere. XD

Anyway, this is solved for me, the thread can be locked as far as I am concerned.

Original thread: https://forums.rpgmakerweb.com/threads/understanding-an-error.88735/