Hourglass Contracts & Milestone Invoicing

Hourglass is a web-based invoicing application that makes sure you get paid, on time and every time.

Collect deposits, deliver milestone invoices and start every project with one of our contract templates

5 Things Highly-Paid Rails Consultants Do Differently

With this valuable course, you'll learn the secrets of how to:

  • Regain valuable hours back by hacking your workflow
  • Build a one-man consultancy and work wherever you want
  • Attract high-paying clients you'll love working with, every time
  • Build passive income by productizing what you're already selling!
P.S.: We'll send you actionable advice. No junk. Unsubscribe anytime.

Free Guide!

Five Things Highly-Paid Freelancers Do Differently

Using Ruby: How To Check If A File Exists

Ruby is such a fun language to program with because of its inherent power and beautiful syntax. Even if you're totally new to programming, you may be able to understand some of the simpler methods in the Ruby programming language.

However, this can sometimes cause confusion. Let's pretend that you're writing a Ruby program of your own and you want to check whether or not a file exists. If you look through this Ruby documentation, you may see something like this.

    #Check if file exits
    File.exist?('image.jpg')
    => true

Reading this code in English, it seems pretty straightforward: we're checking to see if a file called 'filename' exists. If you pull up a session of irb and test this out, things should work out fine. irb should return true.

However, what happens if you make a mistake and you accidentally enter in the name of a directory rather than a filename?

    # Will also return true from directories - watch out!
    File.exist?('~/images')
    => true

Ruby will still return this as true.

But wait, didn't we only want to check to see if file exists? The method exist? will return true if it finds a directory or a file.

Think about as more of a "does this exist in the file tree" interrogation, not specifically whether or not the path in question is strictly a file.

If you're only looking to see if a file exists, and wish to return false for directories, you'll want to use the method file?

    File.file?('~/images')      # file? will only return true for files
    => false
    File.file?('image.jpg')
    => true

As you can see, the method file? is slightly more picky than exist?. Depending on the specificity of your project, whichever method you use could make a big difference. Because of Ruby's easy-to-read syntax, things can get somewhat confusing when dealing with phrases that appear to be interchangeable.

I hope that this helps you avoid any false positives that could've thrown a monkey wrench into your plans!

5 Things Highly-Paid Rails Consultants Do Differently

With this valuable course, you'll learn the secrets of how to:

  • Regain valuable hours back by hacking your workflow
  • Build a one-man consultancy and work wherever you want
  • Attract high-paying clients you'll love working with, every time
  • Build passive income by productizing what you're already selling!
P.S.: We'll send you actionable advice. No junk. Unsubscribe anytime.

Free Guide!

Five Things Highly-Paid Freelancers Do Differently