Class: Dat::Word
- Inherits:
-
Object
- Object
- Dat::Word
- Defined in:
- lib/dat/word.rb
Instance Attribute Summary collapse
-
#definition ⇒ Object
Returns the value of attribute definition.
-
#relatives ⇒ Object
readonly
Returns the value of attribute relatives.
-
#type ⇒ Object
Returns the value of attribute type.
-
#word ⇒ Object
(also: #get)
readonly
Returns the value of attribute word.
Class Method Summary collapse
-
.relatives(*words) ⇒ Object
Helper to declare that words come from the same root.
Instance Method Summary collapse
- #add_relative(word) ⇒ Object (also: #<<)
-
#initialize(word, defn = "") ⇒ Word
constructor
A new instance of Word.
- #isolate! ⇒ Object
- #to_dict_entry ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(word, defn = "") ⇒ Word
Returns a new instance of Word.
9 10 11 12 13 |
# File 'lib/dat/word.rb', line 9 def initialize(word, defn="") @word = word.upcase @definition = defn @relatives = Set.new end |
Instance Attribute Details
#definition ⇒ Object
Returns the value of attribute definition.
6 7 8 |
# File 'lib/dat/word.rb', line 6 def definition @definition end |
#relatives ⇒ Object (readonly)
Returns the value of attribute relatives.
5 6 7 |
# File 'lib/dat/word.rb', line 5 def relatives @relatives end |
#type ⇒ Object
Returns the value of attribute type.
6 7 8 |
# File 'lib/dat/word.rb', line 6 def type @type end |
#word ⇒ Object (readonly) Also known as: get
Returns the value of attribute word.
5 6 7 |
# File 'lib/dat/word.rb', line 5 def word @word end |
Class Method Details
.relatives(*words) ⇒ Object
Helper to declare that words come from the same root
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/dat/word.rb', line 38 def self.relatives(*words) # make a set of all the relatives relatives = Set.new words.each do |word| word.relatives.each { |rs| relatives.add(rs) } relatives.add(word) end # update words to include the full list of relatives words.each do |word| relatives.each { |r| word.add_relative(r) } end end |
Instance Method Details
#add_relative(word) ⇒ Object Also known as: <<
15 16 17 |
# File 'lib/dat/word.rb', line 15 def add_relative(word) @relatives.add(word) if word.get != @word && !@relatives.include?(word) end |
#isolate! ⇒ Object
20 21 22 23 24 |
# File 'lib/dat/word.rb', line 20 def isolate! @relatives.each { |r| r.relatives.delete(self) } @relatives = Set.new @type = nil end |
#to_dict_entry ⇒ Object
30 31 32 33 34 35 |
# File 'lib/dat/word.rb', line 30 def to_dict_entry str = @word.clone str << (@type ? " (" << @type << ") " : " ") str << @definition.strip << " " unless @definition.strip.empty? str << "[#{@relatives.to_a.join(" ")}]" end |
#to_s ⇒ Object
26 27 28 |
# File 'lib/dat/word.rb', line 26 def to_s @word end |