0

I have array output I am outputting to the Show page like this:

@store.products.map(&:title)

But the output to the page includes the [" "] that I don't want, like:

Product: ["Hair Pins"] 

How can I get rid of the brackets and quotes? Why isn't it just returning the string without the characters?

I have tried strip, chomp, and those don't work. Is there a regular expression I can use to clip off the brackets and quotes?

Thank you!

3
  • 3
    please provide the code used to render the output and I'm sure we can fix this real quick. Where is "Product: " coming from? Commented May 21, 2014 at 20:19
  • 1
    This looks like the output of p rather than the output of to_s Commented May 21, 2014 at 20:19
  • Are the brackets in the database? Commented May 21, 2014 at 20:30

2 Answers 2

1

If you want to print the products on a single line this should work:

@store.products.map(&:title).join(",")

So you have a String instead of an Array being rendered

Sign up to request clarification or add additional context in comments.

1 Comment

Sweet! the .join(",") did the trick. Thank you. I had a feeling it was something simple.
0

You can get rid of the brackets and quotes by using:

["Hair Pins"].gsub('[', '').gsub(']', '').gsub('"', '')

1 Comment

Most likely, the brackets are being added unintentionally. The best solution is to identify where that's happening and not put them there in the first place.

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.