I am trying to improve my OO ruby skills. I've created a class and when calling and trying to make ans instance of it, I get error.
Here is the code.
class EncapsulatedPerson
def initialize(name)
set_name(name)
end
def name
@first_name + " "+ @last_name
end
def set_name
first_name, last_name = name.split(/\s+/)
set_first_name(first_name)
set_last_name(last_name)
end
def set_first_name
@first_name = name
end
def set_last_name
@last_name = name
end
end
when I load the file, which I wrote above lines into, and attempt to create instances of this class
2.2.0 :004 > ep = EncapsulatedPerson.new('William Wallace')
ArgumentError: wrong number of arguments (1 for 0)
... #
As I understand from error, I should pass 0 arguments when creating new instance (However I believe def initialize(name) ...
necessiates one (name) argument'
2.2.0 :003 > ep = EncapsulatedPerson.new
ArgumentError: wrong number of arguments (0 for 1)
I've totally confused. How many arguments does new
method require when trying to instantiate it? One argument or no argument?
I'd say, that the error of
number of arguments (1 for 0)
is caused by yourset_first_name
andset_last_name
methods, which are declared not to accept any arguments:Try updating them, so they accept arguments:
Hope this helps!
EDIT
The complete code should look like: