D41d8cd98f00b204e9800998ecf8427e

A simple method to create a bunch of test files of different sizes. create_file creates a file of a given size and returns its name. I'd like to get rid of the temporary 'files' variable

def create_files
  files = []
  sizes = ['10K', '100K', '500K', '1M', '2M', '5M', '10M', '20M']
  
  sizes.each do |size|
    100.times do
      files << create_file(size)
    end
  end
  
  files
end

Refactorings

No refactoring yet !

Ee0505bbd355292778077fb662c88f13

Fu86

June 23, 2010, June 23, 2010 23:57, permalink

2 ratings. Login to rate!

You can use Array#flatten to merge the nested arrays into a big one.

def create_files
  %w{ 10K 100K 500K 1M 2M 5M 10M 20M }.map do |size|
    100.times.map {|index| create_file(size)}
  end.flatten
end
A3897deec9b1dbc6def9f5dfcb58bb60

spaghetticode

June 25, 2010, June 25, 2010 18:01, permalink

1 rating. Login to rate!
def create_files
  %w{ 10K 100K 500K 1M 2M 5M 10M 20M }.inject([]) do |files, size|
    100.times { files << create_file(size) }
    files
  end
end
A8d3f35baafdaea851914b17dae9e1fc

Adam

June 26, 2010, June 26, 2010 06:56, permalink

5 ratings. Login to rate!
def create_files
  (%w{ 10K 100K 500K 1M 2M 5M 10M 20M } * 100).map(&method(:create_file))
end
30c3b8bad41c9546a9e1ace522d2bec0

Tony Schneider

July 20, 2010, July 20, 2010 12:44, permalink

No rating. Login to rate!

Sorry if this solution has already been submitted, trying not to look above. I think this is pretty readable.

FILE_SIZES = ['10K', '100K', '500K', '1M', '2M', '5M', '10M', '20M']

def create_files

  FILE_SIZES.inject([]) do |files, size|
    10.times do
      files << create_file(size)
    end
    files
  end

end

Your refactoring





Format Copy from initial code

or Cancel