Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我是否可以使用高级ruby概念来改进这段代码,并且我还想打印带有项值的列表?_Ruby - Fatal编程技术网

我是否可以使用高级ruby概念来改进这段代码,并且我还想打印带有项值的列表?

我是否可以使用高级ruby概念来改进这段代码,并且我还想打印带有项值的列表?,ruby,Ruby,编写一个用于创建购物清单的简单DSL。我们应该能够指定商品名称和数量。。 差不多 我的代码适用于预定义的哈希映射,但一旦我接受用户输入以创建哈希映射,它就会失败,而且我还想通过使用更高级的概念来改进代码,以实现此结果。有什么建议吗 class Store def initialize(item, quantity) @products = { "item" => quantity } @cart = [] end def add_to_

编写一个用于创建购物清单的简单DSL。我们应该能够指定商品名称和数量。。 差不多

我的代码适用于预定义的哈希映射,但一旦我接受用户输入以创建哈希映射,它就会失败,而且我还想通过使用更高级的概念来改进代码,以实现此结果。有什么建议吗

class Store
    def initialize(item, quantity)
       @products = { "item" => quantity }
       @cart = []
    end

    def add_to_cart( item )
        @cart << item
    end

    def add_product( item, price )
        @products[item] = price
    end

    def cart_total
        @cart.inject(0){|sum, item| sum + @products[item]}
    end

    def items
        @products.join(', ')
    end
end

puts "Please provide item name"
item = gets.chomp
puts "Please provide quantity associated with item"
quantity = gets.chomp.to_i
store = Store.new(item, quantity)
store.add_to_cart(item)

puts store.cart       
printf "$%6.2f", store.cart_total 
类存储
def初始化(项目、数量)
@产品={“项目”=>数量}
@购物车=[]
结束
def添加到购物车(项目)

@cart这是一个相当广泛的问题,不应该如此,但我还是想谈谈我的看法

  • 使用默认值()初始化空哈希
  • 不要忘记访问实例变量()
  • 检查文档中此处使用的方法(,)
下面是一个可能的重构:

class Store

    attr_reader :cart, :products # you need to access the variables

    def initialize
        @cart = Hash.new(0) # initilize the Hash with default value
        @products = Hash.new
    end

    def add_to_cart(item, quantity)
        # increase by quantity, thanks to default value of the Hash,
        # only if the product is available
        if products.has_key? item # products thanks to the attr_reader, then see Hash docs
            cart[item] += quantity
            true
        else
            false
        end
    end

    def add_product(item, price)
        products[item] = price
    end

    def cart_total
        cart.sum { |k, v| products[k] * v }
    end

    def products_list
        products.keys
    end
end
因此,您可以按如下方式使用该类:

store = Store.new
store.add_product("Apple", 10.0 )
store.add_product("Lemon", 12.0 )
store.add_product("Melon", 20.0 )

store.products_list
#=> ["Apple", "Lemon", "Melon"]
store.products
#=> {"Apple"=>10.0, "Lemon"=>12.0, "Melon"=>20.0}

store.add_to_cart('Apple', 4) #=> true
store.add_to_cart('Melon', 5) #=> true
store.add_to_cart('Carrot', 1)  #=> false # not available, not added

store.cart # thanks to attr_reader
#=> {"Apple"=>4, "Melon"=>2}

store.cart_total
#=> 140.0

你也可以考虑定义一个
购物车
类…

不确定是谁给你这个任务的“DSL”是什么意思,但我认为这里没有。我还有一些其他建议,但要求改进的不是你:)是的@SergioTulentsev我把
商店
阅读器放在那里,以防万一……)建议总是受欢迎的:)好吧,给你:1)可枚举的#和接受一个块。2) 可用的产品检查效率非常低(每次都创建一个临时数组。为什么不直接使用散列?)是的,3)使用您的读卡器:)顺便说一句,如果还没有,请检查。Grrr,我无法理解您的意思:)