Skip to content

Instantly share code, notes, and snippets.

@squaresurf
Forked from danott/test_serial_comma.rb
Last active January 4, 2016 09:19
Show Gist options
  • Save squaresurf/8600811 to your computer and use it in GitHub Desktop.
Save squaresurf/8600811 to your computer and use it in GitHub Desktop.
require "minitest/autorun"
def serial_comma(segments)
case segments.count
when 1
return segments.first
when 2
return segments.join " and "
else
return segments.push("and #{segments.pop}").join ", "
end
end
class TestSerialComma < MiniTest::Unit::TestCase
def possessives
%w[hers his yours mine ours]
end
def test_possessives_1
assert_equal "hers", serial_comma(possessives.slice(0, 1))
end
def test_possessives_2
assert_equal "hers and his", serial_comma(possessives.slice(0, 2))
end
def test_possessives_3
assert_equal "hers, his, and yours", serial_comma(possessives.slice(0, 3))
end
def test_possessives_4
assert_equal "hers, his, yours, and mine", serial_comma(possessives.slice(0, 4))
end
def test_possessives_5
assert_equal "hers, his, yours, mine, and ours", serial_comma(possessives)
end
def places
["Seattle", "Lexington", "Athens", "St. Petersberg"]
end
def test_places_1
assert_equal "Seattle", serial_comma(places.slice(0, 1))
end
def test_places_2
assert_equal "Seattle and Lexington", serial_comma(places.slice(0, 2))
end
def test_places_3
assert_equal "Seattle, Lexington, and Athens", serial_comma(places.slice(0, 3))
end
def test_places_4
assert_equal "Seattle, Lexington, Athens, and St. Petersberg", serial_comma(places)
end
end
@danott
Copy link

danott commented Jan 24, 2014

I like this solution. You've identified that there are three major cases of input, and that is really clear in the code.

Some random thoughts that you can take or leave:

  1. Do you need the local variable str? Ruby's implicit returns could eliminate the need for this variable.
  2. Preference - str_arr could be named a little better. It's not bad, but expressiveness is always encouraged.
  3. Since we're in OO land, str_arr[0] could be replaced by str_arr.first
  4. The shovel (<<) on line 10 is a bit confusing...I think it's because you're in the middle of mutating an array, and then you're concatenating a string. It's not bad, just took a second for my brain to parse.

@squaresurf
Copy link
Author

Great points! I've made two more revisions.

The first revision changes Newcastle to St. Petersberg. This tests the scenario where there are sentence segments that contain spaces.

The second revision addresses your comments. My notes are below:

  1. I was using the str variable since it is usually nice to be able to have the method return at the end of the method. In this case it isn't needed and actually hinders readability since it is such a simple method.
  2. I changed str_arr to segments as in sentence segments.
  3. Great point. I really like the how readable the first method is. Didn't know it was there since I write in php mostly.
  4. I went with the << shovel since I was making a method call and thought it was more readable, but now I see that interpolation clarifies it a bit more. I'm used to only interpolating variables in php and using concatenation for method calls.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment