Module: Chunkify
- Defined in:
- lib/chunkify.rb,
lib/chunkify/version.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- VERSION =
"0.1.0"- @@SIZE =
1024
Class Method Summary collapse
Class Method Details
.size ⇒ Object
18 19 20 |
# File 'lib/chunkify.rb', line 18 def self.size @@SIZE end |
.size!(input) ⇒ Object
10 11 12 |
# File 'lib/chunkify.rb', line 10 def self.size! input @@SIZE = 2 ** input.split(" ").length end |
.size=(s) ⇒ Object
14 15 16 |
# File 'lib/chunkify.rb', line 14 def self.size= s @@SIZE = s end |
.split(text, &b) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/chunkify.rb', line 22 def self.split text, &b if !block_given? b = lambda { |e| e } end chunk = [] paragraphs = text.split(/\n\n+/) current_chunk = "" paragraphs.each do |para| if current_chunk.length + para.length > Chunkify.size && !current_chunk.empty? chunk << b.call(current_chunk) current_chunk = para else current_chunk += (current_chunk.empty? ? "" : "\n\n") + para end end chunk << b.call(current_chunk) unless current_chunk.empty? end |