Class: GedString
- Inherits:
-
String
- Object
- String
- GedString
- Defined in:
- lib/gedcom/ged_string.rb
Overview
Our parser tokenize strings into word arrays. This is helpful in word wrapping for CONT and CONC, but pretty silly for most other uses. I'm still thinking about the best way to deal with this.
The current thought is to flatten the array and store the value as a space separated string, and tokenize tokenize it again if we output GEDCOM. Most of the time, this is what we want, and it makes dealing with Data bases easier too.
Instance Method Summary collapse
-
#each {|_self| ... } ⇒ Object
We aren't an array, but to simplify some code, the method each is defined to return our 1 value.
-
#each_word ⇒ Object
yields the string word by word.
-
#each_word_with_index ⇒ Object
(also: #each_with_index)
yields the string word by word.
-
#initialize(word_array) ⇒ GedString
constructor
takes a word array from the parser and creates a space separated string.
Constructor Details
#initialize(word_array) ⇒ GedString
takes a word array from the parser and creates a space separated string. Also excepts a String, assigning this to the
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/gedcom/ged_string.rb', line 13 def initialize(word_array) if(word_array.class == String) super word_array elsif(word_array.class == Array) super(word_array.inject('') do |v, w| if v == '' w elsif w == "\n" || v[-1,1] == "\n" v + w else v + ' ' + w end end ) else raise "GedString word_array passed in as #{word_array.class}" end end |
Instance Method Details
#each {|_self| ... } ⇒ Object
We aren't an array, but to simplify some code, the method each is defined to return our 1 value.
43 44 45 |
# File 'lib/gedcom/ged_string.rb', line 43 def each yield self end |
#each_word ⇒ Object
yields the string word by word. Line separators in the string will also be yielded as words.
33 34 35 |
# File 'lib/gedcom/ged_string.rb', line 33 def each_word self.gsub(/\n/," \n ").split(/ /).each { |w| yield w } end |
#each_word_with_index ⇒ Object Also known as: each_with_index
yields the string word by word. Line separators in the string will also be yielded as words.
38 39 40 |
# File 'lib/gedcom/ged_string.rb', line 38 def each_word_with_index self.gsub(/\n/," \n ").split(/ /).each_with_index { |w,i| yield(w, i) } end |