better idiom for title on an optional list?

S

slothbear

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

James Edward Gray II

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 Gray II
 
A

ara.t.howard

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?

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/
 
S

slothbear

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top