【问题标题】:`initialize': wrong number of arguments (given 3, expected 0) (ArgumentError)`initialize':参数数量错误(给定 3,预期为 0)(ArgumentError)
【发布时间】:2017-05-29 19:06:46
【问题描述】:

我有以下代码:

load 'Point.rb'
class Triangle

    def initialize(x, y , z)
        if !x.is_a?(Point) || !y.is_a?(Point) || !z.is_a?(Point)
            puts "Invalid data, a Triangle can only be initialized through points"
        else
            @x=x
            @y=y
            @z=z
            @type=type(x, y, z)
        end
    end

    def type(x, y, z)
        if x.distance(y) == y.distance(z) && y.distance(z) == z.distance(x)
            return "equilateral"
        elsif x.distance(y)== y.distance(z) || y.distance(z) == z.distance(x) || z.distance(x) == x.distance(y)
            return "isosceles"
        else
            return "scalene"
        end
    end

    attr_accessor :type
end

我这样调用方法:

load 'Triangle.rb'

x=Point.new(0,0)
y=Point.new(1,1)
z=Point.new(2,0)
triangle=Triangle.new x, y, z
puts triangle.type

Point类如下:

class Point

    def initialize (x=0, y=0)
        @x=x.to_i
        @y=y.to_i
    end

    def ==(point)
        if @x==point.x && @y==point.y
            return true
        else
            return false
        end
    end

    def distance(point)
        Math.hypot((@x-point.x),(@y-point.y))
    end

    attr_accessor :y
    attr_accessor :x
end

错误如下:

Triangle.rb:11:in `initialize': wrong number of arguments (given 3, expected 0) (ArgumentError)
    from Triangle_test.rb:6:in `new'
    from Triangle_test.rb:6:in `<main>'

请判断整个代码是否只是垃圾。

【问题讨论】:

  • 你没有显示你打电话给.new的地方。另外,你调用的type 方法是什么?
  • 该错误究竟是在哪里(文件名和行号)引发的?您如何尝试初始化实例? type 是如何定义的?
  • 在那里,我尽可能多地澄清。如果问题太长,我很抱歉。
  • “如果整个代码只是垃圾,请告诉我” - 代码,ahem,没有完全遵守ruby style guidelines。但你几乎让它工作了。这才是最重要的。
  • 另外,友好的建议:学习如何调试和调查问题。这是程序员可以拥有的最重要的技能之一。在您的情况下,错误消息直接指向一行代码。这是您应该首先看到的。

标签: ruby


【解决方案1】:

在您的Triangle 类中,您有方法type,它接受三个参数,然后在下面您有attr_accessor :type,它用无参数的getter 覆盖该3 参数方法。

这就是为什么在初始化程序中执行此操作时会出现该错误

@type=type(x, y, z)

【讨论】:

  • 非常感谢,我刚刚把三参数“type”方法的名字改成了“triangle_type”。
  • @azurox 是的,这应该可以解决问题。
  • @azurox 哦,顺便说一句(因为您是 stackoverflow 的新手),您应该接受最好的/最有帮助的(答案左侧的绿色复选标记)。
【解决方案2】:

这是您的代码的清理版本:

  • 删除了不需要的 if
  • 删除了不需要的退货
  • 定义了一个私有的calculate_type方法
  • attr_accessor 替换为attr_reader
  • x,y,z 重命名为 a,b,c 以避免坐标和点之间的混淆

class Point
  attr_reader :x, :y
  def initialize(x = 0, y = 0)
    @x = x.to_i
    @y = y.to_i
  end

  def ==(point)
    @x == point.x && @y == point.y
  end

  def distance(point)
    Math.hypot((@x - point.x), (@y - point.y))
  end
end

class Triangle
  attr_reader :a, :b, :c, :type

  def initialize(a, b, c)
    raise 'Invalid data, a Triangle can only be initialized through points' unless [a, b, c].all? { |p| p.is_a?(Point) }
    @a, @b, @c = a, b, c
    @type = calculate_type
  end

  private

  def calculate_type
    if a.distance(b) == b.distance(c) && b.distance(c) == c.distance(a)
      'equilateral'
    elsif a.distance(b) == b.distance(c) || b.distance(c) == c.distance(a) || c.distance(a) == a.distance(b)
      'isosceles'
    else
      'scalene'
    end
  end
end

a = Point.new(0, 0)
b = Point.new(1, 1)
c = Point.new(2, 0)
triangle = Triangle.new a, b, c
puts triangle.type
# isosceles

【讨论】:

  • 谢谢,我对这一切还是很陌生。基本上我们明天就要开始上 ruby​​ 课了,但我很兴奋,开始自学。在阅读和搜索您的代码之前,我不知道 ruby​​ 中存在异常,也不知道您可以创建私有方法。非常感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 2021-12-07
  • 2023-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多