Information technology — Programming languages — Ruby

ISO/IEC 30170:2012 specifies the syntax and semantics of the computer programming language Ruby, and the requirements for conforming Ruby processors, strictly conforming Ruby programs, and conforming Ruby programs. It does not specify the limit of size or complexity of a program text which a conforming processor evaluates, the minimal requirements of a data processing system that is capable of supporting a conforming processor, the method for activating the execution of programs on a data processing system, and the method for reporting syntactic and runtime errors.

Technologies de l'information — Langages de programmation — Ruby

General Information

Status
Published
Publication Date
11-Apr-2012
Current Stage
9093 - International Standard confirmed
Start Date
29-Apr-2021
Completion Date
19-Apr-2025
Ref Project
Standard
ISO/IEC 30170:2012 - Information technology -- Programming languages -- Ruby
English language
313 pages
sale 15% off
Preview
sale 15% off
Preview

Standards Content (Sample)


INTERNATIONAL ISO/IEC
STANDARD 30170
First edition
2012-04-15
Information technology — Programming
languages — Ruby
Technologies de l'information — Langages de programmation — Ruby

Reference number
©
ISO/IEC 2012
©  ISO/IEC 2012
All rights reserved. Unless otherwise specified, no part of this publication may be reproduced or utilized in any form or by any means,
electronic or mechanical, including photocopying and microfilm, without permission in writing from either ISO at the address below or
ISO's member body in the country of the requester.
ISO copyright office
Case postale 56  CH-1211 Geneva 20
Tel. + 41 22 749 01 11
Fax + 41 22 749 09 47
E-mail copyright@iso.org
Web www.iso.org
Published in Switzerland
ii © ISO/IEC 2012 – All rights reserved

Contents Page
1 Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2 Normative references . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
3 Conformance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
4 Terms and definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
5 Notational conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
5.1 General description. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
5.2 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
5.2.1 General description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
5.2.2 Productions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
5.2.3 Syntactic term sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
5.2.4 Syntactic terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
5.2.5 Conceptual names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
5.3 Semantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
5.4 Attributes of execution contexts . . . . . . . . . . . . . . . . . . . . . . . . . . 11
6 Fundamental concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
6.1 Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
6.2 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
6.2.1 General description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
6.2.2 Instance variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
6.3 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
6.4 Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
6.5 Classes, singleton classes, and modules . . . . . . . . . . . . . . . . . . . . . . . 14
6.5.1 General description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
6.5.2 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
6.5.3 Singleton classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
6.5.4 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
6.5.5 Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
6.6 Boolean values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
7 Execution contexts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
7.1 General description. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
7.2 The initial state . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
8 Lexical structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
8.1 General description. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
8.2 Program text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
8.3 Line terminators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
8.4 Whitespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
8.5 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
8.6 End-of-program markers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
8.7 Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
8.7.1 General description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
8.7.2 Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
8.7.3 Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
8.7.4 Punctuators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
8.7.5 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
'ISO/IEC 2012 — All rights reserved iii

8.7.6 Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
8.7.6.1 General description . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
8.7.6.2 Numeric literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
8.7.6.3 String literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
8.7.6.3.1 General description . . . . . . . . . . . . . . . . . . . . . . . . 29
8.7.6.3.2 Single quoted strings . . . . . . . . . . . . . . . . . . . . . . . 29
8.7.6.3.3 Double quoted strings . . . . . . . . . . . . . . . . . . . . . . . 30
8.7.6.3.4 Quoted non-expanded literal strings . . . . . . . . . . . . . . . 33
8.7.6.3.5 Quoted expanded literal strings . . . . . . . . . . . . . . . . . 35
8.7.6.3.6 Here documents . . . . . . . . . . . . . . . . . . . . . . . . . . 36
8.7.6.3.7 External command execution . . . . . . . . . . . . . . . . . . . 38
8.7.6.4 Array literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
8.7.6.5 Regular expression literals . . . . . . . . . . . . . . . . . . . . . . . 42
8.7.6.6 Symbol literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
9 Scope of variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
9.1 General description. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
9.2 Scope of local variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
9.3 Scope of global variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
10 Program structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
10.1 Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
10.2 Compound statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
11 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
11.1 General description. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
11.2 Logical expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
11.2.1 General description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
11.2.2 Logical NOT expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
11.2.3 Logical AND expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
11.2.4 Logical OR expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
11.3 Method invocation expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
11.3.1 General description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
11.3.2 Method arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
11.3.3 Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
11.3.4 The super expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
11.3.5 The yield expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
11.4 Operator expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
11.4.1 General description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
11.4.2 Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
11.4.2.1 General description . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
11.4.2.2 Single assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
11.4.2.2.1 General description . . . . . . . . . . . . . . . . . . . . . . . . 66
11.4.2.2.2 Single variable assignments . . . . . . . . . . . . . . . . . . . . 67
11.4.2.2.3 Scoped constant assignments . . . . . . . . . . . . . . . . . . . 69
11.4.2.2.4 Single indexing assignments. . . . . . . . . . . . . . . . . . . . 69
11.4.2.2.5 Single method assignments . . . . . . . . . . . . . . . . . . . . 70
11.4.2.3 Abbreviated assignments . . . . . . . . . . . . . . . . . . . . . . . . 71
11.4.2.3.1 General description . . . . . . . . . . . . . . . . . . . . . . . . 71
11.4.2.3.2 Abbreviated variable assignments . . . . . . . . . . . . . . . . 71
11.4.2.3.3 Abbreviated indexing assignments . . . . . . . . . . . . . . . . 72
11.4.2.3.4 Abbreviated method assignments . . . . . . . . . . . . . . . . 73
11.4.2.4 Multiple assignments . . . . . . . . . . . . . . . . . . . . . . . . . . 74
iv'ISO/IEC 2012 — All rights reserved

11.4.2.5 Assignments with rescue modifiers . . . . . . . . . . . . . . . . . . 78
11.4.3 Unary operator expressions . . . . . . . . . . . . . . . . . . . . . . . . . . 78
11.4.3.1 General description . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
11.4.3.2 The defined? expression . . . . . . . . . . . . . . . . . . . . . . . . 79
11.4.4 Binary operator expressions . . . . . . . . . . . . . . . . . . . . . . . . . . 81
11.5 Primary expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
11.5.1 General description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
11.5.2 Control structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
11.5.2.1 General description . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
11.5.2.2 Conditional expressions . . . . . . . . . . . . . . . . . . . . . . . . . 85
11.5.2.2.1 General description . . . . . . . . . . . . . . . . . . . . . . . . 85
11.5.2.2.2 The if expression . . . . . . . . . . . . . . . . . . . . . . . . . 85
11.5.2.2.3 The unless expression . . . . . . . . . . . . . . . . . . . . . . 87
11.5.2.2.4 The case expression . . . . . . . . . . . . . . . . . . . . . . . . 87
11.5.2.2.5 Conditional operator expression . . . . . . . . . . . . . . . . . 88
11.5.2.3 Iteration expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
11.5.2.3.1 General description . . . . . . . . . . . . . . . . . . . . . . . . 89
11.5.2.3.2 The while expression . . . . . . . . . . . . . . . . . . . . . . . 90
11.5.2.3.3 The until expression . . . . . . . . . . . . . . . . . . . . . . . 91
11.5.2.3.4 The for expression . . . . . . . . . . . . . . . . . . . . . . . . 91
11.5.2.4 Jump expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
11.5.2.4.1 General description . . . . . . . . . . . . . . . . . . . . . . . . 92
11.5.2.4.2 The return expression . . . . . . . . . . . . . . . . . . . . . . 93
11.5.2.4.3 The break expression . . . . . . . . . . . . . . . . . . . . . . . 94
11.5.2.4.4 The next expression . . . . . . . . . . . . . . . . . . . . . . . . 95
11.5.2.4.5 The redo expression . . . . . . . . . . . . . . . . . . . . . . . . 96
11.5.2.4.6 The retry expression . . . . . . . . . . . . . . . . . . . . . . . 97
11.5.2.5 The begin expression . . . . . . . . . . . . . . . . . . . . . . . . . . 97
11.5.3 Grouping expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
11.5.4 Variable references . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
11.5.4.1 General description . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
11.5.4.2 Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
11.5.4.3 Scoped constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
11.5.4.4 Global variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
11.5.4.5 Class variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
11.5.4.6 Instance variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
11.5.4.7 Local variables or method invocations . . . . . . . . . . . . . . . . . 102
11.5.4.7.1 General description . . . . . . . . . . . . . . . . . . . . . . . . 102
11.5.4.7.2 Determination of the type of local variable identifiers . . . . . 103
11.5.4.7.3 Local variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
11.5.4.7.4 Method invocations . . . . . . . . . . . . . . . . . . . . . . . . 104
11.5.4.8 Pseudo variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
11.5.4.8.1 General description . . . . . . . . . . . . . . . . . . . . . . . . 104
11.5.4.8.2 The nil expression . . . . . . . . . . . . . . . . . . . . . . . . 104
11.5.4.8.3 The true expression and the false expression . . . . . . . . . 104
11.5.4.8.4 The self expression . . . . . . . . . . . . . . . . . . . . . . . . 105
11.5.5 Object constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
11.5.5.1 Array constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
11.5.5.2 Hash constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
11.5.5.3 Range expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
12 Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
12.1 General description. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
'ISO/IEC 2012 — All rights reserved v

12.2 Expression statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
12.3 The if modifier statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
12.4 The unless modifier statement . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
12.5 The while modifier statement . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
12.6 The until modifier statement . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
12.7 The rescue modifier statement . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
13 Classes and modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
13.1 Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
...

Questions, Comments and Discussion

Ask us and Technical Secretary will try to provide an answer. You can facilitate discussion about the standard in here.