Skip to content

Instantly share code, notes, and snippets.

@saivenkat
Created January 29, 2010 07:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saivenkat/289531 to your computer and use it in GitHub Desktop.
Save saivenkat/289531 to your computer and use it in GitHub Desktop.
The problem: given a list of strings, produce a list where sequential non-empty strings are concatenate
require "test/unit"
class GlomTest < Test::Unit::TestCase
def glom(l)
l.inject([""]) do |r,x|
r << "" if x.empty?
r.last << x
r
end.reject{|a| a.empty?}
end
def test_concatinate_elements_when_list_elements_have_no_empty_between
l = ["a", "b", "c"]
assert_equal ["abc"], glom(l)
end
def test_donot_concatinate_elements_when_they_have_empty_between
l = ["a", "b", "", "c"]
assert_equal(["ab", "c"], glom(l))
i = ["a", "", "", "c"]
assert_equal(["a", "c"], glom(i))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment