manveru (owner)

Revisions

gist: 180822 Download_button fork
public
Public Clone URL: git://gist.github.com/180822.git
Embed All Files: show embed
split_keychain.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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