Download - Draft Ruby Spec 12 2009
-
8/4/2019 Draft Ruby Spec 12 2009
1/340
Programming Languages RubyIPA Ruby Standardization WG Draft
December 1, 2009
Information-technology Promotion Agency, Japan 2009
-
8/4/2019 Draft Ruby Spec 12 2009
2/340
ii
-
8/4/2019 Draft Ruby Spec 12 2009
3/340
-
8/4/2019 Draft Ruby Spec 12 2009
4/340
11.1 Logical expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3511.2 Method invocation expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
11.2.1 Method arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4211.2.2 Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4411.2.3 The super expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4711.2.4 The yield expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
11.3 Operator expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5111.3.1 Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
11.3.1.1 Single assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5211.3.1.1.1 Single variable assignments . . . . . . . . . . . . . . . . . . . . 5211.3.1.1.2 Single indexing assignments . . . . . . . . . . . . . . . . . . . . 5511.3.1.1.3 Single method assignments . . . . . . . . . . . . . . . . . . . . 55
11.3.1.2 Abbreviated assignments . . . . . . . . . . . . . . . . . . . . . . . . 5611.3.1.2.1 Abbreviated variable assignments . . . . . . . . . . . . . . . . 5611.3.1.2.2 Abbreviated indexing assignments . . . . . . . . . . . . . . . . 5711.3.1.2.3 Abbreviated method assignments . . . . . . . . . . . . . . . . 58
11.3.1.3 Multiple assignments . . . . . . . . . . . . . . . . . . . . . . . . . . 59
11.3.1.4 Assignments with rescue modiers . . . . . . . . . . . . . . . . . . 6211.3.2 Unary operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
11.3.2.1 The defined? expression . . . . . . . . . . . . . . . . . . . . . . . . 6411.3.3 Binary operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
11.4 Primary expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6711.4.1 Control structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
11.4.1.1 Conditional expressions . . . . . . . . . . . . . . . . . . . . . . . . . 6811.4.1.1.1 The if expression . . . . . . . . . . . . . . . . . . . . . . . . . 6811.4.1.1.2 The unless expression . . . . . . . . . . . . . . . . . . . . . . 6911.4.1.1.3 The case expression . . . . . . . . . . . . . . . . . . . . . . . . 7011.4.1.1.4 Conditional operator . . . . . . . . . . . . . . . . . . . . . . . 71
11.4.1.2 Iteration expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . 7111.4.1.2.1 The while expression . . . . . . . . . . . . . . . . . . . . . . . 7211.4.1.2.2 The until expression . . . . . . . . . . . . . . . . . . . . . . . 7211.4.1.2.3 The for expression . . . . . . . . . . . . . . . . . . . . . . . . 73
11.4.1.3 Jump expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7411.4.1.3.1 The return expression . . . . . . . . . . . . . . . . . . . . . . 7411.4.1.3.2 The break expression . . . . . . . . . . . . . . . . . . . . . . . 7611.4.1.3.3 The next expression . . . . . . . . . . . . . . . . . . . . . . . . 7611.4.1.3.4 The redo expression . . . . . . . . . . . . . . . . . . . . . . . . 7711.4.1.3.5 The retry expression . . . . . . . . . . . . . . . . . . . . . . . 78
11.4.1.4 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
11.4.1.4.1 The rescue expression . . . . . . . . . . . . . . . . . . . . . . 7811.4.2 Grouping expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8011.4.3 Variable references . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
11.4.3.1 Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8111.4.3.2 Scoped constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8211.4.3.3 Global variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8211.4.3.4 Class variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8211.4.3.5 Instance variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8311.4.3.6 Local variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8311.4.3.7 Pseudo variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
11.4.3.7.1 nil . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
11.4.3.7.2 true and false . . . . . . . . . . . . . . . . . . . . . . . . . . 8411.4.3.7.3 self . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
iv
-
8/4/2019 Draft Ruby Spec 12 2009
5/340
11.4.4 Object constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8511.4.4.1 Array constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8511.4.4.2 Hash constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8511.4.4.3 Range constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
11.4.5 Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
12 Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8712.1 The expression statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8712.2 The if modier statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8812.3 The unless modier statement . . . . . . . . . . . . . . . . . . . . . . . . . . . 8812.4 The while modier statement . . . . . . . . . . . . . . . . . . . . . . . . . . . 8912.5 The until modier statement . . . . . . . . . . . . . . . . . . . . . . . . . . . 8912.6 The rescue modier statement . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
13 Classes and modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9013.1 Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
13.1.1 General description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
13.1.2 Module denition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9113.1.3 Module inclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9213.2 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
13.2.1 General description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9213.2.2 Class denition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9313.2.3 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9413.2.4 Instance creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
13.3 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9513.3.1 Method denition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9513.3.2 Method parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9613.3.3 Method invocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
13.3.4 Method lookup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10013.3.5 Method visibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10113.3.5.1 Public methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10113.3.5.2 Private methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10113.3.5.3 Protected methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10113.3.5.4 Visibility change . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
13.3.6 The alias statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10213.3.7 The undef statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
13.4 Eigenclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10413.4.1 General description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10413.4.2 Eigenclass denition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
13.4.3 Singleton method denition . . . . . . . . . . . . . . . . . . . . . . . . . . 10514 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
14.1 Cause of exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10614.2 Exception handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
15 Built-in classes and modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10815.1 General description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10815.2 Built-in classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
15.2.1 Ob ject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10915.2.1.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10915.2.1.2 Included modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10915.2.1.3 Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10915.2.1.4 Instance methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
v
-
8/4/2019 Draft Ruby Spec 12 2009
6/340
15.2.1.4.1 Object#initialize . . . . . . . . . . . . . . . . . . . . . . . . . . 11015.2.2 Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
15.2.2.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11015.2.2.2 Singleton methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
15.2.2.2.1 Module.constants . . . . . . . . . . . . . . . . . . . . . . . . . 11015.2.2.2.2 Module.nesting . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
15.2.2.3 Instance methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11115.2.2.3.1 Module# < . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11115.2.2.3.2 Module# < = . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11115.2.2.3.3 Module# < = > . . . . . . . . . . . . . . . . . . . . . . . . . . . 11215.2.2.3.4 Module#== . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11215.2.2.3.5 Module#=== . . . . . . . . . . . . . . . . . . . . . . . . . . . 11215.2.2.3.6 Module# > . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11215.2.2.3.7 Module# > = . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11315.2.2.3.8 Module#alias method . . . . . . . . . . . . . . . . . . . . . . . 11315.2.2.3.9 Module#ancestors . . . . . . . . . . . . . . . . . . . . . . . . . 11315.2.2.3.10 Module#append features . . . . . . . . . . . . . . . . . . . . . 114
15.2.2.3.11 Module#attr . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11415.2.2.3.12 Module#attr accessor . . . . . . . . . . . . . . . . . . . . . . . 11515.2.2.3.13 Module#attr reader . . . . . . . . . . . . . . . . . . . . . . . . 11515.2.2.3.14 Module#attr writer . . . . . . . . . . . . . . . . . . . . . . . . 11615.2.2.3.15 Module#class eval . . . . . . . . . . . . . . . . . . . . . . . . . 11615.2.2.3.16 Module#class variable dened? . . . . . . . . . . . . . . . . . 11715.2.2.3.17 Module#class variable get . . . . . . . . . . . . . . . . . . . . 11815.2.2.3.18 Module#class variable set . . . . . . . . . . . . . . . . . . . . 11815.2.2.3.19 Module#class variables . . . . . . . . . . . . . . . . . . . . . . 11815.2.2.3.20 Module#const dened? . . . . . . . . . . . . . . . . . . . . . . 11915.2.2.3.21 Module#const get . . . . . . . . . . . . . . . . . . . . . . . . . 119
15.2.2.3.22 Module#const missing . . . . . . . . . . . . . . . . . . . . . . 11915.2.2.3.23 Module#const set . . . . . . . . . . . . . . . . . . . . . . . . . 12015.2.2.3.24 Module#constants . . . . . . . . . . . . . . . . . . . . . . . . . 12015.2.2.3.25 Module#extend object . . . . . . . . . . . . . . . . . . . . . . 12015.2.2.3.26 Module#extended . . . . . . . . . . . . . . . . . . . . . . . . . 12115.2.2.3.27 Module#include . . . . . . . . . . . . . . . . . . . . . . . . . . 12115.2.2.3.28 Module#include? . . . . . . . . . . . . . . . . . . . . . . . . . 12115.2.2.3.29 Module#included . . . . . . . . . . . . . . . . . . . . . . . . . 12215.2.2.3.30 Module#included modules . . . . . . . . . . . . . . . . . . . . 12215.2.2.3.31 Module#initialize . . . . . . . . . . . . . . . . . . . . . . . . . 12215.2.2.3.32 Module#initialize copy . . . . . . . . . . . . . . . . . . . . . . 123
15.2.2.3.33 Module#instance methods . . . . . . . . . . . . . . . . . . . . 12315.2.2.3.34 Module#method dened? . . . . . . . . . . . . . . . . . . . . . 12415.2.2.3.35 Module#module eval . . . . . . . . . . . . . . . . . . . . . . . 12515.2.2.3.36 Module#private . . . . . . . . . . . . . . . . . . . . . . . . . . 12515.2.2.3.37 Module#protected . . . . . . . . . . . . . . . . . . . . . . . . . 12515.2.2.3.38 Module#public . . . . . . . . . . . . . . . . . . . . . . . . . . 12515.2.2.3.39 Module#remove class variable . . . . . . . . . . . . . . . . . . 12615.2.2.3.40 Module#remove const . . . . . . . . . . . . . . . . . . . . . . . 12615.2.2.3.41 Module#remove method . . . . . . . . . . . . . . . . . . . . . 12715.2.2.3.42 Module#undef method . . . . . . . . . . . . . . . . . . . . . . 127
15.2.3 Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
15.2.3.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12815.2.3.2 Instance methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
vi
-
8/4/2019 Draft Ruby Spec 12 2009
7/340
15.2.3.2.1 Class#initialize . . . . . . . . . . . . . . . . . . . . . . . . . . 12815.2.3.2.2 Class#initialize copy . . . . . . . . . . . . . . . . . . . . . . . 12915.2.3.2.3 Class#new . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12915.2.3.2.4 Class#superclass . . . . . . . . . . . . . . . . . . . . . . . . . . 129
15.2.4 NilClass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13015.2.4.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
15.2.4.2 Instance methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13015.2.4.2.1 NilClass# & . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13015.2.4.2.2 NilClass# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13015.2.4.2.3 NilClass#nil? . . . . . . . . . . . . . . . . . . . . . . . . . . . 13015.2.4.2.4 NilClass# | . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
15.2.5 TrueClass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13115.2.5.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13115.2.5.2 Instance methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
15.2.5.2.1 TrueClass# & . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13115.2.5.2.2 TrueClass# . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13115.2.5.2.3 TrueClass#to s . . . . . . . . . . . . . . . . . . . . . . . . . . 132
15.2.5.2.4 TrueClass# | . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13215.2.6 FalseClass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
15.2.6.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13215.2.6.2 Instance methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
15.2.6.2.1 FalseClass# & . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13215.2.6.2.2 FalseClass# . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13215.2.6.2.3 FalseClass#to s . . . . . . . . . . . . . . . . . . . . . . . . . . 13315.2.6.2.4 FalseClass# | . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
15.2.7 Numeric . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13315.2.7.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13315.2.7.2 Included modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
15.2.7.3 Instance methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13315.2.7.3.1 Numeric#+@ . . . . . . . . . . . . . . . . . . . . . . . . . . . 13315.2.7.3.2 Numeric# @ . . . . . . . . . . . . . . . . . . . . . . . . . . . 13415.2.7.3.3 Numeric#abs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13415.2.7.3.4 Numeric#coerce . . . . . . . . . . . . . . . . . . . . . . . . . . 134
15.2.8 Integer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13515.2.8.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13615.2.8.2 Instance methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
15.2.8.2.1 Integer#+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13615.2.8.2.2 Integer# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13615.2.8.2.3 Integer#* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
15.2.8.2.4 Integer#/ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13815.2.8.2.5 Integer#% . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13815.2.8.2.6 Integer# < = > . . . . . . . . . . . . . . . . . . . . . . . . . . . 13915.2.8.2.7 Integer#== . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13915.2.8.2.8 Integer# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14015.2.8.2.9 Integer# &. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14015.2.8.2.10 Integer# | . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14015.2.8.2.11 Integer# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14115.2.8.2.12 Integer# > . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14115.2.8.2.14 Integer#ceil . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
15.2.8.2.15 Integer#downto . . . . . . . . . . . . . . . . . . . . . . . . . . 14215.2.8.2.16 Integer#eql? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
vii
-
8/4/2019 Draft Ruby Spec 12 2009
8/340
15.2.8.2.17 Integer#oor . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14315.2.8.2.18 Integer#hash . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14315.2.8.2.19 Integer#next . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14315.2.8.2.20 Integer#round . . . . . . . . . . . . . . . . . . . . . . . . . . . 14315.2.8.2.21 Integer#succ . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14315.2.8.2.22 Integer#times . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
15.2.8.2.23 Integer#to f . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14415.2.8.2.24 Integer#to i . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14415.2.8.2.25 Integer#truncate . . . . . . . . . . . . . . . . . . . . . . . . . 14415.2.8.2.26 Integer#upto . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
15.2.9 Float . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14515.2.9.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14515.2.9.2 Instance methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
15.2.9.2.1 Float#+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14615.2.9.2.2 Float# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14615.2.9.2.3 Float#* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14715.2.9.2.4 Float#/ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
15.2.9.2.5 Float#% . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14815.2.9.2.6 Float# < = > . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14915.2.9.2.7 Float#== . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14915.2.9.2.8 Float#ceil . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15015.2.9.2.9 Float#nite? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15015.2.9.2.10 Float#oor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15015.2.9.2.11 Float#innite? . . . . . . . . . . . . . . . . . . . . . . . . . . . 15115.2.9.2.12 Float#round . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15115.2.9.2.13 Float#to f . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15115.2.9.2.14 Float#to i . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15115.2.9.2.15 Float#truncate . . . . . . . . . . . . . . . . . . . . . . . . . . 152
15.2.10 String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15215.2.10.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15215.2.10.2 Included modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15215.2.10.3 Instance methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
15.2.10.3.1 String#* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15215.2.10.3.2 String#+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15315.2.10.3.3 String# < = > . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15315.2.10.3.4 String#== . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15415.2.10.3.5 String#= . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15415.2.10.3.6 String#[] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15515.2.10.3.7 String#capitalize . . . . . . . . . . . . . . . . . . . . . . . . . 156
15.2.10.3.8 String#capitalize! . . . . . . . . . . . . . . . . . . . . . . . . . 15615.2.10.3.9 String#chomp . . . . . . . . . . . . . . . . . . . . . . . . . . . 15715.2.10.3.10 String#chomp! . . . . . . . . . . . . . . . . . . . . . . . . . . . 15715.2.10.3.11 String#chop . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15715.2.10.3.12 String#chop! . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15815.2.10.3.13 String#downcase . . . . . . . . . . . . . . . . . . . . . . . . . 15815.2.10.3.14 String#downcase! . . . . . . . . . . . . . . . . . . . . . . . . . 15815.2.10.3.15 String#each line . . . . . . . . . . . . . . . . . . . . . . . . . . 15915.2.10.3.16 String#empty? . . . . . . . . . . . . . . . . . . . . . . . . . . . 15915.2.10.3.17 String#eql? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15915.2.10.3.18 String#gsub . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
15.2.10.3.19 String#gsub! . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16115.2.10.3.20 String#hash . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
viii
-
8/4/2019 Draft Ruby Spec 12 2009
9/340
15.2.10.3.21 String#include? . . . . . . . . . . . . . . . . . . . . . . . . . . 16215.2.10.3.22 String#initialize . . . . . . . . . . . . . . . . . . . . . . . . . . 16215.2.10.3.23 String#initialize copy . . . . . . . . . . . . . . . . . . . . . . . 16215.2.10.3.24 String#intern . . . . . . . . . . . . . . . . . . . . . . . . . . . 16315.2.10.3.25 String#length . . . . . . . . . . . . . . . . . . . . . . . . . . . 16315.2.10.3.26 String#match . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
15.2.10.3.27 String#replace . . . . . . . . . . . . . . . . . . . . . . . . . . . 16415.2.10.3.28 String#reverse . . . . . . . . . . . . . . . . . . . . . . . . . . . 16415.2.10.3.29 String#reverse! . . . . . . . . . . . . . . . . . . . . . . . . . . 16415.2.10.3.30 String#scan . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16415.2.10.3.31 String#size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16515.2.10.3.32 String#slice . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16515.2.10.3.33 String#split . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16615.2.10.3.34 String#sub . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16715.2.10.3.35 String#sub! . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16815.2.10.3.36 String#upcase . . . . . . . . . . . . . . . . . . . . . . . . . . . 16815.2.10.3.37 String#upcase! . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
15.2.10.3.38 String#to i . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16815.2.10.3.39 String#to f . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16915.2.10.3.40 String#to s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17015.2.10.3.41 String#to sym . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
15.2.11 Symbol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17015.2.11.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17015.2.11.2 Instance methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
15.2.11.2.1 Symbol#=== . . . . . . . . . . . . . . . . . . . . . . . . . . . 17015.2.11.2.2 Symbol#id2name . . . . . . . . . . . . . . . . . . . . . . . . . 17115.2.11.2.3 Symbol#to s . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17115.2.11.2.4 Symbol#to sym . . . . . . . . . . . . . . . . . . . . . . . . . . 171
15.2.12 Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17115.2.12.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17215.2.12.2 Included modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17215.2.12.3 Singleton methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
15.2.12.3.1 Array.[] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17215.2.12.4 Instance methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
15.2.12.4.1 Array#* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17215.2.12.4.2 Array#+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17315.2.12.4.3 Array#
-
8/4/2019 Draft Ruby Spec 12 2009
10/340
15.2.12.4.19 Array#pop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18015.2.12.4.20 Array#push . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18015.2.12.4.21 Array#replace . . . . . . . . . . . . . . . . . . . . . . . . . . . 18015.2.12.4.22 Array#reverse . . . . . . . . . . . . . . . . . . . . . . . . . . . 18015.2.12.4.23 Array#reverse! . . . . . . . . . . . . . . . . . . . . . . . . . . . 18115.2.12.4.24 Array#shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
15.2.12.4.25 Array#size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18115.2.12.4.26 Array#slice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18115.2.12.4.27 Array#unshift . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
15.2.13 Hash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18215.2.13.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18315.2.13.2 Included modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18315.2.13.3 Instance methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
15.2.13.3.1 Hash#== . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18315.2.13.3.2 Hash#[] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18315.2.13.3.3 Hash#[]= . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18415.2.13.3.4 Hash#clear . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
15.2.13.3.5 Hash#default . . . . . . . . . . . . . . . . . . . . . . . . . . . 18415.2.13.3.6 Hash#default= . . . . . . . . . . . . . . . . . . . . . . . . . . 18515.2.13.3.7 Hash#default proc . . . . . . . . . . . . . . . . . . . . . . . . 18515.2.13.3.8 Hash#delete . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18615.2.13.3.9 Hash#each . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18615.2.13.3.10 Hash#each key . . . . . . . . . . . . . . . . . . . . . . . . . . 18615.2.13.3.11 Hash#each value . . . . . . . . . . . . . . . . . . . . . . . . . 18715.2.13.3.12 Hash#empty? . . . . . . . . . . . . . . . . . . . . . . . . . . . 18715.2.13.3.13 Hash#has key? . . . . . . . . . . . . . . . . . . . . . . . . . . 18715.2.13.3.14 Hash#has value? . . . . . . . . . . . . . . . . . . . . . . . . . 18715.2.13.3.15 Hash#include? . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
15.2.13.3.16 Hash#initialize . . . . . . . . . . . . . . . . . . . . . . . . . . . 18815.2.13.3.17 Hash#initialize copy . . . . . . . . . . . . . . . . . . . . . . . 18815.2.13.3.18 Hash#key? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18915.2.13.3.19 Hash#keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18915.2.13.3.20 Hash#length . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18915.2.13.3.21 Hash#member? . . . . . . . . . . . . . . . . . . . . . . . . . . 19015.2.13.3.22 Hash#merge . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19015.2.13.3.23 Hash#replace . . . . . . . . . . . . . . . . . . . . . . . . . . . 19015.2.13.3.24 Hash#shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19015.2.13.3.25 Hash#size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19115.2.13.3.26 Hash#store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
15.2.13.3.27 Hash#value? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19115.2.13.3.28 Hash#values . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
15.2.14 Range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19215.2.14.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19215.2.14.2 Included modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19215.2.14.3 Instance methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
15.2.14.3.1 Range#== . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19215.2.14.3.2 Range#=== . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19315.2.14.3.3 Range#begin . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19315.2.14.3.4 Range#each . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19415.2.14.3.5 Range#end . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
15.2.14.3.6 Range#exclude end? . . . . . . . . . . . . . . . . . . . . . . . 19415.2.14.3.7 Range#rst . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
x
-
8/4/2019 Draft Ruby Spec 12 2009
11/340
15.2.14.3.8 Range#include? . . . . . . . . . . . . . . . . . . . . . . . . . . 19515.2.14.3.9 Range#initialize . . . . . . . . . . . . . . . . . . . . . . . . . . 19515.2.14.3.10 Range#last . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19515.2.14.3.11 Range#member? . . . . . . . . . . . . . . . . . . . . . . . . . 196
15.2.15 Regexp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19615.2.15.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
15.2.15.2 Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19615.2.15.3 Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19715.2.15.4 Matching process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20015.2.15.5 Singleton methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
15.2.15.5.1 Regexp.compile . . . . . . . . . . . . . . . . . . . . . . . . . . 20115.2.15.5.2 Regexp.escape . . . . . . . . . . . . . . . . . . . . . . . . . . . 20215.2.15.5.3 Regexp.last match . . . . . . . . . . . . . . . . . . . . . . . . . 20215.2.15.5.4 Regexp.quote . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
15.2.15.6 Instance methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20315.2.15.6.1 Regexp#initialize . . . . . . . . . . . . . . . . . . . . . . . . . 20315.2.15.6.2 Regexp#initialize copy . . . . . . . . . . . . . . . . . . . . . . 204
15.2.15.6.3 Regexp#== . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20415.2.15.6.4 Regexp#=== . . . . . . . . . . . . . . . . . . . . . . . . . . . 20515.2.15.6.5 Regexp#= . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20515.2.15.6.6 Regexp#casefold? . . . . . . . . . . . . . . . . . . . . . . . . . 20615.2.15.6.7 Regexp#match . . . . . . . . . . . . . . . . . . . . . . . . . . . 20615.2.15.6.8 Regexp#source . . . . . . . . . . . . . . . . . . . . . . . . . . 206
15.2.16 MatchData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20615.2.16.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20715.2.16.2 Instance methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
15.2.16.2.1 MatchData#[] . . . . . . . . . . . . . . . . . . . . . . . . . . . 20715.2.16.2.2 MatchData#begin . . . . . . . . . . . . . . . . . . . . . . . . . 207
15.2.16.2.3 MatchData#captures . . . . . . . . . . . . . . . . . . . . . . . 20715.2.16.2.4 MatchData#end . . . . . . . . . . . . . . . . . . . . . . . . . . 20815.2.16.2.5 MatchData#initialize copy . . . . . . . . . . . . . . . . . . . . 20815.2.16.2.6 MatchData#length . . . . . . . . . . . . . . . . . . . . . . . . 20915.2.16.2.7 MatchData#offset . . . . . . . . . . . . . . . . . . . . . . . . . 20915.2.16.2.8 MatchData#post match . . . . . . . . . . . . . . . . . . . . . 20915.2.16.2.9 MatchData#pre match . . . . . . . . . . . . . . . . . . . . . . 21015.2.16.2.10 MatchData#size . . . . . . . . . . . . . . . . . . . . . . . . . . 21015.2.16.2.11 MatchData#string . . . . . . . . . . . . . . . . . . . . . . . . . 21015.2.16.2.12 MatchData#to a . . . . . . . . . . . . . . . . . . . . . . . . . . 21015.2.16.2.13 MatchData#to s . . . . . . . . . . . . . . . . . . . . . . . . . . 210
15.2.17 Proc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21115.2.17.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21115.2.17.2 Singleton methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
15.2.17.2.1 Proc.new . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21115.2.17.3 Instance methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
15.2.17.3.1 Proc#[] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21115.2.17.3.2 Proc#arity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21215.2.17.3.3 Proc#call . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21215.2.17.3.4 Proc#clone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21315.2.17.3.5 Proc#dup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
15.2.18 Struct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
15.2.18.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21415.2.18.2 Singleton methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
xi
-
8/4/2019 Draft Ruby Spec 12 2009
12/340
-
8/4/2019 Draft Ruby Spec 12 2009
13/340
15.2.19.6.24 Time#usec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23315.2.19.6.25 Time#utc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23415.2.19.6.26 Time#utc? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23415.2.19.6.27 Time#utc offset . . . . . . . . . . . . . . . . . . . . . . . . . . 23415.2.19.6.28 Time#wday . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23415.2.19.6.29 Time#yday . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
15.2.19.6.30 Time#year . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23515.2.19.6.31 Time#zone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23515.2.19.6.32 Time#initialize . . . . . . . . . . . . . . . . . . . . . . . . . . 23615.2.19.6.33 Time#initialize copy . . . . . . . . . . . . . . . . . . . . . . . 236
15.2.20 IO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23615.2.20.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23715.2.20.2 Included modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23715.2.20.3 Singleton methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
15.2.20.3.1 IO.open . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23715.2.20.4 Instance methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
15.2.20.4.1 IO#close . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
15.2.20.4.2 IO#closed? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23815.2.20.4.3 IO#each . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23815.2.20.4.4 IO#each byte . . . . . . . . . . . . . . . . . . . . . . . . . . . 23915.2.20.4.5 IO#each line . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23915.2.20.4.6 IO#eof? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23915.2.20.4.7 IO#ush . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24015.2.20.4.8 IO#getc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24015.2.20.4.9 IO#gets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24015.2.20.4.10 IO#initialize copy . . . . . . . . . . . . . . . . . . . . . . . . . 24115.2.20.4.11 IO#print . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24115.2.20.4.12 IO#putc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
15.2.20.4.13 IO#puts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24215.2.20.4.14 IO#read . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24315.2.20.4.15 IO#readchar . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24315.2.20.4.16 IO#readline . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24315.2.20.4.17 IO#readlines . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24415.2.20.4.18 IO#sync . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24415.2.20.4.19 IO#sync= . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24515.2.20.4.20 IO#write . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
15.2.21 File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24515.2.21.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24615.2.21.2 Singleton methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
15.2.21.2.1 File.exist? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24615.2.21.3 Instance methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
15.2.21.3.1 File#initialize . . . . . . . . . . . . . . . . . . . . . . . . . . . 24615.2.21.3.2 File#path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
15.2.22 Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24715.2.22.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24715.2.22.2 Built-in exception classes . . . . . . . . . . . . . . . . . . . . . . . . 24715.2.22.3 Singleton methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
15.2.22.3.1 Exception.exception . . . . . . . . . . . . . . . . . . . . . . . . 24715.2.22.4 Instance methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
15.2.22.4.1 Exception#exception . . . . . . . . . . . . . . . . . . . . . . . 247
15.2.22.4.2 Exception#message . . . . . . . . . . . . . . . . . . . . . . . . 24915.2.22.4.3 Exception#to s . . . . . . . . . . . . . . . . . . . . . . . . . . 249
xiii
-
8/4/2019 Draft Ruby Spec 12 2009
14/340
15.2.22.4.4 Exception#initialize . . . . . . . . . . . . . . . . . . . . . . . . 24915.2.23 StandardError . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
15.2.23.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24915.2.24 ArgumentError . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
15.2.24.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25015.2.25 LocalJumpError . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
15.2.25.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25015.2.25.2 Instance methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
15.2.25.2.1 LocalJumpError#exit value . . . . . . . . . . . . . . . . . . . 25015.2.25.2.2 LocalJumpError#reason . . . . . . . . . . . . . . . . . . . . . 250
15.2.26 RangeError . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25015.2.26.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
15.2.27 RegexpError . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25015.2.27.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
15.2.28 RuntimeError . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25115.2.28.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
15.2.29 TypeError . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
15.2.29.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25115.2.30 ZeroDivisionError . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
15.2.30.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25115.2.31 NameError . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
15.2.31.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25115.2.31.2 Instance methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
15.2.31.2.1 NameError#name . . . . . . . . . . . . . . . . . . . . . . . . . 25115.2.31.2.2 NameError#initialize . . . . . . . . . . . . . . . . . . . . . . . 252
15.2.32 NoMethodError . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25215.2.32.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25215.2.32.2 Instance methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
15.2.32.2.1 NoMethodError#args . . . . . . . . . . . . . . . . . . . . . . . 25215.2.32.2.2 NoMethodError#initialize . . . . . . . . . . . . . . . . . . . . 252
15.2.33 IndexError . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25315.2.33.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
15.2.34 StopIteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25315.2.34.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
15.2.35 IOError . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25315.2.35.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
15.2.36 EOFError . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25315.2.36.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
15.2.37 SystemCallError . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
15.2.37.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25315.2.38 ScriptError . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
15.2.38.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25415.2.39 SyntaxError . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
15.2.39.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25415.2.40 LoadError . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
15.2.40.1 Direct superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25415.3 Built-in modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
15.3.1 Kernel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25415.3.1.1 Singleton methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
15.3.1.1.1 Kernel. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
15.3.1.1.2 Kernel.block given? . . . . . . . . . . . . . . . . . . . . . . . . 25515.3.1.1.3 Kernel.eval . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
xiv
-
8/4/2019 Draft Ruby Spec 12 2009
15/340
15.3.1.1.4 Kernel.global variables . . . . . . . . . . . . . . . . . . . . . . 25515.3.1.1.5 Kernel.iterator? . . . . . . . . . . . . . . . . . . . . . . . . . . 25515.3.1.1.6 Kernel.lambda . . . . . . . . . . . . . . . . . . . . . . . . . . . 25615.3.1.1.7 Kernel.local variables . . . . . . . . . . . . . . . . . . . . . . . 25715.3.1.1.8 Kernel.loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25715.3.1.1.9 Kernel.method missing . . . . . . . . . . . . . . . . . . . . . . 257
15.3.1.1.10 Kernel.p . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25815.3.1.1.11 Kernel.print . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25815.3.1.1.12 Kernel.puts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25815.3.1.1.13 Kernel.raise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25915.3.1.1.14 Kernel.require . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
15.3.1.2 Instance methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26015.3.1.2.1 Kernel#== . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26015.3.1.2.2 Kernel#=== . . . . . . . . . . . . . . . . . . . . . . . . . . . 26015.3.1.2.3 Kernel# id . . . . . . . . . . . . . . . . . . . . . . . . . . . 26115.3.1.2.4 Kernel# send . . . . . . . . . . . . . . . . . . . . . . . . . . 26115.3.1.2.5 Kernel# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
15.3.1.2.6 Kernel#block given? . . . . . . . . . . . . . . . . . . . . . . . 26115.3.1.2.7 Kernel#class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26215.3.1.2.8 Kernel#clone . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26215.3.1.2.9 Kernel#dup . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26215.3.1.2.10 Kernel#eql? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26315.3.1.2.11 Kernel#equal? . . . . . . . . . . . . . . . . . . . . . . . . . . . 26315.3.1.2.12 Kernel#eval . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26315.3.1.2.13 Kernel#extend . . . . . . . . . . . . . . . . . . . . . . . . . . . 26315.3.1.2.14 Kernel#global variables . . . . . . . . . . . . . . . . . . . . . . 26415.3.1.2.15 Kernel#hash . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26415.3.1.2.16 Kernel#initialize copy . . . . . . . . . . . . . . . . . . . . . . . 264
15.3.1.2.17 Kernel#inspect . . . . . . . . . . . . . . . . . . . . . . . . . . 26515.3.1.2.18 Kernel#instance eval . . . . . . . . . . . . . . . . . . . . . . . 26515.3.1.2.19 Kernel#instance of? . . . . . . . . . . . . . . . . . . . . . . . . 26515.3.1.2.20 Kernel#instance variable dened? . . . . . . . . . . . . . . . . 26615.3.1.2.21 Kernel#instance variable get . . . . . . . . . . . . . . . . . . . 26615.3.1.2.22 Kernel#instance variable set . . . . . . . . . . . . . . . . . . . 26715.3.1.2.23 Kernel#instance variables . . . . . . . . . . . . . . . . . . . . 26715.3.1.2.24 Kernel#is a? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26715.3.1.2.25 Kernel#iterator? . . . . . . . . . . . . . . . . . . . . . . . . . . 26815.3.1.2.26 Kernel#kind of? . . . . . . . . . . . . . . . . . . . . . . . . . . 26815.3.1.2.27 Kernel#lambda . . . . . . . . . . . . . . . . . . . . . . . . . . 268
15.3.1.2.28 Kernel#local variables . . . . . . . . . . . . . . . . . . . . . . 26815.3.1.2.29 Kernel#loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26815.3.1.2.30 Kernel#method missing . . . . . . . . . . . . . . . . . . . . . . 26915.3.1.2.31 Kernel#methods . . . . . . . . . . . . . . . . . . . . . . . . . . 26915.3.1.2.32 Kernel#nil? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26915.3.1.2.33 Kernel#object id . . . . . . . . . . . . . . . . . . . . . . . . . 26915.3.1.2.34 Kernel#p . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27015.3.1.2.35 Kernel#print . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27015.3.1.2.36 Kernel#private methods . . . . . . . . . . . . . . . . . . . . . 27015.3.1.2.37 Kernel#protected methods . . . . . . . . . . . . . . . . . . . . 27115.3.1.2.38 Kernel#public methods . . . . . . . . . . . . . . . . . . . . . . 271
15.3.1.2.39 Kernel#puts . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27115.3.1.2.40 Kernel#raise . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
xv
-
8/4/2019 Draft Ruby Spec 12 2009
16/340
15.3.1.2.41 Kernel#remove instance variable . . . . . . . . . . . . . . . . . 27215.3.1.2.42 Kernel#require . . . . . . . . . . . . . . . . . . . . . . . . . . . 27215.3.1.2.43 Kernel#respond to? . . . . . . . . . . . . . . . . . . . . . . . . 27215.3.1.2.44 Kernel#send . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27315.3.1.2.45 Kernel#singleton methods . . . . . . . . . . . . . . . . . . . . 27315.3.1.2.46 Kernel#to s . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
15.3.2 Enumerable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27415.3.2.1 Instance methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
15.3.2.1.1 Enumerable#all? . . . . . . . . . . . . . . . . . . . . . . . . . 27415.3.2.1.2 Enumerable#any? . . . . . . . . . . . . . . . . . . . . . . . . . 27515.3.2.1.3 Enumerable#collect . . . . . . . . . . . . . . . . . . . . . . . . 27515.3.2.1.4 Enumerable#detect . . . . . . . . . . . . . . . . . . . . . . . . 27515.3.2.1.5 Enumerable#each with index . . . . . . . . . . . . . . . . . . . 27615.3.2.1.6 Enumerable#entries . . . . . . . . . . . . . . . . . . . . . . . . 27615.3.2.1.7 Enumerable#nd . . . . . . . . . . . . . . . . . . . . . . . . . 27715.3.2.1.8 Enumerable#nd all . . . . . . . . . . . . . . . . . . . . . . . 27715.3.2.1.9 Enumerable#grep . . . . . . . . . . . . . . . . . . . . . . . . . 277
15.3.2.1.10 Enumerable#include? . . . . . . . . . . . . . . . . . . . . . . . 27815.3.2.1.11 Enumerable#inject . . . . . . . . . . . . . . . . . . . . . . . . 27815.3.2.1.12 Enumerable#map . . . . . . . . . . . . . . . . . . . . . . . . . 27915.3.2.1.13 Enumerable#max . . . . . . . . . . . . . . . . . . . . . . . . . 27915.3.2.1.14 Enumerable#min . . . . . . . . . . . . . . . . . . . . . . . . . 28015.3.2.1.15 Enumerable#member? . . . . . . . . . . . . . . . . . . . . . . 28015.3.2.1.16 Enumerable#partition . . . . . . . . . . . . . . . . . . . . . . 28015.3.2.1.17 Enumerable#reject . . . . . . . . . . . . . . . . . . . . . . . . 28115.3.2.1.18 Enumerable#select . . . . . . . . . . . . . . . . . . . . . . . . 28115.3.2.1.19 Enumerable#sort . . . . . . . . . . . . . . . . . . . . . . . . . 28215.3.2.1.20 Enumerable#to a . . . . . . . . . . . . . . . . . . . . . . . . . 282
15.3.3 Comparable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28315.3.3.1 Instance methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
15.3.3.1.1 Comparable# < . . . . . . . . . . . . . . . . . . . . . . . . . . 28315.3.3.1.2 Comparable# < = . . . . . . . . . . . . . . . . . . . . . . . . . 28315.3.3.1.3 Comparable#== . . . . . . . . . . . . . . . . . . . . . . . . . 28315.3.3.1.4 Comparable# > . . . . . . . . . . . . . . . . . . . . . . . . . . 28415.3.3.1.5 Comparable# > = . . . . . . . . . . . . . . . . . . . . . . . . . 28415.3.3.1.6 Comparable#between? . . . . . . . . . . . . . . . . . . . . . . 284
Annex A (informative) Grammar Summary . . . . . . . . . . . . . . . . . . . . . . . . . 286
xvi
-
8/4/2019 Draft Ruby Spec 12 2009
17/340
Information technology Programming Languages Ruby
1 Scope1
This document species the syntax and semantics of the computer programming language Ruby2by specifying requirements for a conforming processor and for a conforming program.3
This document does not specify:4
the size or complexity of a program text that exceeds the capacity of any specic data5processing system or the capacity of a particular processor;6
the minimal requirements of a data processing system that is capable of supporting a7conforming processor;8
the method for activating the execution of programs on a data processing system;9
the method for reporting syntactic and runtime errors.10
2 Normative references11
The following referenced documents are indispensable for the application of this document. For12dated references, only the edition cited applies. For undated references, the latest edition of the13referenced document (including any amendments) applies.14
ISO/IEC 646:1991 Information technology ISO 7-bit coded character set for information in-15terchange.16
IEC 60559:1989 Binary oating-point arithmetic for microprocessor systems.17
3 Conformance18
A conforming Ruby program shall:19
use only those features of the language specied in this document;20
not rely on implementation dependent features;21
A conforming Ruby processor shall:22
1
-
8/4/2019 Draft Ruby Spec 12 2009
18/340
accept any conforming programs and behave as specied in this document;1
reject any program which does not conform to the syntax described in this document;2
report any unhandled exceptions raised during execution of the conforming program;3
A conforming Ruby processor may use an internal model for the Ruby language other than the4one specied in this document, if it does not change the meaning of a conforming program.5
4 Terms and denitions6
For the purposes of this document, the following terms and denitions apply. Other terms are7dened where they appear in bold slant face or on the left side of a syntax rule.8
4.19block10sequence of statements which is passed to a method invocation11
4.212class13object which denes the behavior of a set of other objects called its instances14
NOTE The behavior is a set of methods which can be invoked on an instance.15
4.316class variable17
variable whose value is shared by all the instances of a class18
4.419constant20variable which is dened in a class or a module and is accessible outside the class or module21
NOTE The value of a constant is regularly expected to remain constant during the execution of a22program, but Ruby does not force it. In some implementations, an assignment to a constant which23already exists causes a warning, but this document does not specity it.24
4.525
eigenclass26
special class which denes a behavior for only a single object27
4.628exception29object which represents an unexpected event30
4.731global variable32variable which is accessible everywhere in a Ruby program33
4.834 implementation dened35possibly differing between implementations, but dened for every implementation36
2
-
8/4/2019 Draft Ruby Spec 12 2009
19/340
4.91implementation dependent2possibly differing between implementations, and not necessarily dened for any particular im-3plementation4
4.105
instance method6method which can be invoked on all the instances of a class7
4.118instance variable9variable which belongs to a single object10
4.1211local variable12variable which is accessible only in a certain scope introduced by a program construct such as13a method denition, a block, a class denition, a module denition, an eigenclass denition, or14
the toplevel of a program15
4.1316method17procedure which, when invoked on an object, performs a set of computations on the object18
4.1419method visibility20attribute of a method which determines the conditions on which a method invocation is allowed21
4.1522module23object which provides features to be included into a class or another module24
4.1625object26computational entity which has a state and a behavior27
4.1728singleton method29instance method of the eigenclass of an object30
4.1831variable32computational entity which stores a reference to an object33
5 Notational conventions34
5.1 Syntax35
The syntax of the language is presented as a series of productions. Each production consists36
of the name of the nonterminal symbol being dened followed by ::, followed by one or more37 alternatives separated by |.38
3
-
8/4/2019 Draft Ruby Spec 12 2009
20/340
Terminal symbols are shown in typewriter face , and represent sequences of characters as they1appear in a program text. Non-terminal symbols are shown in italic face .2
Each alternative in a production consists of a sequence of terminal and/or nonterminal symbols3separated by whitespace.4
If the same nonterminal symbol occurs on the right side of a production more than once, each5occurrence is subscripted with a number to distinguish it from the other occurrences of the same6name.7
An optional symbol is denoted by postxing the symbol with ?.8
A sequence of zero or more repetitions of a symbol is denoted by postxing the symbol with9*.10
A sequence of one or more repetitions of a symbol is denoted by postxing the symbol with +.11
Parentheses are used to treat a sequence of symbols as a single symbol.12
A symbol followed by the phrase but not and another symbol represents all sequences of charac-13ters represented by the rst symbol except for sequences of characters represented by the second14symbol.15
EXAMPLE 1 The following example means that non-escaped-character is any member of source-16character except escape-character :17
non-escaped-character ::18
source-character but not
escape-character 19
Text enclosed by [ and ] is used to describe a sequence of characters or a location in a20program text.21
EXAMPLE 2 The following example means that source-character is any character specied in ISO/IEC22646:23
source-character ::24
[ any character in ISO/IEC 646 ]25
In particular, the notation [lookahead / set ] indicates that the token immediately following26the notation shall not begin with a sequence of characters represented by one of the members27of set . The set is represented as a list of one or more terminal symbols separated by commas,28and the list is enclosed by { and }.29
EXAMPLE 3 The following example means that the argument following the method-modier shall not30begin with {:31
command ::32
4
-
8/4/2019 Draft Ruby Spec 12 2009
21/340
method-identier [ lookahead / { { } ] argument 12
In this document, use of the words of and in , when expressing a relationship between nonter-3minal symbols, has the following meanings:4
X of Y : refers to the X occurring directly in a production dening Y .5
X in Y : refers to any X occurring in a sequence which is derived directly or indirectly6from Y .7
5.2 Conceptual name8
A conceptual name is a common name given to a set of semantically related nonterminal9symbols in the grammar in order to refer to this set in a semantic description. A conceptual10name is dened by a conceptual name denition . A conceptual name denition consists11of the conceptual name to be dened followed by ::=, followed by one or more nonterminal12symbols or conceptual names, separated by |.13
EXAMPLE The following example denes the conceptual name assignment , which can be used to refer14either assignment-expression or assignment-statement .15
assignment ::=16assignment-expression 17
| assignment-statement 18
6 Objects19
6.1 General description20
Ruby is a pure ob ject-oriented language. It is pure in the sense that every value manipulated21in a Ruby program is an object including primitive values such as integers.22
An object is a computational entity which has a state and a behavior.23
A variable is a computational entity which stores a reference to an object. A variable has a24name. A variable is said to be bound to an object if the variable stores a reference to the25object. This association of a variable with an object is called a variable binding . When a26variable with name N is bound to an object O , N is called the name of the binding, and O is27called the value of the binding.28
An object has a set of variable bindings. A variable whose binding is in this set is an instance29variable of that object. This set of bindings of instance variables represents the state of that30object and is encapsulated in that object.31
A method is a procedure which, when invoked, performs a set of computations. The behavior32 of an object is dened by a set of methods which can be invoked on that object. A method33has one or more (when aliased) names associated with it. An association between a name and34
5
-
8/4/2019 Draft Ruby Spec 12 2009
22/340
-
8/4/2019 Draft Ruby Spec 12 2009
23/340
6.2 Boolean values1
An object is classied into either a true value or a false value .2
Only false and nil are false values. The pseudo variable false is the only instance of the class3FalseClass , and is represented by the keyword false . The pseudo variable nil is the only4
instance of the class NilClass , and represented by the keyword nil .5
Objects other than false and nil are classied into true values. The pseudo variable true is6the only instance of the class TrueClass , and represented by the keyword true .7
7 Execution context8
7.1 Contextual attributes9
An execution context is a set of attributes which affects an evaluation of a program.10
An execution context is not a part of the language. It is dened in this document only for the11description of the semantics of a program. A conforming processor shall evaluate a program as12if it acted upon an execution context in the manner described in this document.13
An execution context consists of a set of attributes as described below. Each attribute of an ex-14ecution context except [ global-variable-bindings ] forms a logical stack. The names of attributes15are enclosed in double square brackets [ and ] . Attributes of an execution context are16changed when a program construct is evaluated.17
The following are the attributes of an execution context:18
[ self ] : A logical stack of objects, the top of which is the object to which the pseudo variable19self is bound (see 11.4.3.7.3). The object at the top of the stack is called the current20self .21
[ class-module-list ] : A logical stack of lists of classes or modules. The class or module at22the head of the list which is on the top of the stack is called the current class or module .23
[ default-method-visibility ] : A logical stack of visibilities of methods, each of which is one24of the public , private , and protected visibility. The top of the stack is called the current25visibility .26
[ local-variable-bindings ] : A logical stack of sets of bindings of local variables. The element27at the top of the stack is called the current set of local variable bindings . A set of 28bindings is pushed onto the stack on every entry into a local variable scope (see 9.1.1),29and the top element is removed from the stack on every exit from the scope. The scope30with which an element in the stack is associated is called the scope of the set of local 31variable bindings .32
[ invoked-method-name ] : A logical stack of names by which methods are invoked.33
[ dened-method-name ] : A logical stack of names with which the invoked methods are34dened.35
7
-
8/4/2019 Draft Ruby Spec 12 2009
24/340
[ block ] : A logical stack of blocks passed to method invocations. An element of the stack1may be block-not-given , which indicates that no block is passed to a method invocation.2
[ global-variable-bindings ] : A set of bindings of global variables.3
The term unset is used to describe the state of an attribute which is set to nothing.4
7.2 The initial state5
Immediately prior to an execution of a program, the attributes of the execution context is6initialized as follows:7
a) Create an empty set of variable bindings, and set [ global-variable-bindings ] to the set of 8variable bindings.9
b) Create built-in classes and modules as described in 15.10
c) Create an empty stack for each attribute of the execution context except[
global-variable-11bindings ] .12
d) Create a direct instance of the class Object and push it onto [ self ] .13
e) Create a list containing only the class Object and push the list onto [ class-module-list ] .14
f) Push the private visibility onto [ default-visibility ] .15
g) Push block-not-given onto [ block ] .16
8 Lexical structure17
When several prexes of the input under parsing process have matching productions, the pro-18duction that matches the longest prex is selected.19
8.1 Source text20
Syntax21
source-character ::22[ any character in ISO/IEC 646 ]23
A program is represented as a sequence of characters. A conforming processor shall accept24any conforming program which consists of characters in ISO/IEC 646, encoded with the octet25values as specied in ISO/IEC 646. The support for any other character sets and encodings is26implementation dependent.27
Terminal symbols are sequences of those characters in ISO/IEC 646. Control characters in28
ISO/IEC 646 are represented by hexadecimal notation.29
EXAMPLE 0x0a represents a line feed character.30
8
-
8/4/2019 Draft Ruby Spec 12 2009
25/340
8.2 Line terminators1
Syntax2
line-terminator ::3
0x0d? 0x0a4
A line-terminator is ignored when it is used to separate token s. For this reason, except in 8.45and 8.5, line-terminator s are omitted from productions. However, in some cases, the presence6or absence of a line-terminator changes the meaning of a program.7
A location of program text where a line-terminator shall occur is indicated by the notation [8line-terminator here ]. A location of program text where a line-terminator shall not occur is9indicated by the notation [ no line-terminator here ]; however, a conforming processor may10ignore the notation where the ignorance does not introduce ambiguity.11
EXAMPLE statement s are separated by separator s (see 10.2). The syntax of the separator s is as12follows:13
separator ::14;15
| [ line-terminator here]16
The source17
x = 1 + 218puts x19
is therefore separated to two statements x = 1 + 2 and puts x by a line-terminator.20
The source21
x =22 1 + 223
is parsed as a single statement x = 1 + 2 because x = is not a valid statement . However, the source24
x25= 1 + 226
is not a valid Ruby program because a line-terminator shall not occur before = in a single-variable-27assignment-expression , and = 1 + 2 is not a valid statement . The fact that a line-terminator shall not28
occur before = is indicated in the syntax of the single-variable-assignment-expression as follows (see29
11.3.1.1.1):30
9
-
8/4/2019 Draft Ruby Spec 12 2009
26/340
single-variable-assignment-expression ::1variable [ no line-terminator here] = operator-expression 2
8.3 Whitespace3
Syntax4
whitespace ::50x09 | 0x0b | 0x0c | 0x0d | 0x20 | \ 0x0d? 0x0a6
7
whitespace is ignored when it is used to separate token s. For this reason, except in
8.4 and8 8.5, whitespace is omitted from productions. However, in some cases, the presence or absence9of whitespace changes the meaning of a program.10
A location of program text where whitespace shall occur is indicated by the notation [ whitespace11here ]. A location of program text where whitespace shall not occur is indicated by the notation12[ no whitespace here ]. A line-terminator shall not occur in the location where whitespace shall13not occur. Therefore, this notation also indicates that a line-terminator shall not occur.14
8.4 Comments15
Syntax16
comment ::17single-line-comment 18
| multi-line-comment 19
single-line-comment ::20# comment-content ?21
comment-content ::22line-content 23
line-content ::24source-character +25
multi-line-comment ::26multi-line-comment-begin-line multi-line-comment-line ?27
multi-line-comment-end-line28
multi-line-comment-begin-line ::29[ beginning of a line ] =begin rest-of-begin-end-line ? line-terminator 30
10
-
8/4/2019 Draft Ruby Spec 12 2009
27/340
multi-line-comment-end-line ::1[ beginning of a line ] =end rest-of-begin-end-line ?2
( line-terminator | [ end of a program ] )3
rest-of-begin-end-line ::4whitespace + comment-content 5
line ::6comment-content line-terminator 7
multi-line-comment-line ::8line but not multi-line-comment-end-line9
The notation [ beginning of a line ] indicates the beginning of a program or the position10immediately after a line-terminator .11
Any characters that are considered as line-terminator s are not allowed within a line-content .12
A comment is either a single-line-comment or a multi-line-comment . A comment is considered13to be whitespace .14
A single-line-comment begins with # and continues to the end of the line. A line-terminator 15at the end of the line is not considered to be a part of the comment. A single-line-comment can16contain any characters except line-terminator s.17
A multi-line-comment begins with a line beginning with =begin , and continues until and in-18cluding a line that begins with =end . Unlike single-line-comment s, a line-terminator on a19multi-line-comment-end-line , if any, is considered to be part of the comment.20
8.5 Tokens21
Syntax22
token ::23reserved-word 24
| identier 25| punctuator 26| operator 27| literal 28
8.5.1 Reserved words29
Syntax30
reserved-word ::31__LINE__ | __ENCODING__ | __FILE__ | BEGIN | END | alias | and | begin32
11
-
8/4/2019 Draft Ruby Spec 12 2009
28/340
| break | case | class | def | defined? | do | else | elsif | end1| ensure | for | false | if | in | module | next | nil | not | or | redo2| rescue | retry | return | self | super | then | true | undef | unless3| until | when | while | yield4
Reserved words are case-sensitive.5
8.5.2 Identiers6
Syntax7
identier ::8local-variable-identier 9
| global-variable-identier 10
| class-variable-identier 11
| instance-variable-identier 12| constant-identier 13| method-identier 14
local-variable-identier ::15( lowercase-character | _ ) identier-character *16
global-variable-identier ::17 identier-start-character identier-character *18
class-variable-identier ::19@@identier-start-character identier-character *20
instance-variable-identier ::21@identier-start-character identier-character *22
constant-identier ::23uppercase-character identier-character *24
method-identier ::25method-only-identier 26
| assignment-like-method-identier 27| constant-identier 28| local-variable-identier 29
method-only-identier ::30( constant-identier | local-variable-identier ) ( ! | ? )31
assignment-like-method-identier ::32( constant-identier | local-variable-identier ) =33
12
-
8/4/2019 Draft Ruby Spec 12 2009
29/340
identier-character ::1lowercase-character 2
| uppercase-character 3| decimal-digit 4| _5
identier-start-character ::6lowercase-character 7
| uppercase-character 8| _9
uppercase-character ::10A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R11
| S | T | U | V | W | X | Y | Z12
lowercase-character ::13
a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r14| s | t | u | v | w | x | y | z15
decimal-digit ::160 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 917
An identier is a sequence of identier-character s optionally prexed by one of , @@, or18@, and optionally postxed by one of ?, ! , or =.19
A global-variable-identier begins with . A class-variable-identier starts with