ruby - Accessing instance variable in `each` loop -
is possible this?
customer1.shopping_cart.each |item| puts("#{item.name}") the item class has attr_reader instance variable name.
customer1.shopping_cart.each |item| puts("#{item.name}") end this put names every item customer1.shopping_cart collection (presumably array or set)
upd: bit more idiomatic syntax:
customer1.shopping_cart.map(&:name).each(&method(:puts))
Comments
Post a Comment