# Checking what a method has returned

**URL:** <https://www.ruby-forum.com/t/checking-what-a-method-has-returned/86270>\
**Category:** Ruby\
**Created:** [February 21, 2007, 1:11am UTC](https://www.ruby-forum.com/t/checking-what-a-method-has-returned/86270 "2007-02-21T01:11:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [February 21, 2007, 1:11am UTC](https://www.ruby-forum.com/t/checking-what-a-method-has-returned/86270/1 "2007-02-21T01:11:20Z")

</div>

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](http://yahoo.com)’,‘[google.com](http://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

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [February 21, 2007, 1:21am UTC](https://www.ruby-forum.com/t/checking-what-a-method-has-returned/86270/2 "2007-02-21T01:21:29Z")

</div>

On 2/21/07, Chris G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [February 21, 2007, 2:51am UTC](https://www.ruby-forum.com/t/checking-what-a-method-has-returned/86270/3 "2007-02-21T02:51:45Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [February 21, 2007, 3:16am UTC](https://www.ruby-forum.com/t/checking-what-a-method-has-returned/86270/4 "2007-02-21T03:16:36Z")

</div>

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](http://yahoo.com)’,‘[google.com](http://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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
said:

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [February 21, 2007, 3:26am UTC](https://www.ruby-forum.com/t/checking-what-a-method-has-returned/86270/5 "2007-02-21T03:26:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [February 21, 2007, 3:28am UTC](https://www.ruby-forum.com/t/checking-what-a-method-has-returned/86270/6 "2007-02-21T03:28:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [February 21, 2007, 3:20am UTC](https://www.ruby-forum.com/t/checking-what-a-method-has-returned/86270/7 "2007-02-21T03:20:36Z")

</div>

Hi –

On Wed, 21 Feb 2007, removed\_email\_address@domain.invalid 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

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [February 21, 2007, 3:24pm UTC](https://www.ruby-forum.com/t/checking-what-a-method-has-returned/86270/8 "2007-02-21T15:24:43Z")

</div>

Hi –

On Wed, 21 Feb 2007, removed\_email\_address@domain.invalid 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
