Checking what a method has returned

C

Chris Gallagher

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)
downloadImage(@domains)
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
 
C

Chris Gallagher

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?
 
D

dblack

Hi --

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

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

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
C

Chris Gallagher

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
 
A

ara.t.howard

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)
downloadImage(@domains)
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..


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
 
D

dblack

Hi --

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

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

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top