J
Jason
I'm in the process of creating an application launcher in ruby. I've hit
one little hang up though. I want to have the launcher access a file
containing a log of how many times each application has been run, for
ranking purposes.
So far I have the launcher creating the file if it doesn't exist, but
I'm not clear on how to write to that file or update existing lines in
it.
Example of file:
Firefox Web Browser=7
Archive Manager=3
If I create this file manually I'm able to pull the values out and
manipulate them using the following code:
#Open "rankings" file
rankingsFile = File.new("/home/jasbur/.duckduck/rankings")
#Check each line of "rankings" for existing record, if not assign 1 to
@rankingToWrite
rankingsFile.each_line {|line|
lineMatch = line.match("#{appName.chomp}=")
if lineMatch
existingRanking = lineMatch.post_match.to_i
newRanking = existingRanking + 1
puts "New Ranking for #{appName.chomp} is
#{newRanking}"
else
@rankingToWrite = 1
end
}
What I want to do is take "newRanking" and overwrite the old ranking
(existingRanking) in the file. I've seen some people recommend writing a
whole new file and destroying the old one. It seems like there should be
a more elegant way of doing this though. Any ideas?
one little hang up though. I want to have the launcher access a file
containing a log of how many times each application has been run, for
ranking purposes.
So far I have the launcher creating the file if it doesn't exist, but
I'm not clear on how to write to that file or update existing lines in
it.
Example of file:
Firefox Web Browser=7
Archive Manager=3
If I create this file manually I'm able to pull the values out and
manipulate them using the following code:
#Open "rankings" file
rankingsFile = File.new("/home/jasbur/.duckduck/rankings")
#Check each line of "rankings" for existing record, if not assign 1 to
@rankingToWrite
rankingsFile.each_line {|line|
lineMatch = line.match("#{appName.chomp}=")
if lineMatch
existingRanking = lineMatch.post_match.to_i
newRanking = existingRanking + 1
puts "New Ranking for #{appName.chomp} is
#{newRanking}"
else
@rankingToWrite = 1
end
}
What I want to do is take "newRanking" and overwrite the old ranking
(existingRanking) in the file. I've seen some people recommend writing a
whole new file and destroying the old one. It seems like there should be
a more elegant way of doing this though. Any ideas?