Ruby, adding multiple objects to an array at once -
i have shop class, , want add multiple items @ once. want this:
shop1 = shop.new product1 = product.new("dress", 50) shop1.add_products(product1, 5)
to add 5 dresses warehouse
def add(product, qty) @products << product * qty end
so later can use
@products.select{|p| p.name == "dress"}.count
and 5. possible?
the easiest way think is:
def add(product, qty) @products += [product] * qty end
but comes down syntax preferences.
Comments
Post a Comment