Format the display of an array variable

In the book, “Agile Web Development With Rails” there is a simple idea for an example program using the command:

@dirs = Dir.glob(‘*’)

Then they suggest writing the code to display the directory contents but don’t say how. So I wrote a file called app/views/say/dirlist.rb which has the following code:

Directory Listing

Directory:

<%= @dirs %>

The problem is that while all the file and directory names are displayed, they are also all “run together”. What would be the easiest and most logical way to format the output so that the filenames are separated by ", "?

well, you could do this (but I hate the trailing ‘,’ that results )

<%= @dirs.collect { |dir| “#{dir},” } %>

PGP.sig (186 Bytes)

Thank you, Peter. I was wondering if maybe you can think of another approach that involves some sort of "iteration process" so that the final array element can be treated differently. I was reading the manual regarding arrays and there is an example that goes like this:

Given an array called a,

a.each { |name| puts "Got #{name}" }

Any further insights?

Thank you Curtis...very, very much!!! I have saved both ways of doing this (yours and Peter's) in my example file with comments in the HTML section. I'm sure there are dozens of other ways to do the same thing...but I like your first suggestion best of all. It shows me that you have significant Ruby experience and practical experience in helping other Ruby students. Many thanks!