Format the display of an array variable

In the book, “Agile Web D. 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 ", "?


Get your own web address for just $1.99/1st yr. We’ll help. Yahoo! Small
Business.

   <%= @dirs %>

What would be the easiest and most logical way to format
the output so that the filenames are separated by ", "?

<%= @dirs.join ‘,’ %>

or one per line:

<%= @dirs.join
’ %>

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

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


Peter Wright
[email protected]

Personal Blog -> http://peterwright.blogspot.com
Agile Development Blog -> http://exceeding-expectations.blogspot.com

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!

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?