Hi,
Im looking for a way of checking whether a method has returned true or
false.
for example i have an want to start off by checking if an image is
already stored locally by calling " def checkCache". If the image is
already there it will simply come back the process is complete. If it
isnt there already it will need to call another method which will be
called “downloadImage”.
What I have for this at the moment is far from complete so feel free to
change it around:
def index
@domains = [‘yahoo.com’,‘google.com’]
domains_length = @domains.length
domains_length.times { |i|
######## if checkCache finds that there is no image present then it
needs to #########proceed to the next method which would be
downloadImage.
checkCache(@domains[i])
downloadImage(@domains[i])
end
}
end
def checkCache
…
…
end
def downloadImage
…
…
end
Please help me out on this one, I’m a little bit lost. If the question
seems a bit vague or crazy please say so…
Cheers,
Chris
On 2/21/07, Chris G. [email protected] wrote:
called “downloadImage”.
domains_length.times { |i|
######## if checkCache finds that there is no image present then it
needs to #########proceed to the next method which would be
downloadImage.
checkCache(@domains[i])
downloadImage(@domains[i])
end
}
end
If checkCache returns true of false then use inline if / unless.
downloadImage(@domains[i] unless checkCache(@domains[i])
All on one line… Noice
well the reason im doing this is for refoactoring purposes so my code is
chaning a lot.
What I have at the moment is:
file = 'public/images/' + source_url + '.jpg'
if ((FileTest.exists?(file))&&((Time.new-File.stat(file).mtime)<
50000))
…
Im guessing I can stick a ‘return true’ just below that or does it work
that way?
I do not know if this will help you out, I think this is what you
wanted. This only calls the downloadImg method if the file exists.
@domains = [‘yahoo.com’,‘google.com’]
def checkCache(filename)
if File.exists?(filename)
return true
else
return false
end
end
def downloadImg(name)
puts “called the download function”
end
@domains.each do |domain|
if checkCache(domain)
downloadImg(domain)
end
end
On 2007-02-20 17:51:48 -0800, Chris G. [email protected]
said:
thanks for the reply. The thing is that im not just checking the
existance of the file. the condition is that the file must exist and
also be less than x ammount of time old as seen in the if statement
above.
file = ‘public/images/’ + source_url + ‘.jpg’
if ((FileTest.exists?(file))&&((Time.new-File.stat(file).mtime)<
50000))
cheers,
chris
On Wed, 21 Feb 2007, Chris G. wrote:
What I have for this at the moment is far from complete so feel free to
needs to #########proceed to the next method which would be
…
Please help me out on this one, I’m a little bit lost. If the question
seems a bit vague or crazy please say so…
i’d use something like
def unless_cached pathname
yield unless File.exist?(pathname)
end
def download
end
domains.each do |domain|
unless_cached(domain) do
download
end
end
regards.
-a
Hi –
On Wed, 21 Feb 2007, [email protected] wrote:
return false
end
end
Save your fingers some work 
def check_cache(filename)
File.exists?(filename)
end
File.exists? already returns true or false, so you don’t have to wrap
it. In fact, if all you want to do is check file existence, you’re
probably best off just using File.exists? directly.
David
Hi –
On Wed, 21 Feb 2007, [email protected] wrote:
unless_cached(domain) do
download
end
end
I’m curious what that buys you over and above:
unless File.exist?(domain)
or even:
def cached(pathname)
File.exist?(pathname)
end
…
unless cached(domain)
…
Is there a downside to just using unless raw (so to speak)?
David