Kimberly Wright Kimberly Wright - 5 months ago 10
Ruby Question

Initialize Ruby codes error

I tried to run these codes:

class Dog
def set_name(name)
@dogname = name
end

def get_name
return @dogname
end

def talk
return "awww"
end

def initialize(title, description)
@title = title
@description = description
end
end

doggy = Dog.new
doggy.set_name('Sam')
puts doggy.get_name
puts doggy.talk


bogart = Dog.new('The Book', 'The road not taken')
puts bogart.to_s
puts bogart.inspect


I did make sure every argument is correct. However, I got the following errors.

C:\Ruby200\bin\ruby.exe -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) C:/Users/Todd/RubymineProjects/untitled1/test.rb
C:/Users/Todd/RubymineProjects/untitled1/test.rb:15:in `initialize': wrong number of arguments (0 for 2) (ArgumentError)
from C:/Users/Todd/RubymineProjects/untitled1/test.rb:22:in `new'
from C:/Users/Todd/RubymineProjects/untitled1/test.rb:22:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'

Process finished with exit code 1


Tried my best can't find the issue. Any idea where I miss?

Answer
class Dog

  def set_name(name)
    @dogname = name
  end

  def get_name
    return @dogname
  end

  def talk
    return "awww"
  end

  def initialize(title, description)
    @title = title
    @description = description
  end

end

#That will cause an error because your new method have two arguments.
doggy = Dog.new



bogart = Dog.new('The Book', 'The road not taken')
bogart.set_name('Sam')
puts bogart.get_name
puts bogart.talk
puts bogart.to_s
puts bogart.inspect
Comments