# Different results with File.exists?

**URL:** <https://www.ruby-forum.com/t/different-results-with-file-exists/175124>\
**Category:** Ruby\
**Created:** [September 8, 2009, 1:30pm UTC](https://www.ruby-forum.com/t/different-results-with-file-exists/175124 "2009-09-08T13:30:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sepehr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sepehr](https://www.ruby-forum.com/u/sepehr)\
**Post date:** [September 8, 2009, 1:30pm UTC](https://www.ruby-forum.com/t/different-results-with-file-exists/175124/1 "2009-09-08T13:30:05Z")

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

---

<div class="post-metadata">

**Author:** ![sepehr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sepehr](https://www.ruby-forum.com/u/sepehr)\
**Post date:** [September 8, 2009, 1:50pm UTC](https://www.ruby-forum.com/t/different-results-with-file-exists/175124/2 "2009-09-08T13:50:59Z")

</div>

yeah,that was it! thanks 😉

---

<div class="post-metadata">

**Author:** ![sepehr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sepehr](https://www.ruby-forum.com/u/sepehr)\
**Post date:** [September 8, 2009, 1:47pm UTC](https://www.ruby-forum.com/t/different-results-with-file-exists/175124/3 "2009-09-08T13:47:02Z")

</div>

Hi –

On Tue, 8 Sep 2009, Mehdi K. wrote:

> 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 ([The Well-Grounded Rubyist](http://www.manning.com/black2))

September Ruby training in NJ has been POSTPONED. Details to follow.

---

<div class="post-metadata">

**Author:** ![sepehr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sepehr](https://www.ruby-forum.com/u/sepehr)\
**Post date:** [September 8, 2009, 3:00pm UTC](https://www.ruby-forum.com/t/different-results-with-file-exists/175124/4 "2009-09-08T15:00:47Z")

</div>

2009/9/8 Mehdi K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

Cheers

robert

---

<div class="post-metadata">

**Author:** ![sepehr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sepehr](https://www.ruby-forum.com/u/sepehr)\
**Post date:** [September 9, 2009, 12:48pm UTC](https://www.ruby-forum.com/t/different-results-with-file-exists/175124/5 "2009-09-09T12:48:02Z")

</div>

Hi –

On Tue, 8 Sep 2009, Robert K. wrote:

> 2009/9/8 Mehdi K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > 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

–  
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 ([The Well-Grounded Rubyist](http://www.manning.com/black2))

September Ruby training in NJ has been POSTPONED. Details to follow.

---

<div class="post-metadata">

**Author:** ![sepehr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sepehr](https://www.ruby-forum.com/u/sepehr)\
**Post date:** [September 9, 2009, 2:16pm UTC](https://www.ruby-forum.com/t/different-results-with-file-exists/175124/6 "2009-09-09T14:16:27Z")

</div>

2009/9/9 David A. Black [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > path=gets  
> > 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. 🙂

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![sepehr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sepehr](https://www.ruby-forum.com/u/sepehr)\
**Post date:** [September 8, 2009, 4:30pm UTC](https://www.ruby-forum.com/t/different-results-with-file-exists/175124/7 "2009-09-08T16:30:27Z")

</div>

> Additional hint: use p to debug output

Or use pp!

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

---

<div class="post-metadata">

**Author:** ![sepehr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sepehr](https://www.ruby-forum.com/u/sepehr)\
**Post date:** [September 9, 2009, 3:23pm UTC](https://www.ruby-forum.com/t/different-results-with-file-exists/175124/8 "2009-09-09T15:23:46Z")

</div>

Hi –

On Wed, 9 Sep 2009, Robert K. wrote:

> > > 
> > 
> > Â 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:

p x

instead of

p x  
x

but chose a bad example 🙂

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 ([The Well-Grounded Rubyist](http://www.manning.com/black2))

September Ruby training in NJ has been POSTPONED. Details to follow.
