summaryrefslogtreecommitdiff
path: root/doc/string.rdoc
blob: a4423c9d9bf1c67a2820623d0077cd4c48d06346 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# :markup: ruby

class String
  # call-seq:
  #   String.new(string = '') -> new_string
  #   String.new(string = '', encoding: encoding) -> new_string
  #   String.new(string = '', capacity: size) -> new_string
  #
  # Returns a new \String that is a copy of +string+.
  #
  # With no arguments, returns the empty string with the Encoding <tt>ASCII-8BIT</tt>:
  #   s = String.new
  #   s # => ""
  #   s.encoding # => #<Encoding:ASCII-8BIT>
  #
  # With the single \String argument +string+, returns a copy of +string+
  # with the same encoding as +string+:
  #   s = String.new('Que veut dire ça?')
  #   s # => "Que veut dire ça?"
  #   s.encoding # => #<Encoding:UTF-8>
  #
  # Literal strings like <tt>""</tt> or here-documents always use
  # Encoding@Script+encoding, unlike String.new.
  #
  # With keyword +encoding+, returns a copy of +str+
  # with the specified encoding:
  #   s = String.new(encoding: 'ASCII')
  #   s.encoding # => #<Encoding:US-ASCII>
  #   s = String.new('foo', encoding: 'ASCII')
  #   s.encoding # => #<Encoding:US-ASCII>
  #
  # Note that these are equivalent:
  #   s0 = String.new('foo', encoding: 'ASCII')
  #   s1 = 'foo'.force_encoding('ASCII')
  #   s0.encoding == s1.encoding # => true
  #
  # With keyword +capacity+, returns a copy of +str+;
  # the given +capacity+ may set the size of the internal buffer,
  # which may affect performance:
  #   String.new(capacity: 1) # => ""
  #   String.new(capacity: 4096) # => ""
  #
  # The +string+, +encoding+, and +capacity+ arguments may all be used together:
  #
  #   String.new('hello', encoding: 'UTF-8', capacity: 25)
  #
  def initialize(str = '', encoding: nil, capacity: nil)
    Primitive.rb_str_init(str, encoding, capacity)
  end

  #  call-seq:
  #    bytes -> array_of_bytes
  #
  #  Returns an array of the bytes in +self+:
  #
  #    'hello'.bytes # => [104, 101, 108, 108, 111]
  #    'тест'.bytes  # => [209, 130, 208, 181, 209, 129, 209, 130]
  #    'こんにちは'.bytes
  #    # => [227, 129, 147, 227, 130, 147, 227, 129, 171, 227, 129, 161, 227, 129, 175]
  #
  def bytes; end

  #  call-seq:
  #    each_byte {|byte| ... } -> self
  #    each_byte               -> enumerator
  #
  #  Calls the given block with each successive byte from +self+;
  #  returns +self+:
  #
  #    'hello'.each_byte {|byte| print byte, ' ' }
  #    print "\n"
  #    'тест'.each_byte {|byte| print byte, ' ' }
  #    print "\n"
  #    'こんにちは'.each_byte {|byte| print byte, ' ' }
  #    print "\n"
  #
  #  Output:
  #
  #    104 101 108 108 111
  #    209 130 208 181 209 129 209 130
  #    227 129 147 227 130 147 227 129 171 227 129 161 227 129 175
  #
  #  Returns an enumerator if no block is given.
  def each_byte; end


  #  call-seq:
  #    each_line(line_sep = $/, chomp: false) {|substring| ... } -> self
  #    each_line(line_sep = $/, chomp: false)                    -> enumerator
  #
  #  With a block given, forms the substrings ("lines")
  #  that are the result of splitting +self+
  #  at each occurrence of the given line separator +line_sep+;
  #  passes each line to the block;
  #  returns +self+:
  #
  #    s = <<~EOT
  #    This is the first line.
  #    This is line two.
  #
  #    This is line four.
  #    This is line five.
  #    EOT
  #
  #    s.each_line {|line| p line }
  #
  #  Output:
  #
  #    "This is the first line.\n"
  #    "This is line two.\n"
  #    "\n"
  #    "This is line four.\n"
  #    "This is line five.\n"
  #
  #  With a different +line_sep+:
  #
  #    s.each_line(' is ') {|line| p line }
  #
  #  Output:
  #
  #    "This is "
  #    "the first line.\nThis is "
  #    "line two.\n\nThis is "
  #    "line four.\nThis is "
  #    "line five.\n"
  #
  #  With +chomp+ as +true+, removes the trailing +line_sep+ from each line:
  #
  #    s.each_line(chomp: true) {|line| p line }
  #
  #  Output:
  #
  #    "This is the first line."
  #    "This is line two."
  #    ""
  #    "This is line four."
  #    "This is line five."
  #
  #  With an empty string as +line_sep+,
  #  forms and passes "paragraphs" by splitting at each occurrence
  #  of two or more newlines:
  #
  #    s.each_line('') {|line| p line }
  #
  #  Output:
  #
  #    "This is the first line.\nThis is line two.\n\n"
  #    "This is line four.\nThis is line five.\n"
  #
  #  With no block given, returns an enumerator.
  #
  def each_line; end

  #  call-seq:
  #    split(field_sep = $;, limit = nil) -> array
  #    split(field_sep = $;, limit = nil) {|substring| ... } -> self
  #
  #  Returns an array of substrings of +self+
  #  that are the result of splitting +self+
  #  at each occurrence of the given field separator +field_sep+.
  #
  #  When +field_sep+ is <tt>$;</tt>:
  #
  #  - If <tt>$;</tt> is +nil+ (its default value),
  #    the split occurs just as if +field_sep+ were given as a space character
  #    (see below).
  #
  #  - If <tt>$;</tt> is a string,
  #    the split ocurs just as if +field_sep+ were given as that string
  #    (see below).
  #
  #  When +field_sep+ is <tt>' '</tt> and +limit+ is +nil+,
  #  the split occurs at each sequence of whitespace:
  #
  #    'abc def ghi'.split(' ')          # => ["abc", "def", "ghi"]
  #    "abc \n\tdef\t\n  ghi".split(' ') # => ["abc", "def", "ghi"]
  #    'abc  def   ghi'.split(' ')       # => ["abc", "def", "ghi"]
  #    ''.split(' ')                     # => []
  #
  #  When +field_sep+ is a string different from <tt>' '</tt>
  #  and +limit+ is +nil+,
  #  the split occurs at each occurrence of +field_sep+;
  #  trailing empty substrings are not returned:
  #
  #    'abracadabra'.split('ab')   # => ["", "racad", "ra"]
  #    'aaabcdaaa'.split('a')      # => ["", "", "", "bcd"]
  #    ''.split('a')               # => []
  #    '3.14159'.split('1')        # => ["3.", "4", "59"]
  #    '!@#$%^$&*($)_+'.split('$') # => ["!@#", "%^", "&*(", ")_+"]
  #    'тест'.split('т')           # => ["", "ес"]
  #    'こんにちは'.split('に')      # => ["こん", "ちは"]
  #
  #  When +field_sep+ is a Regexp and +limit+ is +nil+,
  #  the split occurs at each occurrence of a match;
  #  trailing empty substrings are not returned:
  #
  #    'abracadabra'.split(/ab/) # => ["", "racad", "ra"]
  #    'aaabcdaaa'.split(/a/)    # => ["", "", "", "bcd"]
  #    'aaabcdaaa'.split(//)     # => ["a", "a", "a", "b", "c", "d", "a", "a", "a"]
  #    '1 + 1 == 2'.split(/\W+/) # => ["1", "1", "2"]
  #
  #  If the \Regexp contains groups, their matches are also included
  #  in the returned array:
  #
  #    '1:2:3'.split(/(:)()()/, 2) # => ["1", ":", "", "", "2:3"]
  #
  #  As seen above, if +limit+ is +nil+,
  #  trailing empty substrings are not returned;
  #  the same is true if +limit+ is zero:
  #
  #    'aaabcdaaa'.split('a')    # => ["", "", "", "bcd"]
  #    'aaabcdaaa'.split('a', 0) # => ["", "", "", "bcd"]
  #
  #  If +limit+ is positive integer +n+, no more than <tt>n - 1-</tt>
  #  splits occur, so that at most +n+ substrings are returned,
  #  and trailing empty substrings are included:
  #
  #    'aaabcdaaa'.split('a', 1) # => ["aaabcdaaa"]
  #    'aaabcdaaa'.split('a', 2) # => ["", "aabcdaaa"]
  #    'aaabcdaaa'.split('a', 5) # => ["", "", "", "bcd", "aa"]
  #    'aaabcdaaa'.split('a', 7) # => ["", "", "", "bcd", "", "", ""]
  #    'aaabcdaaa'.split('a', 8) # => ["", "", "", "bcd", "", "", ""]
  #
  #  Note that if +field_sep+ is a \Regexp containing groups,
  #  their matches are in the returned array, but do not count toward the limit.
  #
  #  If +limit+ is negative, it behaves the same as if +limit+ was +nil+,
  #  meaning that there is no limit,
  #  and trailing empty substrings are included:
  #
  #    'aaabcdaaa'.split('a', -1) # => ["", "", "", "bcd", "", "", ""]
  #
  #  If a block is given, it is called with each substring:
  #
  #    'abc def ghi'.split(' ') {|substring| p substring }
  #
  #  Output:
  #
  #    "abc"
  #    "def"
  #    "ghi"
  #
  def split; end

end