Class: String19::Wrapper

Inherits:
BlankSlate show all
Defined in:
lib/string19.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wrapped) ⇒ Wrapper

Returns a new instance of Wrapper.



17
18
19
# File 'lib/string19.rb', line 17

def initialize(wrapped)
  @chars = wrapped.scan(/./m)
end

Instance Attribute Details

#charsObject (readonly)

Returns the value of attribute chars.



15
16
17
# File 'lib/string19.rb', line 15

def chars
  @chars
end

Class Method Details

.delegate(*args) ⇒ Object



87
88
89
90
91
# File 'lib/string19.rb', line 87

def self.delegate(*args)
  args.each do |method|
    eval("def #{method}(*args, &block); @chars.#{method}(*args, &block); end")
  end
end

.delegate_to_s(*args) ⇒ Object



93
94
95
96
97
# File 'lib/string19.rb', line 93

def self.delegate_to_s(*args)
  args.each do |method|
    eval("def #{method}(*args, &block); to_s.#{method}(*args, &block); end")
  end
end

.wrap(*args) ⇒ Object



81
82
83
84
85
# File 'lib/string19.rb', line 81

def self.wrap(*args)
  args.each do |method|
    eval("def #{method}(*args, &block); String19(@chars.#{method}(*args, &block)); end")
  end
end

Instance Method Details

#==(other) ⇒ Object



41
42
43
44
# File 'lib/string19.rb', line 41

def ==(other)
  other = other.to_s if other.is_a?(String19::Wrapper)
  other.is_a?(String) and to_s == other
end

#bytesizeObject



21
22
23
# File 'lib/string19.rb', line 21

def bytesize
  to_s.size
end

#classObject



33
34
35
# File 'lib/string19.rb', line 33

def class
  String
end

#encodingObject



29
30
31
# File 'lib/string19.rb', line 29

def encoding
  Encoding.new('UTF-8')
end

#gsub(*args, &block) ⇒ Object



76
77
78
79
# File 'lib/string19.rb', line 76

def gsub(*args, &block)
  copy = dup
  copy.gsub!(*args, &block) || copy
end

#gsub!(*args, &block) ⇒ Object



70
71
72
73
74
# File 'lib/string19.rb', line 70

def gsub!(*args, &block)
  return unless result = to_s.send(:gsub!, *args, &block)
  @chars = result.scan(/./m)
  self
end

#index(what, offset = 0) ⇒ Object



54
55
56
57
58
59
# File 'lib/string19.rb', line 54

def index(what, offset=0)
  s = to_s
  offset = self[0...offset].to_s.size
  return unless found = s.index(what, offset)
  String19(s[0...found]).size
end

#insert(position, item) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/string19.rb', line 61

def insert(position, item)
  if position.abs > size 
    raise IndexError, "index #{position} out of string"
  end

  @chars.insert(position, *String19(item).chars.to_a)
  self
end

#is_a?(klass) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/string19.rb', line 37

def is_a?(klass)
  [String19::Wrapper, String, Object].include?(klass)
end

#to_sObject



46
47
48
# File 'lib/string19.rb', line 46

def to_s
  @chars.to_s
end

#valid_encoding?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/string19.rb', line 50

def valid_encoding?
  true # i hope so :P
end