Hi, I have done this method in order to put the content of a txt file
into an array. Now I should have to construct a bucle out of the method
to print the content of the array in the screen but I dont’t know what
to do. This is my method:
def cargar_tokens(txtFileName)
tokens=[]
txtFile = File.open(txtFileName)
txtFile.each(" ") do |palabra|
if (palabra=~ /(.+)/)
token = $1
tokens.push(token)
end
end
txtFile.close
return tokens
end
if (ARGV.length<1)
puts “I need one argument: 1) name of the file”
else
tokens=cargar_tokens(ARGV[0])
end
def cargar_tokens(txtFileName)
tokens=[]
txtFile = File.open(txtFileName)
txtFile.each(" ") do |palabra|
if (palabra=~ /(.+)/)
token = $1
tokens.push(token)
end
end
txtFile.close
return tokens
end
if (ARGV.length<1)
puts “I need one argument: 1) name of the file”
else
tokens=cargar_tokens(ARGV[0])
end
It looks like Brian answered your question, but if you are interested in
the more idiomatic “ruby” approach you can do something more like:
def cargar_tokens(txt_file_name)
tokens = []
File.open(txt_file_name) do |file|
file.each_line(’ ') { |palabra| tokens << $1 if palabra =~ /(.+)/ }
end
tokens
end
if ARGV.empty?
puts “I need one argument: 1) name of the file”
else
tokens = cargar_tokens(ARGV.first)
end
The nice rubyisms are what make it fun, to me at least!