Getting ArgumenError while trying to create instance of a ruby class

222 views Asked by At

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?

3

There are 3 answers

6
Paweł Dawczak On BEST ANSWER

I'd say, that the error of number of arguments (1 for 0) is caused by your set_first_name and set_last_name methods, which are declared not to accept any arguments:

class EncapsulatedPerson
  # ...

    def set_first_name
        @first_name = name
    end

    def set_last_name
        @last_name = name
    end

end

Try updating them, so they accept arguments:

class EncapsulatedPerson
  # ...

    def set_first_name(name)
        @first_name = name
    end

    def set_last_name(name)
        @last_name = name
    end

end

Hope this helps!

EDIT

The complete code should look like:

class EncapsulatedPerson
  attr_reader :first_name, :last_name

  def initialize(name)
    set_name(name)
  end

  def name
    @first_name + " "+ @last_name
  end

  def set_name(name)
    first_name, last_name = name.split(/\s+/)
    set_first_name(first_name)
    set_last_name(last_name)
  end

  def set_first_name(name)
    @first_name = name
  end

  def set_last_name(name)
    @last_name = name
  end

end

epa = EncapsulatedPerson.new('William Wallace')
puts epa.first_name
1
jon snow On

Oh Dear, your class definition is wrong, change set_first_name and set_last_name methods in EncapsulatedPerson like this.

def set_first_name(name)
    @first_name = name
end

def set_last_name(name)
    @last_name = name
end
0
Amit Thawait On

In the initialize method, you are calling set_name method which accepts 1 argument :

def initialize(name)
    set_name(name)
end

whereas your set_name method definition doesn't accept any arugument :

def set_name
    first_name, last_name = name.split(/\s+/)
    set_first_name(first_name)
    set_last_name(last_name)
end

As per you design since the method name is already created as an instance method, no need not pass it as an argument to set_name.

But I would say you should modify your class as shown below.

To fix your issue, change the class like this :

class EncapsulatedPerson
  def initialize(name)
    @first_name, @last_name = name.split(/\s+/)
    set_name(@first_name, @last_name)
  end

  def name
    @first_name + " "+ @last_name
  end

  def set_name(first_name, last_name)
    set_first_name(first_name)
    set_last_name(last_name)
  end

  def set_first_name(first_name)
    @first_name = first_name
  end

  def set_last_name(last_name)
    @last_name = last_name
  end

end