Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot Initialize Object in Ruby

I am trying to initialize an object in Ruby, but I get an error: "undefined method `Thing’ for main:Object (NoMethodError)"

However, isn’t that what the initialize method is for?

class Thing
  def initialize(id, color)
    @id = id
    @color = color
  end
end

Thing thing = Thing.new(1, "blue")

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Local variables are created by simply assigning to them. You don’t have to declare them or specify their type. (in fact, you can’t)

Just write:

thing = Thing.new(1, "blue")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading