T
Tom Lynch
Greetings,
I'm very new to Ruby, but have been playing around with validating IP addresses from a .yml file. Everything works but would like to make sure I'm doing things the proper Ruby way (I program in Perl non-OO and bash). The code:
require 'ipaddr'
require 'yaml'
yml = YAML.load_file("/data/dna.yml")
#
# Check hash for key...
#
if (yml.has_key?(board))
puts "Found Key: #{board}"
if (yml[board]["ports"])
yml[board]["ports"].each do |ip|
ipaddr = IPAddr.new "#{ip}"
if (ipaddr.ipv4?)
puts "Good: IPV4: #{ip}"
elsif (ipaddr.ipv6?)
puts "Good: IPV6: #{ip}"
else
puts "ERROR: Not an IPV4 or IPV6 format #{ip}..."
end
end
else
puts "Ports not found?"
end
else
puts "Key Not Found: #{board}"
end
My concern is in the each loop with IPAddr.new "#{ip}", is this the proper way to do this in Ruby? In Perl it might have been "ipv4($ip);" or equivalent without need to do the IPAddr.new on each run through the loop.
Just need to understand Ruby/Libraries and bast practices usage.
Thanks for any help in advance.
Tom
I'm very new to Ruby, but have been playing around with validating IP addresses from a .yml file. Everything works but would like to make sure I'm doing things the proper Ruby way (I program in Perl non-OO and bash). The code:
require 'ipaddr'
require 'yaml'
yml = YAML.load_file("/data/dna.yml")
#
# Check hash for key...
#
if (yml.has_key?(board))
puts "Found Key: #{board}"
if (yml[board]["ports"])
yml[board]["ports"].each do |ip|
ipaddr = IPAddr.new "#{ip}"
if (ipaddr.ipv4?)
puts "Good: IPV4: #{ip}"
elsif (ipaddr.ipv6?)
puts "Good: IPV6: #{ip}"
else
puts "ERROR: Not an IPV4 or IPV6 format #{ip}..."
end
end
else
puts "Ports not found?"
end
else
puts "Key Not Found: #{board}"
end
My concern is in the each loop with IPAddr.new "#{ip}", is this the proper way to do this in Ruby? In Perl it might have been "ipv4($ip);" or equivalent without need to do the IPAddr.new on each run through the loop.
Just need to understand Ruby/Libraries and bast practices usage.
Thanks for any help in advance.
Tom