Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.39 KB | None | 0 0
  1. # flatten.rb
  2. # Example Usage:
  3. # [[1,2,[3]],4].flatten_with_regex
  4.  
  5. class Array
  6. def flatten_with_regex
  7. self
  8. .to_s # This turns the array into: "[[1,2,[3]],4]"
  9. .gsub(/[\[|\]]/ism, "") # Remove those brackets, so it's now: "1,2,3,4"
  10. .split(", ") # Now rejoin into a single array: ["1","2","3","4"]
  11. .map(&:to_i) # Convert all items in array back to integers: [1,2,3,4]
  12. end
  13. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement