I'm trying to build a ruby program which creates a directory and name it using console input.
1 #!/usr/bin/env ruby¬
2 ¬
3 puts 'HW date, format MM-DD:'¬
4 ¬
5 directory_name = 'hw_' + gets¬
6 ¬
7 begin¬
8 ▸ if File::directory?(directory_name)¬
9 ▸ ▸ return¬
10 ▸ end¬
11 ▸ Dir.mkdir(directory_name)¬
12 end¬
However the directies created alway have garbage characters on the end of them?
$ ./start_hw.rb
HW date, format MM-DD:
01-13
$ ls
hw_01-13? start_hw.rb
How do I make it stop placing the ? (i.e. Non-printable characters)
The ruby gets
function returns the line ending characters at the end so you should use chomp
to remove them.
directory_name = 'hw_' + gets.chomp
puts directory_name.inspect # print it to make sure there is no junk