0

好的,伙计们,我正在学习 ruby​​,但我在教程中遇到了一些麻烦。我想知道你能不能帮帮我!

采取以下代码:

class Dish
def initialize(name, ingred, descrip)
    @name = name
    @ingred = ingred
    @descrip = descrip
end
def name
    @name
end
def name=(new_name)
    @name = new_name
end
def ingred
    @ingred
end
def ingred=(new_ingred)
    @ingred = new_ingred
end
def descrip
    @descrip
end
def descrip=(new_descrip)
    @descrip = new_descrip
end
def display  
    puts "I am a #{@name} and my ingredient is #{@ingred} and my description is #{descrip}"  
    end
end
dis1 = Dish.new('Pizza', 'sauce', 'put sauce on that thing')
dis1.display

好的,这是我的问题,我希望我解释得足够好。到目前为止,我已经学会了在创建类的新实例时输入一个参数(即(名称、ingred、描述))。我想知道的是,如果一道菜有多种成分,我该如何将它添加到我的课堂上?另外,如果我想计算成分的数量或名称的数量,我该怎么做。我只是在学习课程,但在匹配我想用谷歌搜索的确切措辞时遇到了麻烦。谢谢!

4

3 回答 3

4

我会试着回答你的一些问题。为简化起见,我删除了您的变量descrip及其相关方法。你看我在*in 前面放ingred了一个initialize。这意味着可变数量的参数在name. 这是处理您关于拥有多种成分的问题的一种方法。这ingred是一个数组。由于@ingred设置等于ingred@ingred也是一个数组。如果您查看各种方法以及调用时打印的内容(显示在底部),您应该能够看到它是如何工作的。(编辑添加了一些功能。您可能需要在底部向下滚动。)

class Dish
  def initialize(name, *ingred)
    @name = name
    @ingred = ingred
  end
  def name
    @name
  end
  def name=(new_name)
    @name = new_name
  end
  def ingred
    @ingred
  end
  def ingred=(*ingred)
    @ingred = ingred
  end
  def add_ingred(ingred)
    @ingred << ingred
  end  
  def remove_ingred(ingred)
    @ingred.delete(ingred)
  end  
  def nbr_ingred
    @ingred.count
  end
end

dis1 = Dish.new("Pizza", "sauce", "crust", "cheese", "anchovies")
p dis1.ingred #=> ["sauce", "crust", "cheese", "anchovies"]
dis1.add_ingred("olives")
p dis1.ingred #=> ["sauce", "crust", "cheese", "anchovies", "olives"]
dis1.add_ingred(["capers", "mushrooms"])
p dis1.ingred #=> ["sauce", "crust", "cheese", "anchovies", "olives", ["capers", "mushrooms"]]
dis1.ingred.flatten!
p dis1.ingred #=> ["sauce", "crust", "cheese", "anchovies", "olives", "capers", "mushrooms"]
dis1.remove_ingred("anchovies")
p dis1.ingred #=> ["sauce", "crust", "cheese", "olives", "capers", "mushrooms"]
p dis1.nbr_ingred #=> 6
dis1.ingred = "olives", "pineapple" # treated as ["olives", "pineapple"]
p dis1.ingred #=> [["olives", "pineapple"]]
dis1.ingred = ["cheese", "crust"]
p dis1.ingred #=> [["olives", "pineapple"]]
dis1.ingred.flatten!
p dis1.ingred #=> ["olives", "pineapple"]
于 2013-09-26T02:36:53.457 回答
0

Use arrays.

class Dish
    class Ingredient
        attr_accessor :name, :description

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

        def to_s
            "#{name} - #{description}"
        end
    end

    attr_accessor :name, :description, :ingredients

    def initialize(name, description)
        @name = name
        @description = description
        @ingredients = []
    end

    def to_s
        "#{name} - #{description}\n #{ingredients.join("\n").to_s}"
    end
end

pizza = Dish.new("Pizza", "Italian style pizza")
pizza.ingredients << Dish::Ingredient.new("Tomato Sauce", "Juicy Juicy Tomato Sauce.")
pizza.ingredients << Dish::Ingredient.new("Cheese", "Cheese, duh.")

puts pizza.to_s
于 2013-09-26T02:22:24.580 回答
0

由于我之前的两个答案都省略了描述参数,因此我将窃取 Cary Swoveland 的答案并添加描述参数:

class Dish
  attr_accessor :name, :descrip
  def initialize(name, *ingred, descrip) # Only in Ruby 1.9+
      @name = name
      @ingred = *ingred
      @descrip = descrip
  end
  def ingred
    @ingred
  end
  def ingred=(*ingred)
    @ingred = ingred
  end
  def add_ingred(ingred)
    @ingred << ingred
  end  
  def remove_ingred(ingred)
    @ingred.delete(ingred)
  end  
  def nbr_ingred
    @ingred.count
  end
  def display  
      puts "I am a #{@name} and my ingredient is #{@ingred.join(', ')} and my description is #{descrip}"  
  end
end

dis1 = Dish.new('Pizza', 'sauce', 'ham', 'put ingredients on that thing.')
dis1.add_ingred('fish')
dis1.display #=> I am a Pizza and my ingredient is sauce, ham, fish and my description is put ingredients on that thing.
于 2013-09-26T07:44:49.023 回答