Ruby简单类问题

时间:2016-09-19 20:03:02

标签: ruby class

我已经到了它看起来似乎有效但它不是......

class Person1
  def initialize (name, surname, age)
    @name = name
    @surname = surname
    @age = age
  end

  def introduction
    p @name
    p @surname
    p @age
  end
end

我称之为:

ken = Person1.new ("Ken", "Bolo", 4)
ken.introduction

并与编译器有关于我传递给实例化对象的参数数量的参数。 3它告诉我,我有很多,2它告诉我它太多了,1它告诉我它太少了!坚果!

提前致谢。

1 个答案:

答案 0 :(得分:3)

只需删除方法名称和括号之间的空格:

class Person1
  def initialize(name, surname, age)
    @name = name
    @surname = surname
    @age = age
  end

  def introduction
    p @name
    p @surname
    p @age
  end
end

ken = Person1.new("Ken", "Bolo", 4)
ken.introduction
#=> "Ken"
#=> "Bolo"
#=> 4