我有一个关于 Ruby 范围的问题。我将粘贴同一脚本的两个版本,它们都可以工作,但一个将变量引入到类上方的类中,另一个在类实例和调用之前将它们引入下面。我更喜欢在课堂上与他们一起写作,因为如果有的话,我似乎更容易跟踪课堂上的内容。我不能这样做有什么原因吗?
脚本 A
require 'ipaddress'
puts "What class network?"
network_class = gets.chomp.to_s.downcase
puts "What is the ip?"
ip = gets.chomp.to_s.downcase
puts "How many subnets?"
subnets = gets.chomp.to_i
class TheSubNetter
def initialize(network_class, ip, subnets)
@network_class = network_class
@ip = ip
@subnets = subnets
end
def ip_class
if @network_class == "c"
IPAddress "#{@ip}/24"
elsif @network_class == "b"
IPAddress "#{@ip}/16"
elsif @network_class == "a"
IPAddress "#{@ip}/8"
else
"Error!"
end
end
def subnet(ip)
subnets = ip.split(@subnets)
end
def ranges(subnets)
bcast = subnets.map do |i|
i.broadcast
end
bcast.map do |i|
[i.first.to_string, i.last.to_string]
end
end
end
get_subd = TheSubNetter.new(network_class, ip, subnets)
address = get_subd.ip_class
subnets = get_subd.subnet(address)
ranges = get_subd.ranges(subnets)
ranges.each do |i|
puts i[0] + " => " + i[1]
end
脚本 B
require 'ipaddress'
class TheSubNetter
def initialize(network_class, ip, subnets)
@network_class = network_class
@ip = ip
@subnets = subnets
end
def ip_class
if @network_class == "c"
IPAddress "#{@ip}/24"
elsif @network_class == "b"
IPAddress "#{@ip}/16"
elsif @network_class == "a"
IPAddress "#{@ip}/8"
else
"Error!"
end
end
def subnet(ip)
subnets = ip.split(@subnets)
end
def ranges(subnets)
bcast = subnets.map do |i|
i.broadcast
end
bcast.map do |i|
[i.first.to_string, i.last.to_string]
end
end
end
puts "What class network?"
network_class = gets.chomp.to_s.downcase
puts "What is the ip?"
ip = gets.chomp.to_s.downcase
puts "How many subnets?"
subnets = gets.chomp.to_i
get_subd = TheSubNetter.new(network_class, ip, subnets)
address = get_subd.ip_class
subnets = get_subd.subnet(address)
ranges = get_subd.ranges(subnets)
ranges.each do |i|
puts i[0] + " => " + i[1]
end