# Different results with File.exists?

**URL:** <https://rubytalk.org/t/different-results-with-file-exists/55295>\
**Category:** ruby-talk\
**Created:** [8 September 2009 11:30 UTC](https://rubytalk.org/t/different-results-with-file-exists/55295 "2009-09-08T11:30:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mehdi\_Karamnejad](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@Mehdi\_Karamnejad](https://rubytalk.org/u/Mehdi_Karamnejad)\
**Post date:** [8 September 2009 11:30 UTC](https://rubytalk.org/t/different-results-with-file-exists/55295/1 "2009-09-08T11:30:14Z")

</div>

when I use a string constant with File.exists? like the following ,it  
works fine:  
File.exists?("/home/sepehr")  
but when I get the string from user:  
path=gets  
if File.exists?(path) then

> **···**
>
> ----  
> ----  
> end  
> it doesn't work! what's wrong with the string which is typed by the  
> user?  
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [8 September 2009 11:46 UTC](https://rubytalk.org/t/different-results-with-file-exists/55295/2 "2009-09-08T11:46:39Z")

</div>

Hi --

> **···**
>
> On Tue, 8 Sep 2009, Mehdi Karamnejad wrote:
> 
> > when I use a string constant with File.exists? like the following ,it  
> > works fine:  
> > File.exists?("/home/sepehr")  
> > but when I get the string from user:  
> > path=gets  
> > if File.exists?(path) then  
> > ----  
> > end  
> > it doesn't work! what's wrong with the string which is typed by the  
> > user?
> 
> It ends with a newline. Try path.chomp.
> 
> David
> 
> --  
> David A. Black / Ruby Power and Light, LLC / [http://www.rubypal.com](http://www.rubypal.com)  
> Ruby/Rails training, mentoring, consulting, code-review  
> Latest book: The Well-Grounded Rubyist ([http://www.manning.com/black2\](http://www.manning.com/black2%5C))
> 
> September Ruby training in NJ has been POSTPONED. Details to follow.

---

<div class="post-metadata">

**Author:** ![Mehdi\_Karamnejad](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@Mehdi\_Karamnejad](https://rubytalk.org/u/Mehdi_Karamnejad)\
**Post date:** [8 September 2009 11:51 UTC](https://rubytalk.org/t/different-results-with-file-exists/55295/3 "2009-09-08T11:51:01Z")

</div>

yeah,that was it! thanks 😉

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [8 September 2009 12:59 UTC](https://rubytalk.org/t/different-results-with-file-exists/55295/4 "2009-09-08T12:59:32Z")

</div>

Additional hint: use p to debug output what you have. e.g.

path=gets  
p path  
if File.exists?(path) then

Cheers

robert

> **···**
>
> 2009/9/8 Mehdi Karamnejad \<sepehr.online@gmail.com\>:
> 
> > yeah,that was it! thanks 😉
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Marc\_Heiler](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@Marc\_Heiler](https://rubytalk.org/u/Marc_Heiler)\
**Post date:** [8 September 2009 14:30 UTC](https://rubytalk.org/t/different-results-with-file-exists/55295/5 "2009-09-08T14:30:29Z")

</div>

> Additional hint: use p to debug output

Or use pp!

With arrays and hashes pp beats p easily! :\>

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [9 September 2009 10:47 UTC](https://rubytalk.org/t/different-results-with-file-exists/55295/6 "2009-09-09T10:47:23Z")

</div>

Hi --

> > yeah,that was it! thanks 😉
> 
> Additional hint: use p to debug output what you have. e.g.
> 
> path=gets  
> p path  
> if File.exists?(path) then

Meta-additional hint: in 1.9, p returns its argument(s), so if you  
have:

&nbsp;&nbsp;&nbsp;path = gets

you can just change it to:

&nbsp;&nbsp;&nbsp;p path = gets

which makes for slightly easier back-and-forth between the p version  
and the non-p version.

David

> **···**
>
> On Tue, 8 Sep 2009, Robert Klemme wrote:
> 
> > 2009/9/8 Mehdi Karamnejad \<sepehr.online@gmail.com\>:
> 
> --  
> David A. Black / Ruby Power and Light, LLC / [http://www.rubypal.com](http://www.rubypal.com)  
> Ruby/Rails training, mentoring, consulting, code-review  
> Latest book: The Well-Grounded Rubyist ([http://www.manning.com/black2\](http://www.manning.com/black2%5C))
> 
> September Ruby training in NJ has been POSTPONED. Details to follow.

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [9 September 2009 12:15 UTC](https://rubytalk.org/t/different-results-with-file-exists/55295/7 "2009-09-09T12:15:42Z")

</div>

David, did you rather mean "path = p gets"? Because "p path = gets"  
does work in pre 1.9 already. 🙂

Kind regards

robert

> **···**
>
> 2009/9/9 David A. Black \<dblack@rubypal.com\>:
> 
> > Hi --
> > 
> > On Tue, 8 Sep 2009, Robert Klemme wrote:
> > 
> > > 2009/9/8 Mehdi Karamnejad \<sepehr.online@gmail.com\>:
> > > 
> > > > yeah,that was it! thanks 😉
> > > 
> > > Additional hint: use p to debug output what you have. e.g.
> > > 
> > > path=gets  
> > > p path  
> > > if File.exists?(path) then
> > 
> > Meta-additional hint: in 1.9, p returns its argument(s), so if you  
> > have:
> > 
> > path = gets
> > 
> > you can just change it to:
> > 
> > p path = gets
> > 
> > which makes for slightly easier back-and-forth between the p version  
> > and the non-p version.
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [9 September 2009 12:47 UTC](https://rubytalk.org/t/different-results-with-file-exists/55295/8 "2009-09-09T12:47:46Z")

</div>

Hi --

> > Hi --
> > 
> > > > yeah,that was it! thanks 😉
> > > 
> > > Additional hint: use p to debug output what you have. e.g.
> > > 
> > > path=gets  
> > > p path  
> > > if File.exists?(path) then
> > 
> > Meta-additional hint: in 1.9, p returns its argument(s), so if you  
> > have:
> > 
> > path = gets
> > 
> > you can just change it to:
> > 
> > p path = gets
> > 
> > which makes for slightly easier back-and-forth between the p version  
> > and the non-p version.
> 
> David, did you rather mean "path = p gets"? Because "p path = gets"  
> does work in pre 1.9 already. 🙂

I didn't really mean path = p gets -- I was thinking in general of the  
idiom of:

&nbsp;&nbsp;&nbsp;p x

instead of

&nbsp;&nbsp;&nbsp;p x  
&nbsp;&nbsp;&nbsp;x

but chose a bad example 🙂

David

> **···**
>
> On Wed, 9 Sep 2009, Robert Klemme wrote:
> 
> > 2009/9/9 David A. Black \<dblack@rubypal.com\>:
> > 
> > > On Tue, 8 Sep 2009, Robert Klemme wrote:
> > > 
> > > > 2009/9/8 Mehdi Karamnejad \<sepehr.online@gmail.com\>:
> 
> --  
> David A. Black / Ruby Power and Light, LLC / [http://www.rubypal.com](http://www.rubypal.com)  
> Ruby/Rails training, mentoring, consulting, code-review  
> Latest book: The Well-Grounded Rubyist ([http://www.manning.com/black2\](http://www.manning.com/black2%5C))
> 
> September Ruby training in NJ has been POSTPONED. Details to follow.
