Class: SubString

Inherits:
SubObject
  • Object
show all
Defined in:
lib/sub_string.rb

Overview

Child class of SubObject for String: SubString

See for detail the full reference in the top page SubString and in Github and also SubObject and that in Github

Author:

  • Masa Sakano (Wise Babel Ltd)

Constant Summary collapse

TO_SOURCE_METHOD =

Symbol of the method that projects to (returns) the original-like instance; e.g., :to_str for String. The value should be overwritten in the child class of SubObject, namely this class SubString in this case.

:to_str

Instance Method Summary collapse

Constructor Details

#initialize(source, pos = 0, size = nil, attr: nil) ⇒ SubString

Returns a new instance of SubString equivalent to source[ pos, size ]

Difference from the original SubObject is the defaults are set for the second and third parameters.

If the third parameter is nil, it is set as the maximum size possible counted from pos (the starting position)

Parameters:

  • source (String)
  • pos (Integer) (defaults to: 0)

    Starting character index position.

  • size (Integer, nil) (defaults to: nil)

    Size of the substring to make.

  • attr: (Object) (defaults to: nil)

    user-specified arbitrary object



34
35
36
37
# File 'lib/sub_string.rb', line 34

def initialize(source, pos=0, size=nil, attr: nil)
  size ||= ((pos >= 0) ? source.size - pos : -pos)
  super source, pos, size, attr: attr
end