jet8832
jet8832

Reputation: 13

Use variable with Dir.glob

I am trying to make a small change to a Ruby script so i can specify a folder location at runtime.

I was pretty sure that this would be an easy task, even though i am not a Ruby programmer, but i cannot find the correct syntax.

puts "Enter folder name and press enter: "
folder = gets

files = Dir.glob("folder/[0-100]*.txt"); # What is the correct syntax to use, so the content of the variable folder will be used?

puts files

Upvotes: 1

Views: 1611

Answers (1)

sepp2k
sepp2k

Reputation: 370367

To insert a variable (or any ruby expression) into a string, you can use #{}:

Dir.glob("#{folder}/[0-100]*.txt")

Also note that the string returned by gets will have a newline (\n) at the end, which isn't valid in folder names of course. So you'll have to use the chomp method to get rid of that.

Upvotes: 2

Related Questions