Is there a way to add something to each element of the array.
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]
Do you want to use the map:
["foo", "bar", "baz"].map { |word| "prepend-#{word}" } #=> ["prepend-foo", "prepend-bar", "prepend-baz"]
classic case for this method. This method can also accept an array containing elements of any type.
We have a default method for adding elements to an array.
header = [1, 2, 3] header.map { |h| h.to_s.prepend("filename.")}}
Output:
["filename.1", "filename.2", "filename.3"]
Source: https://habr.com/ru/post/1384242/More articles:Incorrect ruby ββversion used - ruby-on-railsNodeJS Delayed Socket.IO - javascriptString to jQuery object, how to remove elements - jqueryMapReduce - Anything Else Beyond Word Counting? - multithreadingInvalid email subject title for subject> 75 characters using codeigniter email lib - emailComparing integers with warning different signs using Xcode - buildHow to access a button inside a list? specially using button_click to perform an action - vb.netProblem with SqlDataSource call and stored procedure - sqlAndroid Monkey does not generate HPROF reset - androidSaving chat messages inside MySql table - phpAll Articles