Skip to content

Instantly share code, notes, and snippets.

@igrep
Created July 11, 2010 06:23
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 igrep/471336 to your computer and use it in GitHub Desktop.
Save igrep/471336 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby -wKu
# vim: set fileencoding=utf-8 :
=begin
return the results of both Enumerable#take and Enumerable#drop
=end
module Enumerable
def split_between val
first = true
first_ary = []
second_ary = []
self.each{|e|
if first
first_ary << e
first = false if e == val
else
second_ary << e
end
}
[first_ary, second_ary]
end
def split_between_if &block
first = true
first_ary = []
second_ary = []
self.each{|e|
if first
if yield(e)
first_ary << e
else
first = false
second_ary << e
end
else
second_ary << e
end
}
[first_ary, second_ary]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment