Forwarding Forwarding - 3 months ago 8
Ruby Question

How to read a list into an array in Ruby?

I've got a

.txt
file like this:

1039812323
2534563457
3457346731
2525146715


How do I pass this into an array and then access it's elements by index?

Answer

If you want an array

File.readlines("a.txt")

take care that you are going to have an string with "\n" at the end of each number

["1039812323\n", "2534563457\n", "3457346731\n", "2525146715\n"]

Then you can use any ruby's array iterator (each, collect, inspect, map, etc)