[Ace] NoMethodError occured when creating a object

Hello forums,

I'm relatively new to RGSS3 scripting and the Ruby language as a whole. I made a little script to excercise what I know and hopefully get better at typing the lanuage's syntax, but I haven't been able to understand why its throwing me this error.
 

class Person def initialize(color, number) @color, @number = color, number endendPerson = Person.new( "Red ", 2) Person2 = Person.new ("Blue", 1)When I run this, I get this

"line 8: NoMethodError occured"

"undefined method 'new' for #<Person0x773cb78 @color = "Red", @number=2>"



Okay so here's the funny thing about this code. Ruby is saying I'm getting a error at line 8, but once I get rid of line 9 the code works.

The only way I can get this code to work while still having 2 objects is by changing

"Person = Person.new("Red", 2)" in line 8 to something like

"Person1 =Person.new("Red", 2)"

So why doesn't RGSS3 not like the word "Person" as the object name ? Well it likes it, but only when I don't make a second object of the class Person. Is there something I'm not understanding here ??

Oh just to throw this out. The code is not suppose to do anything yet, but run the game with no errors.
The problem is that you are replacing the Person class with an instance of the Person class.

After you do "Person = Person.new("Red", 2)", the Person class can no longer be found.
Person = Person.new( "Red ", 2) makes Person a new object of Person type

Person2 = Person.new ("Blue", 1) looks for the method new in the Person object previously made

Most programming languages look for the most local declared instance of something and then branches out to a more global scope.

For example, a local variable will always be used over the global one, if they have the same name. (which can be useful in some instances).
Don't start variable names with upper case. Is Person your class name or your variable name? See the confusion?
Ok from my understanding from what you guys gave me. The whole reason I got an error was because Ruby didn't know I wanted to used the variable called "Person" and not the class, so instead the line

Person = Person.new( "Red ", 2)
 

assigned the class "Person" to Person.new completely getting rid of the class named "Person" since its now a instance of what it suppose to be (which WAS a class)

so on line 9 of the code....

Person2 = Person.new ("Blue", 1)

I made a variable called "Person2". Now Ruby KNOWS that I want a variable called "Person2" since there isn't anything else in the program to throw confusion like "Person" did. So now when I tell Ruby that I want to make new object out of the Person class its now like, "What class ? . Sorry but I can't make an object when there is no class nor can I run the 'new' method." Since the Person class is no gone.


So is that basically whats happening here ? If that's the case, thanks you everyone for the quick replies. I really appreciate it. Now I remember the importance of lower-casing your variables, and keeping your constants/classes captialized.

I'll keep that advice in mind lol


 
That's basically it.
Mostly what the problem is...

Basically what your doing in that code is making Person into an object based on the Person class. Then trying to call a method from the Person object that does not exist and set the result to Person2.

The Person class does still exist, but you cannot access it wherever the Person object is defined.

That is why it is throwing errors that no method is existing, for .new is not defined in the Person class.

edit: mostly saying this for it could solve some other errors that could occur to you in the future if you understand it.
Ahhhh.... I see, it makes a lot of sense now. At first glance I was like "Wait....What exactly is wrong with this..?", because I didn't see an error, nor did I understand what was producing it. Now that you guys broke it down for me, its a lot more clearer to me now.

Thank you so much! ( ◜◡◝) 
One does not simply name two objects using the same name... 🙂
Case sensitivity can be confusing at first.

Note the difference in the following code:

Code:
class Person  def initialize(color, number)    @color, @number = color, number  endendperson = Person.new("Red", 2) person2 = Person.new("Blue", 1)

Original thread: https://forums.rpgmakerweb.com/threads/ace-nomethoderror-occured-when-creating-a-object.35523/