Is there a way to prepend each element of an array with something.
eg:
file = File.new(my_file,'r')
header = IO.readlines(my_file)[1] # header looks like [1,2,3]
#Prepend each elelement of header with filename, something like
header.prepend(filename+".") #header looks like [filename.1,filename.2,filename.3]
You want to use map:
["foo", "bar", "baz"].map { |word| "prepend-#{word}" }
#=> ["prepend-foo", "prepend-bar", "prepend-baz"]
classic case for this method. This method can also take an array containing elements of any type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With