- Split sentence preserve punctuation
https://stackoverflow.com/a/15687656/2096740
Use scan
(throw strip in there to get rid of trailing spaces).
s = "I am a lion. Hear me roar! Where is my cub? Never mind, found him."
s.scan(/[^\.!?]+[\.!?]/).map(&:strip) # => ["I am a lion.", "Hear me roar!", "Where is my cub?", "Never mind, found him."]
- Simple detect puncutation in string
https://stackoverflow.com/a/5541356/2096740
sub!(/[?.!,;]?$/, '')
- Read large files in batches
https://stackoverflow.com/questions/2962134/ruby-read-file-in-batches
File.open('filename','r') do |f|
chunk = f.read(2048)
...
end
- Easy strip HTML/XML tags from string
str = "Remind me to <task>buy milk</task> <time>in one hour</time>"
puts str.gsub(/<\/?[^>]*>/, "")
=> "Remind me to buy milk in one hour"
- Keep delimiters when splitting strings
https://stackoverflow.com/questions/18089562/how-do-i-keep-the-delimiters-when-splitting-a-ruby-string
content = "Do you like to code? How I love to code! I'm always coding."
content.split(/(?<=[?.!])/)
# Returns an array with:
# ["Do you like to code?", " How I love to code!", " I'm always coding."]
- Split string by multiple delimiters
https://stackoverflow.com/questions/19509307/split-string-by-multiple-delimiters
"a,b'c d".split /\s|'|,/
# => ["a", "b", "c", "d"]
- Split a string and its punctuation
https://stackoverflow.com/questions/32037300/splitting-a-string-into-words-and-punctuation-with-ruby
s = "here...is a happy-go-lucky string that I'm writing"
s.scan(/[\w'-]+|[[:punct:]]+/)
#=> ["here", "...", "is", "a", "happy-go-lucky", "string", "that", "I'm", "writing"]
- Search within a two-dimensional array
https://stackoverflow.com/questions/29745810/how-to-search-within-a-two-dimensional-array
array = [[1,1], [1,2], [1,3], [2,1], [2,4], [2,5]]
array.select{|(x, y)| x == 1}
# => [[1, 1], [1, 2], [1, 3]]
array.select{|(x, y)| x == 1}.map{|(x, y)| y}
# => [1, 2, 3]