35

I found this code:

.each_key {|a| self[a].strip! if self[a].respond_to? :strip! }

...but it is for a hash, whereas I am trying to do the same with an array.

4 Answers 4

81

This is what collect is for.

The following handles nil elements by leaving them alone:

yourArray.collect{ |e| e ? e.strip : e }

If there are no nil elements, you may use:

yourArray.collect(&:strip)

...which is short for:

yourArray.collect { |e| e.strip }

strip! behaves similarly, but it converts already "stripped" strings to nil:

[' a', ' b ', 'c ', 'd'].collect(&:strip!)
=> ["a", "b", "c", nil]

https://ruby-doc.org/core/Array.html#method-i-collect

https://ruby-doc.org/core/String.html#method-i-strip

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

2 Comments

I am really new to Ruby, but isn't .strip should never return nil. .strip! though returns nil if it didn't strip anything. I can confirm that with interactive shell (irb).
Can you explain why you did the || x?
10

If you don't mind first removing nil elements:

YourArray.compact.collect(&:strip) 

https://ruby-doc.org/core/Array.html#method-i-compact

1 Comment

FYI you don't want to call compact on Array itself. For your answer supply something like a.compact.collect(&:strip) or use yourArray as in Jamie's answer. I know you are going for pseudocode but it is helpful to the questioner to be technically precise.
4

If you are using Rails, consider squish:

Returns the string, first removing all whitespace on both ends of the string, and then changing remaining consecutive whitespace groups into one space each.

yourArray.collect(&:squish)

1 Comment

Just to shorten it a bit. myArray.collect(&:squish)
3

Adapting the approach you found, from working on a hash to working on an array:

[' c ', 'd', nil, 6, false].each { |a| a.strip! if a.respond_to? :strip! }
=> ["c", "d", nil, 6, false]

3 Comments

I am doing row.each {|a| a.strip! if a.respond_to? :strip! } (and have tried row2 = row.each {|a| a.strip! if a.respond_to? :strip! } to make sure it wasn't that , but the items still have the white space. Not sure it helps, but I am doing this on a csv file that I am pulling in with FasterCSV. I could probably clean up the CSV output, but would be useful to know this for the future anyways.
@Toby (to updated comment) Check which type array elements have (e.g., puts x[0].class). If it's not a string, you can't use string's strip! method on them.
turns out I am just dumb, I didn't know that if you use :headers => true, it would make each item an array with the header as the first element, and the value as the second, I got it to work on the second item, then found out it was just as easy to add -W to sqlcmd to output from mssql without the whitespace. Thanks for everyone's help.

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.