1

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!

4
  • 1
    It seems like the syntax error is coming from the argument being passed to #push - you're passing in a string interpolation of line, but not wrapping it in a string. Try changing it to "#{line}". Commented Jan 8, 2018 at 18:32
  • 1
    You mean IO::readlines e.g. File.readlines("infile.txt") Commented Jan 8, 2018 at 18:36
  • @engineersmnky I did not want to use this as I get symbols for newlines at the end of each of the array elements, which I do not want. Although there are probably ways to remove those (using chomp as suggested below, for example). Thanks for the suggestion. Commented Jan 8, 2018 at 19:10
  • 1
    Very well then File.foreach("infile.txt").map {|line| line.chomp } Commented Jan 8, 2018 at 19:12

2 Answers 2

1

Your problem is that you're trying to use string interpolation (#{...}) outside a string:

ARRAY.push(#{line})
# ---------^^^^^^^

You could use string interpolation by throwing in some double quotes:

ARRAY.push("#{line}")

but there's no need to convert a string (line) to an identical string ("#{line}") so you could just push straight onto the array:

ARRAY.push(line)

Or you could just skip all that explicit iteration and use #to_a:

array = File.open("infile.txt").to_a

And if you wanted to strip off the newlines:

array = File.open('infile.txt').map(&:chomp)

As engineersmnky points out in the comments, using File.readlines would be a better approach:

array = File.readlines('infile.txt')
array = File.readlines('infile.txt').map(&:chomp)
#...

And don't forget to check IO as well as File for methods when working with files.

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

1 Comment

In the final 2 options presented the file will remain open because a block is not passed to open and since it remains unassigned you will have to wait for GC to collect it before it is closed. Maybe instead use File.readlines('infile.txt') for the first and File.open('infile.txt') { |f| f.map(&:chomp) } or File.foreach('infile.txt').map(&:chomp) for the second.
0

You can also do this:

array = []

IO.foreach("path/to/file.txt") { |line|

  array.push(line.chomp)

} 

Then if you want to clear the array from empty lines just use delete:

array.delete("")

Comments

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.