Guest User

Untitled

a guest
Jul 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. outcome_array=the_text.split(pattern_to_split_by)
  2.  
  3. irb(main):015:0> s = "split on the word on okay?"
  4. => "split on the word on okay?"
  5. irb(main):016:0> b=[]; s.split(/(on)/).each_slice(2) { |s| b << s.join }; b
  6. => ["split on", " the word on", " okay?"]
  7.  
  8. irb(main):007:0> "split it here and here okay".split(/ (here) /)
  9. => ["split it", "here", "and", "here", "okay"]
  10.  
  11. s = "split on the word on and include on with previous"
  12. a = s.split(/(on)/)
  13.  
  14. # iterate through and combine adjacent items together and store
  15. # results in a second array
  16. b = []
  17. a.each_index{ |i|
  18. b << a[i] if i.even?
  19. b[b.length - 1] += a[i] if i.odd?
  20. }
  21.  
  22. print b
  23.  
  24. ["split on", " the word on", " and include on", " with previous"]
Add Comment
Please, Sign In to add comment