5

I have the below class method:

def self.product(basket)
  Product.find(basket.to_a).collect do |product| 
    product.name + " " + product.size + " " + product.color
  end
end

The above produces the following:

["T-Shirt Medium Grey", "Sweatshirt Medium Black"]

I've tried the following:

def self.product(basket)
  a = Product.find(basket.to_a).collect do |product| 
    product.name + " " + product.size + " " + product.color
  end
  b = a.shift.strip
end

But this ends up only giving me the first part of the array T-shirt Medium Grey

I'm looking for it to give me

T-shirt Medium Grey, Sweatshirt Medium Black

Can anyone help?

Thanks

1
  • What is the output you are looking for? Commented May 8, 2014 at 10:39

2 Answers 2

21

Your problem is how to customize displaying array content. One of possible solution is converting to string using Array#join method:

a.join(', ')
# => "T-Shirt Medium Grey, Sweatshirt Medium Black"
Sign up to request clarification or add additional context in comments.

2 Comments

bravoooooooooooo!
again bravoooo! hehe
3

This should work:

def self.product(basket)
  Product.find(basket.to_a).map{|product| [product.name, product.size, product.color].join(" ")}.join(', ')
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.