File: encode.rdoc

package info (click to toggle)
ruby3.4 3.4.5-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 154,784 kB
  • sloc: ruby: 1,259,653; ansic: 829,955; yacc: 28,233; pascal: 7,359; sh: 3,864; python: 1,799; cpp: 1,158; asm: 808; makefile: 801; javascript: 414; lisp: 109; perl: 62; awk: 36; sed: 4; xml: 4
file content (47 lines) | stat: -rw-r--r-- 1,935 bytes parent folder | download | duplicates (2)
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
Returns a copy of +self+ transcoded as determined by +dst_encoding+.
By default, raises an exception if +self+
contains an invalid byte or a character not defined in +dst_encoding+;
that behavior may be modified by encoding options; see below.

With no arguments:

- Uses the same encoding if <tt>Encoding.default_internal</tt> is +nil+
  (the default):

    Encoding.default_internal # => nil
    s = "Ruby\x99".force_encoding('Windows-1252')
    s.encoding                # => #<Encoding:Windows-1252>
    s.bytes                   # => [82, 117, 98, 121, 153]
    t = s.encode              # => "Ruby\x99"
    t.encoding                # => #<Encoding:Windows-1252>
    t.bytes                   # => [82, 117, 98, 121, 226, 132, 162]

- Otherwise, uses the encoding <tt>Encoding.default_internal</tt>:

    Encoding.default_internal = 'UTF-8'
    t = s.encode              # => "Ruby™"
    t.encoding                # => #<Encoding:UTF-8>

With only argument +dst_encoding+ given, uses that encoding:

  s = "Ruby\x99".force_encoding('Windows-1252')
  s.encoding            # => #<Encoding:Windows-1252>
  t = s.encode('UTF-8') # => "Ruby™"
  t.encoding            # => #<Encoding:UTF-8>

With arguments +dst_encoding+ and +src_encoding+ given,
interprets +self+ using +src_encoding+, encodes the new string using +dst_encoding+:

  s = "Ruby\x99"
  t = s.encode('UTF-8', 'Windows-1252') # => "Ruby™"
  t.encoding                            # => #<Encoding:UTF-8>

Optional keyword arguments +enc_opts+ specify encoding options;
see {Encoding Options}[rdoc-ref:encodings.rdoc@Encoding+Options].

Please note that, unless <code>invalid: :replace</code> option is
given, conversion from an encoding +enc+ to the same encoding +enc+
(independent of whether +enc+ is given explicitly or implicitly) is a
no-op, i.e. the string is simply copied without any changes, and no
exceptions are raised, even if there are invalid bytes.