2007-02-27 17:21:26 Ruby Indentations and Blocks
John started this with this piece of code:
( ruby )
  1  [
2 Group.new( :kind => 'committee' ),
3 Group.new( :kind => 'topic' ),
4 Group.new( :kind => 'adhoc' )
5 ].each do |g|
6 assert( g.save! )
7 end


And I'll just mention this other possibility:
( ruby )
  1  [ 'committee', 'topic', 'adhoc' ].map {
2 |kind| Group.new( :kind => kind )
3 }.each do |g|
4 assert( g.save! )
5 end


Which only really works out as well as it does because of the uniformity of the initial array.
  • Jeff (Tue, February 27th, 2007, 5:43pm UTC)
    Although, for this specific example, couldn't you just do this?
    ( ruby )
      1  [ 'committee', 'topic', 'adhoc' ].each do |kind|
    2 assert( Group.new( :kind => kind ).save! )
    3 end

  • john (Tue, February 27th, 2007, 5:52pm UTC)
    sure.

Leave a comment