O
O
Ord EO2020-08-05 15:36:46
ruby
Ord EO, 2020-08-05 15:36:46

How to drive the contents of an external file into an array in ruby?

There is a file with a list of domains
. Is it possible to somehow assign an array of all these domains, separated by commas, to some variable using pure ruby?
This file will also be added to the code, so I need the option to read not from a link from the Internet, but from a finished file that I will place in the same directory as the code

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artur Bordenyuk, 2020-08-05
@HighQuality

Of course you can

require 'open-uri'
require 'csv'

url = 'https://data.iana.org/TLD/tlds-alpha-by-domain.txt'
data = open(url)
CSV.new(data, headers: true).each do |((_, domain))|
  puts domain
end

R
Roman Mirilaczvili, 2020-08-05
@2ord

Maybe. And what are the difficulties?
Read the documentation for the each_line method of the File class. Or IO.foreach.
In a variable list, I hope, there will be no difficulty to bring.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question