Unicode variable names: Difference between revisions

→‎{{header|Ruby}}: Removed ruby 1.9 stuff (even Ruby 2.0 is EOL)
(Added FreeBASIC)
(→‎{{header|Ruby}}: Removed ruby 1.9 stuff (even Ruby 2.0 is EOL))
Line 658:
 
=={{header|Ruby}}==
Ruby supports about 100 encodings, the default being UTF-8.
This task requires Ruby 1.9. Multilingualization, or m17n, is a major new feature of Ruby 1.9. With m17n, the identifiers can use the non-ASCII characters. Ruby is a Code Set Independent (CSI) language, so there are many different character encodings.
<lang ruby>Δ = 1
 
# Any non-ASCII characters require a '''magic comment''' to select the encoding.
# Ruby source code must be '''ASCII compatible'''. For example, SJIS and UTF-8 are ASCII compatible, but ISO-2022-JP and UTF-16LE are not compatible. So one can write the source file in UTF-8, but not in UTF-16LE.
 
A more complete reference is [http://yokolet.blogspot.com/2009/07/design-and-implementation-of-ruby-m17n.html ''The design and implementation of Ruby M17N''].
 
The next example uses a magic comment to select the Big5 encoding. Then it creates a local variable named Δ.
{{works with|Ruby|1.9}}
<lang ruby># -*- coding: big5 -*-
Δ = 1
Δ += 1
puts Δ # => 2</lang>
 
<pre>00000000 23 20 2d 2a 2d 20 63 6f 64 69 6e 67 3a 20 62 69 |# -*- coding: bi|
00000010 67 35 20 2d 2a 2d 0a a3 47 20 3d 20 31 0a a3 47 |g5 -*-.Δ = 1.Δ|
00000020 20 2b 3d 20 31 0a 70 75 74 73 20 a3 47 0a | += 1.puts Δ.|
0000002e</pre>
The output is <code>2</code>.
One can also use the non-ASCII characters in a method name. The next example selects the EUC-JP encoding, and creates a method named ≦, with a parameter named ♯♭♪. Because ≦ is an ordinary method, not an operator, so the program must use a dot to call the method.
{{works with|Ruby|1.9}}
<lang ruby># -*- coding: euc-jp -*-
 
class Numeric
def ≦(♯♭♪)
self <= ♯♭♪
end
end
 
∞ = Float::INFINITY
±5 = [-5, 5]
p [(±5.first.≦ ∞),
(±5.last.≦ ∞),
(∞.≦ ∞)]</lang>
<pre>00000000 23 20 2d 2a 2d 20 63 6f 64 69 6e 67 3a 20 65 75 |# -*- coding: eu|
00000010 63 2d 6a 70 20 2d 2a 2d 0a 0a 63 6c 61 73 73 20 |c-jp -*-..class |
00000020 4e 75 6d 65 72 69 63 0a 20 20 64 65 66 20 a1 e5 |Numeric. def ≦|
00000030 28 a2 f4 a2 f5 a2 f6 29 0a 20 20 20 20 73 65 6c |(♯♭♪). sel|
00000040 66 20 3c 3d 20 a2 f4 a2 f5 a2 f6 0a 20 20 65 6e |f <= ♯♭♪. en|
00000050 64 0a 65 6e 64 0a 0a a1 e7 20 3d 20 46 6c 6f 61 |d.end..∞ = Floa|
00000060 74 3a 3a 49 4e 46 49 4e 49 54 59 0a a1 de 35 20 |t::INFINITY.±5 |
00000070 3d 20 5b 2d 35 2c 20 35 5d 0a 70 20 5b 28 a1 de |= [-5, 5].p [(±|
00000080 35 2e 66 69 72 73 74 2e a1 e5 20 a1 e7 29 2c 0a |5.first.≦ ∞),.|
00000090 20 20 20 28 a1 de 35 2e 6c 61 73 74 2e a1 e5 20 | (±5.last.≦ |
000000a0 a1 e7 29 2c 0a 20 20 20 28 a1 e7 2e a1 e5 20 a1 |∞),. (∞.≦ |
000000b0 e7 29 5d 0a |)].|
000000b4</pre>
The output is <code>[true, true, true]</code> because the numbers -5, 5 and infinity are all less than or equal to infinity.
 
=={{header|Rust}}==
1,149

edits