Class: String19::Wrapper
- Inherits:
-
BlankSlate
- Object
- BlankSlate
- String19::Wrapper
- Defined in:
- lib/string19.rb
Instance Attribute Summary collapse
-
#chars ⇒ Object
readonly
Returns the value of attribute chars.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #bytesize ⇒ Object
- #class ⇒ Object
- #encoding ⇒ Object
- #gsub(*args, &block) ⇒ Object
- #gsub!(*args, &block) ⇒ Object
- #index(what, offset = 0) ⇒ Object
-
#initialize(wrapped) ⇒ Wrapper
constructor
A new instance of Wrapper.
- #insert(position, item) ⇒ Object
- #is_a?(klass) ⇒ Boolean
- #to_s ⇒ Object
- #valid_encoding? ⇒ Boolean
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
#chars ⇒ Object (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 |
#bytesize ⇒ Object
21 22 23 |
# File 'lib/string19.rb', line 21 def bytesize to_s.size end |
#class ⇒ Object
33 34 35 |
# File 'lib/string19.rb', line 33 def class String end |
#encoding ⇒ Object
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
37 38 39 |
# File 'lib/string19.rb', line 37 def is_a?(klass) [String19::Wrapper, String, Object].include?(klass) end |
#to_s ⇒ Object
46 47 48 |
# File 'lib/string19.rb', line 46 def to_s @chars.to_s end |
#valid_encoding? ⇒ Boolean
50 51 52 |
# File 'lib/string19.rb', line 50 def valid_encoding? true # i hope so :P end |