Different results with File.exists?

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

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
Ruby/Rails training, mentoring, consulting, code-review
Latest book: The Well-Grounded Rubyist (http://www.manning.com/black2\)

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

yeah,that was it! thanks :wink:

···

--
Posted via http://www.ruby-forum.com/.

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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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/\.

Hi --

yeah,that was it! thanks :wink:

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

···

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
Ruby/Rails training, mentoring, consulting, code-review
Latest book: The Well-Grounded Rubyist (http://www.manning.com/black2\)

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

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

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

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/

Hi --

Hi --

yeah,that was it! thanks :wink:

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. :slight_smile:

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

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
Ruby/Rails training, mentoring, consulting, code-review
Latest book: The Well-Grounded Rubyist (http://www.manning.com/black2\)

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