【问题标题】:Method undefined error - Ruby方法未定义错误 - Ruby
【发布时间】:2018-03-21 04:05:16
【问题描述】:

这是数字分区的代码,在最后一节中我遇到了麻烦,因为由于方法错误,我无法得到分区的数字。

class Num_Part

  def Particiones n     //method for the number partititons
    if n == 1
      return [[1]]
    elsif n < 1
      return [[]]
    end
    Particiones(n)
    listaparticiones = []
    for k in (n)..downto(0)
      cola= Particiones(n) - k
      for x in cola
        particion= [k] + x
      end
      listaparticiones << particion
    end
    return listaparticiones
  end

end

puts "Ingrese el Valor de N: \n" // this is where i ask for the value or the 
n =Integer(gets.chomp)            //number to be parted
lista = Particiones(n)

    print "\n cantidad de particiones: " +String(lista.size)

lista.each do |i|
  print [i]
end
obj = Num_Part.new
obj.Particiones(n)

【问题讨论】:

    标签: ruby class methods


    【解决方案1】:

    你在这里定义一个实例方法

    class Num_Part
    
      def Particiones n 
    

    你称它为类方法

    Num_Part.Particiones(n)
    

    因此,要么将定义更改为def self.Particiones n,要么将调用更改为Num_Part.new.Partitiones(n)

    顺便说一句,你的名字不符合惯例。例如方法通常都是小写的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多