Pergunta

I am fairly new to Ruby and was wondering if there is a way to extract unique struct values within an array.

I have the following struct:

Struct.new(:source_ip_address, :destination_ip_address, :port)

Within my code I am getting these struct values from a file, then storing them in an array:

ip_port_list

So basically for each line in the file, a new struct is created. That struct is then added to the ip_port_list array. The next thing I am trying to do is pull out all the unique port values within the entire array. Is there a way to do this? Any help would be greatly appreciated.

Foi útil?

Solução

ip_port_list.map(&:port).uniq

Should do it.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top