Skip to content

Instantly share code, notes, and snippets.

@manveru
Created September 4, 2009 10:30
Show Gist options
  • Save manveru/180822 to your computer and use it in GitHub Desktop.
Save manveru/180822 to your computer and use it in GitHub Desktop.
require 'bacon'
Bacon.summary_on_exit
describe 'keychain splitter' do
def split(keychain)
return nil, keychain if keychain.first == '0'
argument, rest = [], []
digits = true
keychain.each do |key|
if digits && key =~ /^\d$/
argument << key
else
digits = false
rest << key
end
end
unless argument.empty?
return argument.join.to_i, rest
else
return nil, rest
end
end
should 'split keychain with single numeric prefix' do
keychain = %w[1 f o o]
split(keychain).should == [1, %w[f]]
end
should 'split keychain with multiple numeric prefix' do
keychain = %w[1 2 3 f o]
split(keychain).should == [123, %w[f]]
end
should 'split keychain without numeric prefix' do
keychain = %w[f o o 1 2]
split(keychain).should == [nil, %w[f o o 1 2]]
end
should 'split keychain zero prefix' do
keychain = %w[0 f o o 1 2]
split(keychain).should == [nil, %w[0 f o o 1 2]]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment