Class: LangExtract::Core::CharInterval
- Inherits:
-
Object
- Object
- LangExtract::Core::CharInterval
- Defined in:
- lib/langextract/core/data.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#end_pos ⇒ Object
readonly
Returns the value of attribute end_pos.
-
#start_pos ⇒ Object
readonly
Returns the value of attribute start_pos.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #contains?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(start_pos:, end_pos:) ⇒ CharInterval
constructor
A new instance of CharInterval.
- #length ⇒ Object
- #overlaps?(other) ⇒ Boolean
- #shift(offset) ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(start_pos:, end_pos:) ⇒ CharInterval
Returns a new instance of CharInterval.
41 42 43 44 45 46 47 48 |
# File 'lib/langextract/core/data.rb', line 41 def initialize(start_pos:, end_pos:) raise ArgumentError, "start_pos must be non-negative" if start_pos.negative? raise ArgumentError, "end_pos must be >= start_pos" if end_pos < start_pos @start_pos = start_pos @end_pos = end_pos freeze end |
Instance Attribute Details
#end_pos ⇒ Object (readonly)
Returns the value of attribute end_pos.
39 40 41 |
# File 'lib/langextract/core/data.rb', line 39 def end_pos @end_pos end |
#start_pos ⇒ Object (readonly)
Returns the value of attribute start_pos.
39 40 41 |
# File 'lib/langextract/core/data.rb', line 39 def start_pos @start_pos end |
Class Method Details
.from_h(hash) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/langextract/core/data.rb', line 70 def self.from_h(hash) start_pos = HashCoercion.read(hash, :start_pos) end_pos = HashCoercion.read(hash, :end_pos) raise ArgumentError, "start_pos is required" if start_pos.nil? raise ArgumentError, "end_pos is required" if end_pos.nil? new( start_pos: start_pos.to_i, end_pos: end_pos.to_i ) end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
82 83 84 |
# File 'lib/langextract/core/data.rb', line 82 def ==(other) other.is_a?(self.class) && start_pos == other.start_pos && end_pos == other.end_pos end |
#contains?(other) ⇒ Boolean
58 59 60 |
# File 'lib/langextract/core/data.rb', line 58 def contains?(other) start_pos <= other.start_pos && end_pos >= other.end_pos end |
#hash ⇒ Object
87 88 89 |
# File 'lib/langextract/core/data.rb', line 87 def hash [start_pos, end_pos].hash end |
#length ⇒ Object
50 51 52 |
# File 'lib/langextract/core/data.rb', line 50 def length end_pos - start_pos end |
#overlaps?(other) ⇒ Boolean
54 55 56 |
# File 'lib/langextract/core/data.rb', line 54 def overlaps?(other) start_pos < other.end_pos && other.start_pos < end_pos end |
#shift(offset) ⇒ Object
62 63 64 |
# File 'lib/langextract/core/data.rb', line 62 def shift(offset) self.class.new(start_pos: start_pos + offset, end_pos: end_pos + offset) end |
#to_h ⇒ Object
66 67 68 |
# File 'lib/langextract/core/data.rb', line 66 def to_h { "start_pos" => start_pos, "end_pos" => end_pos } end |
#to_s ⇒ Object
91 92 93 |
# File 'lib/langextract/core/data.rb', line 91 def to_s "[#{start_pos}, #{end_pos})" end |