shanahobo86
shanahobo86

Reputation: 497

Open a file with user input Ruby

I take a string variable from a user like this:

mail = gets

and I want to use this variable to open a file.

file = File.new(mail, "r") ##obviously this isn't working

How do I actually use this mail variable to open a file of that name?

Thanks

Upvotes: 0

Views: 999

Answers (2)

Josh
Josh

Reputation: 5721

I prefer mail = gets.strip.

strip seems to be slightly slower than chomp but I find it to be a little bit more readable.

If you're curious about the benchmark, check out the gist here.

Upvotes: 0

SwiftMango
SwiftMango

Reputation: 15284

mail = gets.chomp

gets function gives a string with \n in the end.

Upvotes: 2

Related Questions