Better idiom for title on an optional list?

I'm still learning Ruby, and most of the time I can find a beautiful
Ruby idiom that collapses two or more statements into one easy-to-read
(or learn) idiom, like h[key] ||= 0. My current project has a lot of
places where it emits lists if they contain any items, much like this:

unless items.empty?
  puts "items\n-----"
  items.each { |item| puts item }
end

My header code and list code is more complicated, and sometimes there
are lists within lists, and it all just starts to look ugly, even
split out into smaller methods. It would seem more Ruby-like if I
could fold the header into the iterator somehow -- rather than what I
have now that feels like it tests the Array for items twice. Or maybe
I'm trying too hard to make it terse?

puts "items",
      "-----",
      items unless items.empty?

I don't guess that's much better.

It does feel like it should be a separate method to me.

James Edward Gray II

···

On Nov 29, 2007, at 11:25 AM, slothbear wrote:

I'm still learning Ruby, and most of the time I can find a beautiful
Ruby idiom that collapses two or more statements into one easy-to-read
(or learn) idiom, like h[key] ||= 0. My current project has a lot of
places where it emits lists if they contain any items, much like this:

unless items.empty?
  puts "items\n-----"
  items.each { |item| puts item }
end

1)

   puts "list", "----", list unless list.empty?

2) refactor you code so that this is ok

   require 'yaml'

   y 'list' => list

   this will print and empty list. personally i think this is best since analysis of output is very hard when the schema keeps changing

3) same as above, but with test

   y 'list' => list unless list.empty?

4) dry it up

   def report label, list
     puts label, '---', list unless list.empty?
   end

   report 'foo', foo

5) create a list class and use it

   class List < ::Array
     def initialize label
       @label = label
     end

     def to_s
       ...
   end

   unfortunately this gets icky because ruby handles any subclass of array specically when given as an arg to puts...

regards.
a @ http://codeforpeople.com/

···

On Nov 29, 2007, at 10:25 AM, slothbear wrote:

I'm still learning Ruby, and most of the time I can find a beautiful
Ruby idiom that collapses two or more statements into one easy-to-read
(or learn) idiom, like h[key] ||= 0. My current project has a lot of
places where it emits lists if they contain any items, much like this:

unless items.empty?
  puts "items\n-----"
  items.each { |item| puts item }
end

My header code and list code is more complicated, and sometimes there
are lists within lists, and it all just starts to look ugly, even
split out into smaller methods. It would seem more Ruby-like if I
could fold the header into the iterator somehow -- rather than what I
have now that feels like it tests the Array for items twice. Or maybe
I'm trying too hard to make it terse?

--
we can deny everything, except that we have the possibility of being better. simply reflect on that.
h.h. the 14th dalai lama

Thanks for the ideas. I ended up creating a separate method that
accepts a label and list as arguments. It looks pretty good and feels
pretty natural. Success! my thanks again.

sb