Ruby/RGSSx questions that don't deserve their own thread

Have to say, creating a hash table of symbols (or if you are wary of typos, an array/hash with constants for indices/keys) does also sound like a good solution here.
Edit: You could also use a Struct instead of a hash table.

Otherwise I would gravitate to kyonides's solution (although you could do something like $game_system.settings.dup to single out your settings only)

Another option would be to use
$game_system.send( methodname, argument1, argument2, ... )
(documentation) which allows you to access random methods of $game_system, the method name can be a symbol or string (although at this point I would probably prefer the hash table method).

E.g.
Ruby:
class Game_System
  attr_accessor :dungeon_size
end
# ...
$game_system.send("dungeon_size")       # nil
$game_system.send("dungeon_size=", 17)  # 17
$game_system.send("dungeon_size")       # 17

Another option would be to make a wrapper object that you can modify as you like:
Ruby:
class Setting
  attr_accessor :value
  def initialize(initial_value = nil)
    @value = initial_value
  end
end
@kyonides This is for duplication and is relevant for saving and loading, not for repetively changing specific attributes. But thank you for your answer.

Edit: I did not see the message below, thank you for your answer, it seems closer to what I intended. I will investigate it
Cannot speak for kyonides, I was assuming they had something like this in mind:
Ruby:
settings = $game_system.clone
# ...
settings.dungeon_size = 17
settings.enemy_density = 0.25
settings.enemy_strength = 0.85
settings.puzzle_difficulty = 1.5
# ...
$game_system = settings

I also don't know what you had in mind, one option that I didn't think of, but is already used in other places in the default scripts would be to use a callback lambda:
Ruby:
callback = lambda { |value| $game_system.dungeon_size = value }
# ...
callback.call(17)  # Sets dungeon size to 17 from anywhere
Okay I found it, what I wanted was to have a module that listed the attributes from game_system that could be changed along with other data like string displayed when selecting the object etc.
My idea was to just store in my module the symbol of the attributes and get or set them without having to link getters or setters methods.

In class Object, there are two methods that solve my issue:
instance_variable_get(symb)
instance_variable_set(symb,obj)

Now, I can have in my module the symbols of the attributes that I want to change, and then refer to them when getting or setting the values.
hey, absolute noob at coding here, I'm trying to make a code that changes a variable depending on whether the last attack did successfully hit or not, but I keep getting errors. What am I doing wrong? if Game_ActionResult.@hit?
$game_variables[200] = 1
end

if Game_ActionResult.@evaded || Game_ActionResult.@missed
$game_variables[200] = 0
end
hey, absolute noob at coding here, I'm trying to make a code that changes a variable depending on whether the last attack did successfully hit or not, but I keep getting errors. What am I doing wrong? if Game_ActionResult.@hit?
$game_variables[200] = 1
end

if Game_ActionResult.@evaded || Game_ActionResult.@missed
$game_variables[200] = 0
end

That's not valid Ruby syntax. Where are you trying to add that?
That's not valid Ruby syntax. Where are you trying to add that?
i tired to make a separate script with just that, but eventually decided to put it in some other script that uses these action result functions or whatever they're called, and it works as intended now. However, I'd like to learn as much as I can from it. What is the problem with the syntax?
What is the problem with the syntax?

Hello, there are many things that do not work in your previous scriptlet:

Game_ActionResult.@hit?
$game_variables[200] = 1
end

if Game_ActionResult.@evaded || Game_ActionResult.@missed
$game_variables[200] = 0
end

Game_ActionResult is a class (a family of objects), it's not an object you want to use here.
Using @attr_name refers to an attribute of the object in the context of the code, this symbol should not be used unless you refer to the attribute of the object calling the method.

Here is a very basic code to synthesize this:
Ruby:
class A
  #new method called foo
  def foo
    #will set the value to the attribute @bar of the object of class A when using obj.foo
    @bar = "Hello"
    puts @bar #print the attribute on the console
  end
end

x = A.new #create an object of class A and refer it with variable named x
x.foo #valid call: x has a method foo

#Invalid calls:
#A.foo #class does not have method foo
#A.@bar #invalid syntax
#x.@bar #invalid syntax
#x.bar #object does not have method bar

I suggest you check the ruby and RGSS tutorials to have a rough idea of what you can write, maybe the slip into ruby could be a good introduction.

On an unrelated note, you've asked for a script here and I asked you a few questions but you did not respond, does that mean you're not interested in it anymore?
How can I get the screen's center current x and y in relation to the tilemap? For example, like the xy tiles of the center of where the camera is pointed at. $game_map.display_x/y gives me the leftmost/upmost tile ID.
You can use the center_ commands from Game_Player:
Ruby:
$game_map.display_x + $game_player.center_x #x-coordinate of center tile (float)
$game_map.display_y + $game_player.center_y #y-coordinate of center tile (float)
Heyo, with the recent talk with @kyonides here, I wanted to try to implement in ruby the .one? method that does not exist in ruby 1.8.1 (XP and VX version of ruby).

As I've never used blocks as parameters or arguments of methods, I was wondering if this code was the proper way to take a block as parameter and pass it to another method:
Ruby:
module Enumerable
  unless method_defined?(:one?)
    def one?(&block)
      select(&block).size == 1
    end
  end
end #Enumerable
I did not find a satisfying web page explaining how to handle blocks so if you have a good reference to explain how to handle them I would be interested.
Ruby:
module Enumerable
  def one?(&block)
    if block_given?
      i = 0
      each do |n|
        i += 1 if block.call(n)
        return false if i > 1
      end
      return i == 1
    else
      self.size == 1
    end
  end
end

I'm not really interested in explaining blocks here, but I can show you a very manual way to replace a select method call. There it's a given the code will be used in vanilla RMXP or RMVX only.

Curiously, I can't tell for certain whether this will be faster or slower than a straight forward call to select...
Thank you for your answer.
I'm not really interested in explaining blocks here, but I can show you a very manual way to replace a select method call. There it's a given the code will be used in vanilla RMXP or RMVX only.
I see, so I can perform .call on blocks, I also found that yield could work too, especially when using implicit blocks.

So for the sake of the exercise, if I were to write my own select method, I could do something like this I suppose:
Ruby:
module Enumerable
  #explicit block
  def select3(&block)
    return self.dup unless block_given?
    res = []
    each {|n| res.push(n) if block.call(n)}
    res
  end
  #implicit block 
  def select2
    return self.dup unless block_given?
    res = []
    each {|n| res.push(n) if yield(n)}
    res
  end
end
I think I'm supposed to return an Enumerator object when no block is given to select, but I don't know how I should create it. (also select in XP must take a block as an argument otherwise I get an error message)
Fun fact, in Ace (haven't tried this in XP), when I wrote a select method in Enumerable, it would not be called by the arrays when doing .select
I don't understand why, maybe the Array class redefines its own select method?

Curiously, I can't tell for certain whether this will be faster or slower than a straight forward call to select...
Yeah, we would need to run a few tests and compare the time. I bet in most rpg maker cases the call to select would be faster and we would only observe a meaningful difference when the object gets much bigger (and the eventual matching elements are close to the beginning of the object).
(It reminded me of the benchmarks for a binary search implementation in ruby)
The self.dup call as a result is completely unnecessary, even if you were to compare the resulting array with another one using the equality operator === later on. Just a straight forward self return value would suffice there.

I don't understand why, maybe the Array class redefines its own select method?

In time you'll discover that Array need optimizations for very specific reasons, and core devs have always been working on that. Would they ever stop? Probably not as long as Ruby keeps getting updates.
I don't understand why, maybe the Array class redefines its own select method?
Ruby:
Array.instance_methods(false).include?("select")  # => true
Yes.
As kyonides noted, this is probably done for performance optimization. If you look at the code snippet in Ruby Doc, you can see that the implementation works directly on the C array data instead of going back and fetching each array element through another Ruby call.

I think I'm supposed to return an Enumerator object when no block is given to select, but I don't know how I should create it.
I think there is no Enumerator class in the Ruby 1.8.1 core library. In Ruby 1.9.2 (VXAce) you can use self.enum_for(:select)

If you are interested in recreating enumerators for older RPG makers and don't mind some performance penalties, you could take a look at how Generators were implemented using continuations: here
(Of course, if you only need an enumerator for a specific case, you can likely implement that without continuations)

The self.dup call as a result is completely unnecessary, even if you were to compare the resulting array with another one using the equality operator === later on.
I don't think a defensive copy is a bad move here since it prevents issues if some unsuspecting user modifies one of the arrays after. (like using array.select2(&nil).freeze)
On the other hand, if the user does not modify the array, then the duplication should be almost free if I remember correctly. (cloned arrays should share the original array data until one array gets modified)
Thank you both for your answers. I think I understand a bit more blocks now 🙂

Yes the dup call was to create a new array quickly to allow calls on it that would change the array but not the original one (like push or map! etc.). But it should be replaced with an enum_for (thank you for the line Another Fen) or raise an error like in XP.

Original thread: https://forums.rpgmakerweb.com/threads/ruby-rgssx-questions-that-dont-deserve-their-own-thread.150/