Jes Jes - 1 year ago 45
Ruby Question

Ruby, adding multiple objects to an array at once

I have Shop class, and I want to add multiple items at once.
I want this:

shop1 =
product1 ="Dress", 50)
shop1.add_products(product1, 5)

to add 5 dresses to warehouse

def add(product, qty)
@products << product * qty

so later I can use{|p| == "Dress"}.count

and get 5. Is it possible?

Answer Source

The easiest way I think is:

def add(product, qty)
  @products += [product] * qty

But it all comes down to your syntax preferences.