steve_gallagher steve_gallagher -5 years ago 141
Ruby Question

Trying to open a file in Ruby - Getting TypeError: can't convert String into Integer

Not sure whats going on here, or what could be the integer in this case. Here's the code:

def build_array_from_file(filename)
contents = []'lib/project_euler/' + filename), 'r') do |file|
while line = file.get
contents << line

filename is a string and I've checked to make sure the path comes up valid.

Any thoughts? Thanks.

Answer Source has no second argument for mode nor block, that's

contents_string ='lib/project_euler/' + filename))

Note that you can also write:

contents = # returns a lazy enumerator, keeps the file open


contents = File.readlines(path) # returns an array, the file is closed.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download