Class: String::Selection

Inherits:
Object
  • Object
show all
Defined in:
lib/core_ext/string.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ Selection

Returns a new instance of Selection.



9
10
11
12
13
# File 'lib/core_ext/string.rb', line 9

def initialize(**args)
  @parent = args[:parent]
  @cursor = args[:cursor].to_i
  @value = @parent[@cursor,args[:length]]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/core_ext/string.rb', line 33

def method_missing(m,*args, &block)
  if(!args)
    args = []
  end
  @value.send(m,*args,&block) # preplay action on @value to maintain length
  if(!!args[0] && args[0].is_a?(Integer))
    args[0] = @cursor+args[0]
    @parent.send(m,*args,&block)
  else
    source.send(m,*args,&block)
  end
end

Instance Attribute Details

#cursorObject

Returns the value of attribute cursor.



8
9
10
# File 'lib/core_ext/string.rb', line 8

def cursor
  @cursor
end

#lengthObject

Returns the value of attribute length.



8
9
10
# File 'lib/core_ext/string.rb', line 8

def length
  @length
end

#parentObject

Returns the value of attribute parent.



8
9
10
# File 'lib/core_ext/string.rb', line 8

def parent
  @parent
end

#valueObject

Returns the value of attribute value.



8
9
10
# File 'lib/core_ext/string.rb', line 8

def value
  @value
end

Instance Method Details

#inspectObject



19
20
21
# File 'lib/core_ext/string.rb', line 19

def inspect
  return source
end

#sourceObject



23
24
25
# File 'lib/core_ext/string.rb', line 23

def source
  @parent[self.cursor,self.length]
end

#source=(replacement) ⇒ Object



27
28
29
30
# File 'lib/core_ext/string.rb', line 27

def source=(replacement)
  @parent[self.cursor,self.length] = replacement
  @value = @parent[self.cursor,replacement.length]
end