John Maclean
1/30/2006 5:31:00 AM
Hey chaps,
# A script to ask a user for a string and use it to grep through a
file. My first version works.
# It was suggested to turn the constants into objects whilst chatting on
ruby-lang. Please compare the two methods
(excuse the pun). What's wrong with using constants and what am I doing
wrong in my OO version?
#first version using constants
DataDir= "/home/jayeola/bin/acid/data/lookups/"
DataFile = "lookup_assetype.txt"
Lookup = File.join(DataDir, DataFile)
def usage
screen = "type some letters in the word"
end
def grabber
# grab first couple letters and grep thru file
xx = gets.chomp!
File.open("#{Lookup}") do |test|
test.each do |line|
puts line if line =~ /#{xx}/
end
end
end
puts usage
grabber
# This doesn't work. The "object" way...
def usage
screen = "type a few letters containing the word"
end
i = Greppa.new("{@datadir}", "{@datafile}")
i.inspect
puts usage
puts lookup
# filelocations
class Greppa
def initialize(datadir, datafile )
@datadir= "/home/jayeola/bin/acid/data/lookups/"
@datafile = "lookup_assetype.txt"
@lookup = File.join(@datadir, @datafile)
end
end
def grabber
lookup = File.join("{@datadir}", "{@datafile}")
# grab first couple letters and grep thru file
xx = gets.chomp!
File.open("#{lookup}") do |test|
test.each do |line|
puts line if line =~ /#{xx}/
end
end
end
#
i = Greppa.new("{@datadir}", "{@datafile}")
i.inspect
puts usage
puts lookup
--
John Maclean
MSc (DIC)
07739 171 531