문제

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
도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top