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

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -