16 July 2014

slice_before is a convenient method to split stuff.

For example you could easily split an array on even values.

[1,2,3,4,5].slice_before{|e| e%2 == 0 }.map{|a| a}

you could split the hash by the keys you want.

def split_hash_by_key(hash, *args)
  hash.slice_before { |k,v| args.include? k }.map { |a| a.to_h }
end

# split_hash_by_key({ :a=>1, :b=>2, :c=>3, :d=>4, :e=>5, :f=>6 }, :c, :e )
# [ {:a=>1, :b=>2}, {:c=>3, :d=>4}, {:e=>5, :f=>6} ]
"abcedfghijklmnopqrstuvwxyz".split('').slice_before{|c| c =~ /[aeiou]/i }.map{|a| a}
# [["a", "b", "c"], ["e", "d", "f", "g", "h"], ["i", "j", "k", "l", "m", "n"], ["o", "p", "q", "r", "s", "t"], ["u", "v", "w", "x", "y", "z"]]

Check http://apidock.com/ruby/Enumerable/slice_before for more info.