1

I am writing a program to read a directory of nested directories and files and get back a nested array that matches the file/directory structure.

I have the following:

dirs = File.join(<file_path>, "**", "*")
dirs_array = Dir.glob(dirs)

When I prints dirs_array, I get back a list of all the file paths but it's not in array format. However, when I call 'dirs_array.class' the output is Array. What am I missing here? And how do I get the dirs_array to print in array format?

Stefan
  • 109,145
  • 14
  • 143
  • 218
beans3598
  • 11
  • 2

1 Answers1

1

You're likely getting an Array back, but you're not seeing it because puts tries to work with that and instead prints one entry per line. This can obscure the internals.

To show the structure:

puts dirs_array.inspect

There's also a shorthand form for this:

p dirs_array

Or if you like things fancy you can use the "pretty printer":

pp dirs_array

This form is more verbose, but can help reveal more complex structures.

tadman
  • 208,517
  • 23
  • 234
  • 262