Adding double quotes around a string adds the closing quote on a new line

Viewed 74

I am just starting with Ruby and working on adding double quotes around a string.

print "Enter the name of the file to use (including the file type)"
file_name = gets.to_s
puts "\"#{file_name}\""

and I get an output of

"test1.txt
"

Any ideas on what could be going wrong are greatly appreciated. Thank you!

1 Answers

This doesn't have to do with the quotations you're adding, but rather that you're not using String#chomp to remove the trailing newline that is included with every gets:

gets # I type foo
# => "foo\n"

gets.chomp # I type foo
# => "foo"

You don't actually need the to_s here because gets will always return a string.

Related