Hi,
I am creating a ruby script to split out nagios configuration files. I
am trying to do it the OOP way by using classes and objects etc. Here is
the code.
#!/usr/bin/ruby
class Host
attr_accessor :name, :ipaddress
def initialize(hostfile)
@hostfile = hostfile
puts name
puts ipaddress
end
def spltter(hostfile, inme)
myfile = File.open(hostfile)
myfile.each_line do |line|
if line =~ /#{inme}/
a = line.split(/\s+/)
return a[2]
end
end
end
def name
@name = spltter(@hostfile, “host_name*.*”)
end
def ipaddress
@ipaddress = spltter(@hostfile, “address*.*”)
end
end
Get all files in directory
Dir.foreach("./configs") do |files|
#filter out anything that isn’t a config file
if files =~ /.cfg/
hostfile = “./configs/” + files
Host.new(hostfile)
end
end
at the moment it outputs the correct information but that is coming from
the puts in the initialisation method in the Host class. What I would
like to do may not be possible, but here goes.
just after the Host.new method
I would like to use
currenthost = Host.name
currenthostip = Host.ipaddress
Currently when I do this it returns no method defined
I am reasonably new to Ruby and OOP programming and am working through
several books on Ruby right now.
Any help would be much appreciated.
Thank you
Rob