I would like to create an array in Ruby rake called ARRAY where each line of an infile ("infile.txt") is an element of the array. This is how I have tried it so far:
desc "Create new array"
task :new_array do
ARRAY=Array.new
end
desc "Add elements to array"
task :add_elements => [:new_array] do
File.open("infile.txt").each do |line|
ARRAY.push(#{line})
end
end
However, I get the following error:
syntax error, unexpected keyword_end, expecting ')'
for the end after "ARRAY.push(#{line})"
Can someone explain to me what the problem is or let me know of another way to do this?
Many thanks!
#push- you're passing in a string interpolation ofline, but not wrapping it in a string. Try changing it to"#{line}".IO::readlinese.g.File.readlines("infile.txt")File.foreach("infile.txt").map {|line| line.chomp }