880cbab435f00197613c9cc2065b4f5a

This code is functional, but ugly. Only after publishing it did I look at it critically. Show me your way.

1
2
3
4
5
6
7
8
def sum_string_sizes(units)
  units.inject(0) {|memo, unit| memo + unit.size }
end

units = %w{ one two three four five six seven eight nine }

sum_string_sizes(units)
=> 36

Refactorings

No refactoring yet !

Bfec5f7d1a4aaafc5a2451be8c42d26a

macournoyer

September 19, 2007, September 19, 2007 19:52, permalink

No rating. Login to rate!

You don't have to count the sizes separately, just join them up in one big string and you're almost done!

1
2
3
units = %w{ one two three four five six seven eight nine }

units.join.size # => 36
A77873df3a9766b208e009248a2a9a56

Hampton

September 21, 2007, September 21, 2007 09:11, permalink

1 rating. Login to rate!

Just another way to do it. Very similar.

1
2
3
4
5
6
7
8
9
10
units = %w{ one two three four five six seven eight nine }


units.sum { |i| i.size } #=> 36

# A shorter equivalent if ActiveSupport is around
units.sum &:size         #=> 36

# Sum acts like join on an array of strings and is one character shorter if we are counting. ;)
units.sum.size
A77873df3a9766b208e009248a2a9a56

Hampton

September 21, 2007, September 21, 2007 09:20, permalink

1 rating. Login to rate!

I also want to write the most complex solution to this one. That is, the most complex solution that has no parts that aren't moving towards the solution.

For instance, to_s.to_i.to_s is not out of the rules.

DefactorMyCode.com

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
units = %w{ one two three four five six seven eight nine }

(units.collect do |item|
  item.size
end).sum


# Or, even more
counts = (units.collect do |item|
  counter = 0
  item.to_a.each do |character|
    counter = counter + 1
  end
  counter
end)
total_size = 0
counts.each do |size|
  total_size = total_size + size
end
total_size
Bfec5f7d1a4aaafc5a2451be8c42d26a

macournoyer

September 21, 2007, September 21, 2007 10:13, permalink

1 rating. Login to rate!

Welcome to the site Hampton! Nice defactoring!

But... counting in memory ?? This is so old school! Using file is much much faster, better, stronger!!

1
2
3
4
5
6
7
8
9
units = %w{ one two three four five six seven eight nine }

File.open('counter.txt', 'w') do |f|
  units.each do |unit|
    f << unit
  end
end

puts File.size('counter.txt')
22100344f91ad34ad9fb8fb6b6940a84

Vamsee

October 4, 2007, October 04, 2007 07:04, permalink

No rating. Login to rate!
1
2
3
units = %w{ one two three four five six seven eight nine }

p units.join.length
22100344f91ad34ad9fb8fb6b6940a84

Vamsee

October 4, 2007, October 04, 2007 07:05, permalink

No rating. Login to rate!

Ahhh111, Sorry for dupe macournoyer alredy posted.

Bfec5f7d1a4aaafc5a2451be8c42d26a

macournoyer

October 4, 2007, October 04, 2007 09:52, permalink

No rating. Login to rate!

plus you used length +2 chars, ouch! :p

Your refactoring





Format Copy from initial code

or Cancel