I have a doubt [Ruby] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I have a doubt [Ruby]

[English] When i wrote this code: class Person attr_accessor :name, :age def initialiaze( name, age ) @name = name @age = age end def change( n, a ) self.name = n self.age = a end def show_info puts('#{self.name} is #{self.age}') end end p = Person.new("David",28) p.change("Bob",42) p.show_info it appears me this: Traceback (most recent call last): 2: from clases2.rb:17:in `<main>' 1: from clases2.rb:17:in `new' clases2.rb:17:in `initialize': wrong number of arguments (given 2, expected 0) (ArgumentError) [Españo] Cuando escribo este código: class Person attr_accessor :name, :age def initialiaze( name, age ) @name = name @age = age end def change( n, a ) self.name = n self.age = a end def show_info puts('#{self.name} is #{self.age}') end end p = Person.new("David",28) p.change("Bob",42) p.show_info me aparece esto: Traceback (most recent call last): 2: from clases2.rb:17:in `<main>' 1: from clases2.rb:17:in `new' clases2.rb:17:in `initialize': wrong number of arguments (given 2, expected 0) (ArgumentError)

12th Mar 2021, 3:52 PM
Marco Useche
2 Answers
0
it is initialize not initialiaze . And put double quotes to output data . single quotes won't evaluate expression .
12th Mar 2021, 4:02 PM
TOLUENE
TOLUENE - avatar
0
Like Md Sayed said, you've misspelled it. It should be def initialize. A method called initialize runs internally when you call .new on your Person class. If you want to take control of this method, you need to define it yourself. In your case, Ruby doesn't recognise your arguments because you haven't defined the method correctly because of the misspelled method name. This is why the error message states that you've provided the wrong number of arguments to the initialize method. It says given 2 - that's the two you've given when calling .new - but it's expecting 0 because it thinks you haven't defined an initialize method.
21st Mar 2021, 10:36 AM
CamelBeatsSnake
CamelBeatsSnake - avatar