# Better idiom for title on an optional list?

**URL:** <https://www.ruby-forum.com/t/better-idiom-for-title-on-an-optional-list/121661>\
**Category:** Ruby\
**Created:** [November 29, 2007, 6:26pm UTC](https://www.ruby-forum.com/t/better-idiom-for-title-on-an-optional-list/121661 "2007-11-29T18:26:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![slothbear](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba8739/32.png) [@slothbear](https://www.ruby-forum.com/u/slothbear)\
**Post date:** [November 29, 2007, 6:26pm UTC](https://www.ruby-forum.com/t/better-idiom-for-title-on-an-optional-list/121661/1 "2007-11-29T18:26:12Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![slothbear](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba8739/32.png) [@slothbear](https://www.ruby-forum.com/u/slothbear)\
**Post date:** [November 29, 2007, 6:34pm UTC](https://www.ruby-forum.com/t/better-idiom-for-title-on-an-optional-list/121661/2 "2007-11-29T18:34:23Z")

</div>

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

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 G. II

---

<div class="post-metadata">

**Author:** ![slothbear](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba8739/32.png) [@slothbear](https://www.ruby-forum.com/u/slothbear)\
**Post date:** [November 29, 2007, 6:45pm UTC](https://www.ruby-forum.com/t/better-idiom-for-title-on-an-optional-list/121661/3 "2007-11-29T18:45:34Z")

</div>

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

> 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?

1. 

puts “list”, “----”, list unless list.empty?

1. refactor you code so that this is ok

2. same as above, but with test

3. dry it up

4. create a list class and use it

regards.  
a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![slothbear](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba8739/32.png) [@slothbear](https://www.ruby-forum.com/u/slothbear)\
**Post date:** [December 4, 2007, 10:31pm UTC](https://www.ruby-forum.com/t/better-idiom-for-title-on-an-optional-list/121661/4 "2007-12-04T22:31:05Z")

</div>

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
