ArgumentError: wrong number of arguments in Ruby

匿名 (未验证) 提交于 2019-12-03 08:54:24

问题:

Trying to solve this problem,

  class Person       def initialize(name)         @name=name       end        def greet(other_name)        puts "Hi #{other_name}, my name is #{name}"       end     end      initialize("ak")     greet("aks") 

but I am getting the error like:

ArgumentError: wrong number of arguments calling `initialize` (1 for 0) 

I don't understand what is asking here, if its just the argument then why the error is like (1 for 0). can someone help me understand this problem.

回答1:

Look at this code:

class Person   attr_reader :name    def initialize( name )     puts "Initializing Person instance #{object_id}"     @name = name   end    def greet( name )     puts "Hi #{name}, I'm #{name()}"   end end 

When you wrote initialize without explicit receiver:

initialize( "ak" ) 

It was only a matter of luck that your message was recognized. Look who has responded:

method( :initialize ).owner #=> BasicObject 

BasicObject, the foremother of all Object instances, herself responded to your call, by scolding you about wrong number of arguments, because:

method( :initialize ).arity #=> 0 

Not only this method does not expect any arguments, but also you are not expected to call it at all. In fact, you are not expected to call #initialize on any object by yourself, save for exceptional situations. Class#new method handles calling of Person#initialize method for you:

A = Person.new( 'Abhinay' ) Initializing Person instance -605867998  #=> #<Person:0xb7c66044 @name="Abhinay"> 

Person.new handled creation of a new instance and automatically called its #initialize method. Also, #initialize method is created private, even if you did not specify it explitcitly. The technical term for such irregular behavior is magic. Person#initialize is magically private:

A.initialize( 'Fred' ) NoMethodError: private method `initialize' called for #<Person:0xb7c66044 @name="Abhinay"> 

You cannot just reinitialize yourself to 'Fred', you know. All other methods are public unless prescribed otherwise:

A.greet "Arup" Hi Arup, I'm Abhinay #=> nil 


回答2:

You need to call the methods on the object (not just call the methods) and initialize is automatically called when creating a new object:

p = Person.new("ak") p.greet("aks")          #=> "Hi aks, my name is ak" 


回答3:

The problem is that to create new object you need to call method new on class, and not initialize on the object.

So code looks like this:

p = Person.new("John") 


回答4:

Please, take a look at code below:

class Person   attr_reader :name    def initialize(name)     @name = name   end    def greet(other_name)     puts "Hi #{other_name}, my name is #{name}"   end end  person = Person.new("ak") person.greet("aks")  #=> Hi aks, my name is ak 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!