Checking what a method has returned

Discussion in 'Ruby' started by Chris Gallagher, Feb 21, 2007.

  1. 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

    --
    Posted via http://www.ruby-forum.com/.
    Chris Gallagher, Feb 21, 2007
    #1
    1. Advertising

  2. 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?

    --
    Posted via http://www.ruby-forum.com/.
    Chris Gallagher, Feb 21, 2007
    #2
    1. Advertising

  3. Chris Gallagher

    Guest

    Hi --

    On Wed, 21 Feb 2007, wrote:

    > 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)
    , Feb 21, 2007
    #3
  4. 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

    --
    Posted via http://www.ruby-forum.com/.
    Chris Gallagher, Feb 21, 2007
    #4
  5. Chris Gallagher

    Guest

    On Wed, 21 Feb 2007, Chris Gallagher wrote:

    > 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
    --
    we can deny everything, except that we have the possibility of being better.
    simply reflect on that.
    - the dalai lama
    , Feb 21, 2007
    #5
  6. Chris Gallagher

    Guest

    Hi --

    On Wed, 21 Feb 2007, wrote:

    > 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)
    , Feb 21, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    324
    Chris Uppal
    Mar 10, 2006
  2. Mars
    Replies:
    2
    Views:
    409
    Bengt Richter
    Jul 19, 2003
  3. Brad Tilley
    Replies:
    10
    Views:
    407
    Dennis Lee Bieber
    Sep 16, 2004
  4. C++Liliput
    Replies:
    5
    Views:
    891
    Pete Becker
    Dec 12, 2007
  5. Replies:
    0
    Views:
    88
Loading...

Share This Page