*将实例插入Array时,评估错误*

时间:2017-08-21 10:04:55

标签: ruby

我有Category课程:

  class Category
    include Comparable

    attr_reader :name

    ##
    # Initialize the Category
    #
    # +name+ is the name of the current Category
    # +context+ is the Liquid:Tag context
    #
    def initialize(name, context)
      @name = name
      @context = context
    end

    ##
    # Compare this Category against another Category.
    # Comparison is a comparison between the 2 names.
    #
    def <=>(other)
      return nil unless name.is_a? String
      name <=> other.name
    end
  end

我想创建一个Categories类,它接受一个字符串数组并将其转换为Category的数组。这是我写的:

  class Categories
    ##
    # Initialize the list of categories by passing a list String
    #
    # +category_names+ A list of strings representing a list of categories.
    # +context+ is the Liquid:Tag context
    #
    def initialize(category_names, context)
      category_names = [category_names] unless category_names.is_a? Array
      byebug
      @category_list = category_names.map{|string| Category.new(string, context)}
    end
  end

当我尝试调试时,我有以下内容:

(byebug) category_names.map
#<Enumerator: ["Heroku"]:map>
(byebug) cat = Category.new("Heroku", context)                                                                          
#<Jekyll::Category:0x00000002a85000>
(byebug) cat.name
"Heroku"
(byebug) @category_list = [cat]
*Error in evaluation*

失败并显示错误*Error in evaluation*。我无法理解为什么。有人能帮助我吗?

0 个答案:

没有答案