Skip to content

Instantly share code, notes, and snippets.

@semmons99
Created January 18, 2011 18:56
Show Gist options
  • Save semmons99/784930 to your computer and use it in GitHub Desktop.
Save semmons99/784930 to your computer and use it in GitHub Desktop.
def parse(name)
case name
when /^\[(.+)\](.+)\[(.+)\]$/
[ $1 + $2 + $3, $1 + $2, $2 + $3, $2 ]
when /^\[(.+)\](.+)$/
[ $1 + $2, $2 ]
when /^(.+)\[(.+)\]$/
[ $1, $1 + $2 ]
else
[ name ]
end
end
describe "parse" do
it "should list all permutations, text in brackets is optional" do
strings = parse("foo[bar]")
strings.should include('foo')
strings.should include('foobar')
strings.should_not include('bar')
strings.size.should == 2
end
it "should list all permutations, text in brackets is optional" do
strings = parse("[foo]bar")
strings.should include('bar')
strings.should include('foobar')
strings.should_not include('foo')
strings.size.should == 2
end
it "should list all permutations, text in brackets is optional" do
strings = parse("[foo]bar[baz]")
strings.should include('foobar')
strings.should include('bar')
strings.should include('barbaz')
strings.should include('foobarbaz')
strings.should_not include('foo')
strings.should_not include('baz')
strings.size.should == 4
end
it "should return the string, when there are no brackets" do
strings = parse("foo")
strings.should include('foo')
strings.size.should == 1
end
end
@doolin
Copy link

doolin commented Jan 18, 2011

This looks really familiar... I think I'm working through a different section of the same tutorial?

@semmons99
Copy link
Author

I got it from here

@doolin
Copy link

doolin commented Jan 18, 2011

I like it. It's a good pattern. I'm a noob to ruby (not to programming), but I picked this up at a glance. Thanks for posting it.

@semmons99
Copy link
Author

no problem

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