简体   繁体   中英

Reading input from text file into an array in ruby

I want to read data from a text file and retrieve just the ip_addresses contained in the file.

The file contains data as:

[#<host:192.168.2.135>, #<host:175.41.142.49>, #<host:20:aa:4b:9b:eb:8c>, #<host:20:7c:8f:10:38:a0>, #<host:192.168.2.1>, #<host:239.255.255.250>]

From these data, i just want the host addresses like 192.168.2.135 and so on and store them in an array.Could somebody please help me with this? Thanks in advance.

open(path_to_file, &:read).scan(/#<host:(.+?)>/).flatten

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM