Dir.mkdir appending question mark after directory name

Hi all

I´m starting to laern Ruby, but i´ve encountered this strange problem.
This is my code:

pname = gets
pname.chomp
Dir.mkdir(pname)

If I enter test for pname, the resulting folder is named “test?”. What
am I doing wrong here?

Thanks


ruby --version gives this:
ruby 1.8.7 (2011-06-30 patchlevel 352) [x86_64-linux]

I´m running Ubuntu 12.04 on a MacBook

On Wed, Aug 29, 2012 at 10:51 AM, Joakim L. [email protected]
wrote:

Hi all

Im starting to laern Ruby, but ive encountered this strange problem.
This is my code:

pname = gets
pname.chomp

You are missing an exclamation mark:
pname.chomp!

The method without returns a new object but does not modify your
pname. You can see that by inserting

p pname

before creating the directory.

Dir.mkdir(pname)

If I enter test for pname, the resulting folder is named “test?”. What
am I doing wrong here?

See above.

Kind regards

robert

Thanks, Robert!

That helped :slight_smile:

I suppose pname.chomp! would mean the same as pname = pname.chomp then.
Didn´t think of that…

Robert K. wrote in post #1073741:

On Wed, Aug 29, 2012 at 10:51 AM, Joakim L. [email protected]
wrote:

Hi all

Im starting to laern Ruby, but ive encountered this strange problem.
This is my code:

pname = gets
pname.chomp

You are missing an exclamation mark:
pname.chomp!

The method without returns a new object but does not modify your
pname. You can see that by inserting

p pname

before creating the directory.

Dir.mkdir(pname)

If I enter test for pname, the resulting folder is named “test?”. What
am I doing wrong here?

See above.

Kind regards

robert

On Wed, Aug 29, 2012 at 11:33 AM, Joakim L. [email protected]
wrote:

Thanks, Robert!

YWC.

That helped :slight_smile:

Good!

I suppose pname.chomp! would mean the same as pname = pname.chomp then.

It has the same effects in your specific example. But effects will be
different if there is another reference the object referred to by
pname. In that case #chomp! and #chomp will behave differently
because with exclamation mark you’ll see the same object through both
references but without and assignment you won’t.

Kind regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs