Skip to content
x768 edited this page Jan 21, 2018 · 7 revisions

Text encoding

The encoding of source code must be UTF-8 strictly. Otherwise, you will get following error.

TokenError:Invalid UTF-8 sequence

Line terminating

The end-of-line character must be LF (\n). Any control characters except LF(\n) and TAB(\t) causes following error.

TokenError:Unknown character '\r'

Expressions in parentheses, brackets or curly braces can be split over more than one physical line.

let a = (b + c + d +
         e + f + g)

Semicolon splits a physical line over more than one logical line.

let a = 10; puts a

Integer riterals

The following notations are available. There is no limit for the length of integer literals.

puts 0
puts 010  // 10 (not an octal number)
puts 12_000_000_000_000_000_000

puts 0b1000_0101   // binary notation
puts 0o775         // octal notation
puts 0xDEADBEEF    // hexadecimal notation

The upper character ('B', 'O' and 'X') causes error.

puts 0B0101
puts 0O775
puts 0Xdeadbeef

Floating point number riterals

The following notations are available.

puts 1.0
puts 3.1415_9265_3589
puts 3e8    // 300000000
puts 1.2e-2 // 0.012

Decimal number riterals

The following notations are available. There is no limit for the length of decimal literals.

puts 0d
puts 12_000_000_000_000_000_000d
puts 3.1415_9265_3589d
puts 1.2e-2d  // 0.012

String riterals

puts "abc\tdef"
puts 'C:\Users\fox\Documents'
puts 'Single quote ''.'
puts %(abc\tdef)
puts %q|C:\Users\fox\Documents|

Escape sequences recognized "" and %()

Escape sequence Meaning
\\ Backslash
\' Single quote (')
\" Double quote (")
\b Backspace
\f Formfeed
\n Linefeed(LF)
\r Carriage return (CR)
\t Horizontal Tab (TAB)
\xHH Character with hex code (0x00 - 0x7F)
\x{HHHHHH} Character with hex code (UCS4)
\uHHHH Character with hex code (UCS2)

Bytes riterals

// ASCII characters only
puts b"abc\tdef"
puts b'C:\Users\fox\Documents'
puts %B(abc\tdef)
puts %b(C:\Users\fox\Documents)

puts b"\x41" // A
puts b"\xFF" // Cannot print

Character riterals

puts c'A'
puts c'\x41'   // A
puts c'\u3042' // あ

Regex riterals

if "abcde".find(/c|d/) {}
if "abcde".find(/B/i) {}  // ignore case
if "abc\ndef".find(/c.d/s) {} // dotall
if "abc\ndef".find(/c$/m) {} // multiline
if "あいうえお".find(/[あいう]u/) {}  // UTF-8
if "abcdefg".find(/b c d/x) {} // trim space

if %[abcde].find(%r{c|d}) {}

For more details, see http://www.pcre.org/

Keywords

The following identifiers are used as reserved words.

abstract  break     class     continue  case
catch     def       default   else      elif
false     for       if        import    in
let       null      return    switch    super
this      throw     true      try       var
while     yield