Class: String::Wrapper

Inherits:
Object show all
Defined in:
opal/opal/corelib/string/inheritance.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string = '') ⇒ Wrapper

Returns a new instance of Wrapper



38
39
40
# File 'opal/opal/corelib/string/inheritance.rb', line 38

def initialize(string = '')
  @literal = string
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'opal/opal/corelib/string/inheritance.rb', line 42

def method_missing(*args, &block)
  result = @literal.__send__(*args, &block)

  if `result.$$is_string != null`
    if `result == #@literal`
      self
    else
      self.class.allocate(result)
    end
  else
    result
  end
end

Class Method Details

.[](*objects) ⇒ Object



34
35
36
# File 'opal/opal/corelib/string/inheritance.rb', line 34

def self.[](*objects)
  allocate(objects)
end

.allocate(string = "") ⇒ Object



22
23
24
25
26
# File 'opal/opal/corelib/string/inheritance.rb', line 22

def self.allocate(string = "")
  obj = super()
  `obj.literal = string`
  obj
end

.new(*args, &block) ⇒ Object



28
29
30
31
32
# File 'opal/opal/corelib/string/inheritance.rb', line 28

def self.new(*args, &block)
  obj = allocate
  obj.initialize(*args, &block)
  obj
end

Instance Method Details

#%(data) ⇒ Object



116
117
118
# File 'opal/opal/corelib/string/inheritance.rb', line 116

def %(data)
  @literal % data
end

#*(other) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'opal/opal/corelib/string/inheritance.rb', line 85

def *(other)
  %x{
    var result = #{@literal * other};

    if (result.$$is_string) {
      return #{self.class.allocate(`result`)}
    }
    else {
      return result;
    }
  }
end

#+(other) ⇒ Object



81
82
83
# File 'opal/opal/corelib/string/inheritance.rb', line 81

def +(other)
  @literal + other
end

#==(other) ⇒ Object Also known as: eql?, ===



64
65
66
# File 'opal/opal/corelib/string/inheritance.rb', line 64

def ==(other)
  @literal == other
end

#each_line(separator = $/) ⇒ Object



106
107
108
109
# File 'opal/opal/corelib/string/inheritance.rb', line 106

def each_line(separator = $/)
  return enum_for :each_line, separator unless block_given?
  @literal.each_line(separator){|str| yield self.class.allocate(str)}
end

#initialize_copy(other) ⇒ Object



56
57
58
# File 'opal/opal/corelib/string/inheritance.rb', line 56

def initialize_copy(other)
  @literal = `other.literal`.clone
end

#inspectObject



77
78
79
# File 'opal/opal/corelib/string/inheritance.rb', line 77

def inspect
  @literal.inspect
end

#instance_variablesObject



120
121
122
# File 'opal/opal/corelib/string/inheritance.rb', line 120

def instance_variables
  super - ['@literal']
end

#lines(separator = $/, &block) ⇒ Object



111
112
113
114
# File 'opal/opal/corelib/string/inheritance.rb', line 111

def lines(separator = $/, &block)
  e = each_line(separator, &block)
  block ? self : e.to_a
end

#replace(string) ⇒ Object



102
103
104
# File 'opal/opal/corelib/string/inheritance.rb', line 102

def replace(string)
  @literal = string
end

#respond_to?(name) ⇒ Boolean

Returns:



60
61
62
# File 'opal/opal/corelib/string/inheritance.rb', line 60

def respond_to?(name, *)
  super || @literal.respond_to?(name)
end

#split(pattern = undefined, limit = undefined) ⇒ Object



98
99
100
# File 'opal/opal/corelib/string/inheritance.rb', line 98

def split(pattern = undefined, limit = undefined)
  @literal.split(pattern, limit).map{|str| self.class.allocate(str)}
end

#to_sObject Also known as: to_str



71
72
73
# File 'opal/opal/corelib/string/inheritance.rb', line 71

def to_s
  @literal
end