-2

I am trying to create a User class in Ruby On Rails App, and when I call NETWORKING::User.new(1,"Testing") , it returns *** ArgumentError Exception: wrong number of arguments (2 for 0)

Not sure why?

class NETWORKING::User 
   mattr_writer :id, :name

 class << self

    def initialize(id, name)
        @id = id
        @fullname = name

    end

end
end
1
  • 3
    Why did you include class << self, what did you think that would accomplish here? Commented Mar 2, 2018 at 21:36

1 Answer 1

2

class << self serves no purpose here except to break your code. Remove it:

class NETWORKING::User 
  attr_accessor :id, :name

  def initialize(id, name)
    @id = id
    @fullname = name
  end
end
Sign up to request clarification or add additional context in comments.

7 Comments

But when I remove it and try to access the id using NETWORKING::User.id it shows undefined method.
@Wings2fly That makes no sense at all. What should NETWORKING::User.id return? It seems like this is a fundamental misunderstanding in how OOP works.
stackoverflow.com/questions/39694071/… Referred to the answer here.
user = NETWORKING::User.new(1,"Test") ; puts user.id
There is no instance of class << self in that link.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.