I don't think I understand how classes and functions are called properly in Ruby or RPG Maker.

I have a script I was testing to return the direction of something clockwise or counterclockwise to the direction I supply it. Here's my code:

class TestScript def clockwise_direction(direction) return [4,8,2,6][direction/2-1] end def counterclockwise_direction(direction) return [6,8,2,4][direction/2-1] end def reverse_direction(direction) return [8,6,4,2][direction/2-1] endendI THINK the syntax is correct, though I am a Ruby novice. But when I tried to use one of the functions in the script, I get an undefined method error. I'm positive I haven't made a typo. Can anyone stick this code in their own game and maybe figure out why it won't work the way I expected?
I've moved this thread to Learning RGSS and Ruby. Please be sure to post your threads in the correct forum next time. Thank you.
When you declare a function using "def name", you're saying that the function will be run in an instance of the class, that way, you'd need to do this to run it:

instance = TestScript.newd = instance.clockwise_direction(2)
In your case, you should declare it like this:

class TestScriptdef self.clockwise_direction(direction)
When you declare it this way, you can call the functions like this:

d = TestScript.clockwise_direction(2)
Google "Class and Instance Methods in Ruby" to read more about it.
When you declare a function using "def name", you're saying that the function will be run in an instance of the class, that way, you'd need to do this to run it:

instance = TestScript.newd = instance.clockwise_direction(2)In your case, you should declare it like this:

class TestScriptdef self.clockwise_direction(direction)When you declare it this way, you can call the functions like this:

d = TestScript.clockwise_direction(2)Google "Class and Instance Methods in Ruby" to read more about it.
I'll check it out. Do you have any good resources for learning how the classes in RPG Maker already work?

Original thread: https://forums.rpgmakerweb.com/threads/i-dont-think-i-understand-how-classes-and-functions-are-called-properly-in-ruby-or-rpg-maker.41748/