Replace each element in the array with an empty element

This is admittedly a bit of a strange problem, but I need to basically empty every element in the array (but keep the element itself).

For example, if I have this array: [ 0, 5, 4, 7, 1 ]

I need to change it to: [ '', '', '', '', '']

I am using Ruby 1.9.3.

Some charting software that I use requires an array for labels, and the only way to hide these labels is to make the corresponding elements empty. Yes, lame.

+4
source share
1 answer

Enumerable#map replaces each element with the result of a block call:

 array = [ 0, 5, 4, 7, 1 ] array.map { '' } #=> ['', '', '', '', ''] 

If you want to change the source text (if I understand your question, this is exactly what you are NOT doing), then use #map!

 array.map! { '' } array #=> ['', '', '', '', ''] 
+5
source

Source: https://habr.com/ru/post/1494373/


All Articles