This is a pretty trivial issue using String#split :
results = open('a.txt').map { |line| line.split(':')[0] } p results
Conclusion:
["10.199.198.10", "10.199.198.12", "10.199.198.13"]
String#split splits the string into the specified delimiter and returns an array; therefore line.split(':')[0] accepts the first element of this generated array.
If it has a string without : String#split will return an array with a single element, which is the entire string. Therefore, if you need to do some more error checking, you can write something like this:
results = [] open('a.txt').each do |line| results << line.split(':')[0] if line.include? ':' end p results
which will only add separator lines to the result array if the line contains the character :
source share