An = sign included in a method name when defining that method (SOLVED)

Hey all!

Doing my best to learn Window related classes at the moment.

And I've come across this method in VX Ace for the Window_MenuStatus class:
Ruby:
  #--------------------------------------------------------------------------
  # * Set Pending Position (for Formation)
  #--------------------------------------------------------------------------
  def pending_index=(index)
    last_pending_index = @pending_index
    @pending_index = index
    redraw_item(@pending_index)
    redraw_item(last_pending_index)
  end
Please note:
There is a public instance variable in this class called pending_index (or @pending_index)
------------------------------------------------------------------------

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

As the title suggests, I'm not sure what that "=" is doing in the method name for def pending_index=(index)

Is it that it's:
a)
Just a naming choice and doesn't change the method in any meaningful way?
b) Maybe something to do with making @pending_index equal to the established (index)?
c) Something else I'm not quite getting?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I'm thinking it's just a naming choice as there is a line IN that method that makes @pending_index equal to (index) as well.

But, as it looks like this method isn't called by anything anywhere else in the entire Script Editor, I was thinking maybe that equals sign did something special?

Thanks for any help as always,
- Your favourite up and coming scripter 😛, The_Sarah
It is a special syntax to define a setter method.

When you call "pending_index = 1" for exemple, it isn't just changing the value of the @peding_index variable, it is actually calling the method "peding_index=", which by what I can tell is making the window to redraw after each time you change the peding_index value.

when you call "attr_accessor", it is creating under the hood a simple getter and setter method to the symbols provided, but you can define a custom getter or setter yourself.


EDIT

I think this post can clarify the basics about getters and setters in ruby:
https://stackoverflow.com/questions/4370960/what-is-attr-accessor-in-ruby
The method above is doing more than just setting a value. It's recording the old value, setting the new value, and updating the window to redraw both the old and the new.

The public instance variable is read only, and cannot be changed from outside this class.

Code:
  attr_reader   :pending_index            # Pending position (for formation)
Ah, thanks guys @Shaz , @CaRa_CrAzY !

So, if I'm understanding you both correctly, the way it works is this... ? :

Code:
  #--------------------------------------------------------------------------
  # * Public Instance Variables
  #--------------------------------------------------------------------------
  attr_reader   :pending_index            # Pending position (for formation)
Because the above code is an attr_reader and not a writer (or my fav pal accessor), in order to change it it needs a Setter Method.

Setter Methods use that "=" sign in the name format that we see here:
Ruby:
  #--------------------------------------------------------------------------
  # * Set Pending Position (for Formation)
  #--------------------------------------------------------------------------
  def pending_index=(index)
    last_pending_index = @pending_index
    @pending_index = index
    redraw_item(@pending_index)
    redraw_item(last_pending_index)
  end

And thus, this method is telling Ruby we're gonna be changing the reader @pending_index with what this method returns (index) as.

Then, this method goes ahead and does its thing: Assigning last_pending_index the value of what @pending_index is currently first, then changing @pending_index and redrawing the appropriate items.

~~~~~~~~~~~~~~~~~~

Additional Questions though:
1) As far as I can tell, this method isn't actually called anywhere.
So, is it the case that this method is called whenever the @pending_index variable is changed to something else within this class ?

2) If @pending_index was an accessor rather than a reader, would it not be easier?
Or is it the very fact that @pending_index IS a reader, that allows us to then create a multi-functioning setter method like this def pending_index= method does?
Keep in mind it's a window not a menu, that's why you can't find there a call to that method.

attr_accessor :method

does exactly the same as

def method
@method
end

def method=(value)
@method = value
end

It's just a Module class method that's always embedded in any class for lazy people to work with. That's why you define your own getter or setter if you need to add custom code for any personal reason.
As far as I can tell, this method isn't actually called anywhere.
It is. I just did a CTRL + SHIFT + F for pending_index and it's all used within Scene_Menu:
Code:
@status_window.pending_index = -1
@status_window.pending_index = @status_window.index
You only trigger the pending_index= method by setting the value outside of the class (like above) or within the class by using self.pending_index = something. Doing @pending_index = something will not call the method.

Kyo already answered question 2.
@KK20
It is. I just did a CTRL + SHIFT + F for pending_index and it's all used within Scene_Menu:
Ah, I was going by the logic of "@pending_index variable" != "pending_index= method" and that window_menustatus.pending_index thus referred to the one called "pending_index" not the one called "pending_index=" lol.

I see that that was incorrect.
You only trigger the pending_index= method by setting the value outside of the class (like above) or within the class by using self.pending_index = something. Doing @pending_index = something will not call the method.

That was throwing me for a loop, thanks for breaking that down.

@kyonides
Keep in mind it's a window not a menu, that's why you can't find there a call to that method.

attr_accessor :method

does exactly the same as

def method
@method
end

def method=(value)
@method = value
end

It's just a Module class method that's always embedded in any class for lazy people to work with. That's why you define your own getter or setter if you need to add custom code for any personal reason.

Right. That helps. Thanks for explaining, the tutorials I did on attrs clearly didn't go into enough detail lol!

Thanks everyone.
Yes, just in case it's not obvious, the method is called something= but you can have a space before the = when you actually do the call:

Code:
def myvalue=(value)
...
end


later ...

self.myvalue = 10

or from outside the class ...

object.myvalue = 10

which explains why it might not have turned up for you in a global search, if you were searching for pending_index= rather than pending_index =.
Yes, just in case it's not obvious, the method is called something= but you can have a space before the = when you actually do the call:

Code:
def myvalue=(value)
...
end


later ...

self.myvalue = 10

or from outside the class ...

object.myvalue = 10

which explains why it might not have turned up for you in a global search, if you were searching for pending_index= rather than pending_index =.

Nice addition to the thread.

Thanks again for the help everyone, I think with this final post that's the topic fully broken down and explained. What a wonderfully helpful community!

I vow to make some more cool public scripts in the future with this knowledge in return!

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/an-sign-included-in-a-method-name-when-defining-that-method-solved.127079/