Full Text as of 2009-12-01
From Ruby Standard Wiki
How You can Help
This page has been started off with a raw dump of the standard to plain text. It needs to be marked up to be more readable.
Important Note: According to the terms under which the standard is distributed, this text may not be modified. I interpret that to mean that changes to the content may not be made. Changes for style and organizational purposes only, please.
Full Text of the Standard
Programming Languages — Ruby IPA Ruby Standardization WG Draft December 1, 2009
©Information-technology Promotion Agency, Japan 2009
ii
Contents
- 1 Scope
- 2 Normative references
- 3 Conformance
- 4 Terms and definitions
- 5 Notational conventions
- 6 Objects
- 6.1 General description
- 6.2 Boolean values
- 7 Execution context
- 7.1 Contextual attributes
- 7.2 The initial state
- 8 Lexical structure
- 8.1 Source text
- 8.2 Line terminators
- 8.3 Whitespace
- 8.4 Comments
- 8.5 Tokens
- 8.5.1 Reserved words
- 8.5.2 Identifiers
- 8.5.3 Punctuators
- 8.5.4 Operators
- 8.5.5 Literals
- 8.5.5.1 Numeric literals
- 8.5.5.2 String literals
- 8.5.5.2.1 Single quoted strings
- 8.5.5.2.2 Double quoted strings
- 8.5.5.2.3 Quoted non-expanded literal strings
- 8.5.5.2.4 Quoted expanded literal strings
- 8.5.5.2.5 Here documents
- 8.5.5.2.6 External command execution
- 8.5.5.3 Array literals
- 8.5.5.4 Regular expression literals
- 8.5.5.5 Symbol literals
- 9 Scope of variables
- 9.1 Local variables
- 9.1.1 Scopes of local variables
- 9.1.2 References to local variables
- 9.2 Global variables
- 10 Program structure
- 10.1 Program
- 10.2 Compound statement
- 11 Expressions
- 11.1 Logical expressions
- 11.2 Method invocation expressions
- 11.2.1 Method arguments
- 11.2.2 Blocks
- 11.2.3 The super expression
- 11.2.4 The yield expression
- 11.3 Operator expressions
- 11.3.1 Assignments
- 11.3.1.1 Single assignments
- 11.3.1.1.1 Single variable assignments
- 11.3.1.1.2 Single indexing assignments
- 11.3.1.1.3 Single method assignments
- 11.3.1.2 Abbreviated assignments
- 11.3.1.2.1 Abbreviated variable assignments
- 11.3.1.2.2 Abbreviated indexing assignments
- 11.3.1.2.3 Abbreviated method assignments
- 11.3.1.3 Multiple assignments
- 11.3.1.4 Assignments with rescue modifiers
- 11.3.2 Unary operators
- 11.3.2.1 The defined? expression
- 11.3.3 Binary operators
- 11.4 Primary expressions
- 11.4.1 Control structures
- 11.4.1.1 Conditional expressions
- 11.4.1.1.1 The if expression
- 11.4.1.1.2 The unless expression
- 11.4.1.1.3 The case expression
- 11.4.1.1.4 Conditional operator
- 11.4.1.2 Iteration expressions
- 11.4.1.2.1 The while expression
- 11.4.1.2.2 The until expression
- 11.4.1.2.3 The for expression
- 11.4.1.3 Jump expressions
- 11.4.1.3.1 The return expression
- 11.4.1.3.2 The break expression
- 11.4.1.3.3 The next expression
- 11.4.1.3.4 The redo expression
- 11.4.1.3.5 The retry expression
- 11.4.1.4 Exceptions
- 11.4.1.4.1 The rescue expression
- 11.4.2 Grouping expression
- 11.4.3 Variable references
- 11.4.3.1 Constants
- 11.4.3.2 Scoped constants
- 11.4.3.3 Global variables
- 11.4.3.4 Class variables
- 11.4.3.5 Instance variables
- 11.4.3.6 Local variables
- 11.4.3.7 Pseudo variables
- 11.4.3.7.1 nil
- 11.4.3.7.2 true and false
- 11.4.3.7.3 self
- 11.4.4 Object constructors
- 11.4.4.1 Array constructor
- 11.4.4.2 Hash constructor
- 11.4.4.3 Range constructor
- 11.4.5 Literals
- 12 Statements
- 12.1 The expression statement
- 12.2 The if modifier statement
- 12.3 The unless modifier statement
- 12.4 The while modifier statement
- 12.5 The until modifier statement
- 12.6 The rescue modifier statement
- 13 Classes and modules
- 13.1 Modules
- 13.1.1 General description
- 13.1.2 Module definition
- 13.1.3 Module inclusion
- 13.2 Classes
- 13.2.1 General description
- 13.2.2 Class definition
- 13.2.3 Inheritance
- 13.2.4 Instance creation
- 13.3 Methods
- 13.3.1 Method definition
- 13.3.2 Method parameters
- 13.3.3 Method invocation
- 13.3.4 Method lookup
- 13.3.5 Method visibility
- 13.3.5.1 Public methods
- 13.3.5.2 Private methods
- 13.3.5.3 Protected methods
- 13.3.5.4 Visibility change
- 13.3.6 The alias statement
- 13.3.7 The undef statement
- 13.4 Eigenclass
- 13.4.1 General description
- 13.4.2 Eigenclass definition
- 13.4.3 Singleton method definition
- 14 Exceptions
- 14.1 Cause of exceptions
- 14.2 Exception handling
- 15 Built-in classes and modules
- 15.1 General description
- 15.2 Built-in classes
- 15.2.1 Object
- 15.2.1.1 Direct superclass
- 15.2.1.2 Included modules
- 15.2.1.3 Constants
- 15.2.1.4 Instance methods
- 15.2.1.4.1 Object#initialize
- 15.2.2 Module
- 15.2.2.1 Direct superclass
- 15.2.2.2 Singleton methods
- 15.2.2.2.1 Module.constants
- 15.2.2.2.2 Module.nesting
- 15.2.2.3 Instance methods
- 15.2.2.3.1 Module#<
- 15.2.2.3.2 Module#<=
- 15.2.2.3.3 Module#<=>
- 15.2.2.3.4 Module#==
- 15.2.2.3.5 Module#===
- 15.2.2.3.6 Module#>
- 15.2.2.3.7 Module#>=
- 15.2.2.3.8 Module#alias method
- 15.2.2.3.9 Module#ancestors
- 15.2.2.3.10 Module#append features
- 15.2.2.3.11 Module#attr
- 15.2.2.3.12 Module#attr accessor
- 15.2.2.3.13 Module#attr reader
- 15.2.2.3.14 Module#attr writer
- 15.2.2.3.15 Module#class eval
- 15.2.2.3.16 Module#class variable defined?
- 15.2.2.3.17 Module#class variable get
- 15.2.2.3.18 Module#class variable set
- 15.2.2.3.19 Module#class variables
- 15.2.2.3.20 Module#const defined?
- 15.2.2.3.21 Module#const get
- 15.2.2.3.22 Module#const missing
- 15.2.2.3.23 Module#const set
- 15.2.2.3.24 Module#constants
- 15.2.2.3.25 Module#extend object
- 15.2.2.3.26 Module#extended
- 15.2.2.3.27 Module#include
- 15.2.2.3.28 Module#include?
- 15.2.2.3.29 Module#included
- 15.2.2.3.30 Module#included modules
- 15.2.2.3.31 Module#initialize
- 15.2.2.3.32 Module#initialize copy
- 15.2.2.3.33 Module#instance methods
- 15.2.2.3.34 Module#method defined?
- 15.2.2.3.35 Module#module eval
- 15.2.2.3.36 Module#private
- 15.2.2.3.37 Module#protected
- 15.2.2.3.38 Module#public
- 15.2.2.3.39 Module#remove class variable
- 15.2.2.3.40 Module#remove const
- 15.2.2.3.41 Module#remove method
- 15.2.2.3.42 Module#undef method
- 15.2.3 Class
- 15.2.3.1 Direct superclass
- 15.2.3.2 Instance methods
- 15.2.3.2.1 Class#initialize
- 15.2.3.2.2 Class#initialize copy
- 15.2.3.2.3 Class#new
- 15.2.3.2.4 Class#superclass
- 15.2.4 NilClass
- 15.2.4.1 Direct superclass
- 15.2.4.2 Instance methods
- 15.2.4.2.1 NilClass#&
- 15.2.4.2.2 NilClass#ˆ
- 15.2.4.2.3 NilClass#nil?
- 15.2.4.2.4 NilClass#|
- 15.2.5 TrueClass
- 15.2.5.1 Direct superclass
- 15.2.5.2 Instance methods
- 15.2.5.2.1 TrueClass#&
- 15.2.5.2.2 TrueClass#ˆ
- 15.2.5.2.3 TrueClass#to s
- 15.2.5.2.4 TrueClass#|
- 15.2.6 FalseClass
- 15.2.6.1 Direct superclass
- 15.2.6.2 Instance methods
- 15.2.6.2.1 FalseClass#&
- 15.2.6.2.2 FalseClass#ˆ
- 15.2.6.2.3 FalseClass#to s
- 15.2.6.2.4 FalseClass#|
- 15.2.7 Numeric
- 15.2.7.1 Direct superclass
- 15.2.7.2 Included modules
- 15.2.7.3 Instance methods
- 15.2.7.3.1 Numeric#+@
- 15.2.7.3.2 Numeric#−@
- 15.2.7.3.3 Numeric#abs
- 15.2.7.3.4 Numeric#coerce
- 15.2.8 Integer
- 15.2.8.1 Direct superclass
- 15.2.8.2 Instance methods
- 15.2.8.2.1 Integer#+
- 15.2.8.2.2 Integer#−
- 15.2.8.2.3 Integer#*
- 15.2.8.2.4 Integer#/
- 15.2.8.2.5 Integer#%
- 15.2.8.2.6 Integer#<=>
- 15.2.8.2.7 Integer#==
- 15.2.8.2.8 Integer#˜
- 15.2.8.2.9 Integer#&
- 15.2.8.2.10 Integer#|
- 15.2.8.2.11 Integer#ˆ
- 15.2.8.2.12 Integer#<<
- 15.2.8.2.13 Integer#>>
- 15.2.8.2.14 Integer#ceil
- 15.2.8.2.15 Integer#downto
- 15.2.8.2.16 Integer#eql?
- 15.2.8.2.17 Integer#floor
- 15.2.8.2.18 Integer#hash
- 15.2.8.2.19 Integer#next
- 15.2.8.2.20 Integer#round
- 15.2.8.2.21 Integer#succ
- 15.2.8.2.22 Integer#times
- 15.2.8.2.23 Integer#to f
- 15.2.8.2.24 Integer#to i
- 15.2.8.2.25 Integer#truncate
- 15.2.8.2.26 Integer#upto
- 15.2.9 Float
- 15.2.9.1 Direct superclass
- 15.2.9.2 Instance methods
- 15.2.9.2.1 Float#+
- 15.2.9.2.2 Float#−
- 15.2.9.2.3 Float#*
- 15.2.9.2.4 Float#/
- 15.2.9.2.5 Float#%
- 15.2.9.2.6 Float#<=>
- 15.2.9.2.7 Float#==
- 15.2.9.2.8 Float#ceil
- 15.2.9.2.9 Float#finite?
- 15.2.9.2.10 Float#floor
- 15.2.9.2.11 Float#infinite?
- 15.2.9.2.12 Float#round
- 15.2.9.2.13 Float#to f
- 15.2.9.2.14 Float#to i
- 15.2.9.2.15 Float#truncate
- 15.2.10 String
- 15.2.10.1 Direct superclass
- 15.2.10.2 Included modules
- 15.2.10.3 Instance methods
- 15.2.10.3.1 String#*
- 15.2.10.3.2 String#+
- 15.2.10.3.3 String#<=>
- 15.2.10.3.4 String#==
- 15.2.10.3.5 String#=˜
- 15.2.10.3.6 String#[]
- 15.2.10.3.7 String#capitalize
- 15.2.10.3.8 String#capitalize!
- 15.2.10.3.9 String#chomp
- 15.2.10.3.10 String#chomp!
- 15.2.10.3.11 String#chop
- 15.2.10.3.12 String#chop!
- 15.2.10.3.13 String#downcase
- 15.2.10.3.14 String#downcase!
- 15.2.10.3.15 String#each line
- 15.2.10.3.16 String#empty?
- 15.2.10.3.17 String#eql?
- 15.2.10.3.18 String#gsub
- 15.2.10.3.19 String#gsub!
- 15.2.10.3.20 String#hash
- 15.2.10.3.21 String#include?
- 15.2.10.3.22 String#initialize
- 15.2.10.3.23 String#initialize copy
- 15.2.10.3.24 String#intern
- 15.2.10.3.25 String#length
- 15.2.10.3.26 String#match
- 15.2.10.3.27 String#replace
- 15.2.10.3.28 String#reverse
- 15.2.10.3.29 String#reverse!
- 15.2.10.3.30 String#scan
- 15.2.10.3.31 String#size
- 15.2.10.3.32 String#slice
- 15.2.10.3.33 String#split
- 15.2.10.3.34 String#sub
- 15.2.10.3.35 String#sub!
- 15.2.10.3.36 String#upcase
- 15.2.10.3.37 String#upcase!
- 15.2.10.3.38 String#to i
- 15.2.10.3.39 String#to f
- 15.2.10.3.40 String#to s
- 15.2.10.3.41 String#to sym
- 15.2.11 Symbol
- 15.2.11.1 Direct superclass
- 15.2.11.2 Instance methods
- 15.2.11.2.1 Symbol#===
- 15.2.11.2.2 Symbol#id2name
- 15.2.11.2.3 Symbol#to s
- 15.2.11.2.4 Symbol#to sym
- 15.2.12 Array
- 15.2.12.1 Direct superclass
- 15.2.12.2 Included modules
- 15.2.12.3 Singleton methods
- 15.2.12.3.1 Array.[]
- 15.2.12.4 Instance methods
- 15.2.12.4.1 Array#*
- 15.2.12.4.2 Array#+
- 15.2.12.4.3 Array#<<
- 15.2.12.4.4 Array#[]
- 15.2.12.4.5 Array#[]=
- 15.2.12.4.6 Array#clear
- 15.2.12.4.7 Array#collect!
- 15.2.12.4.8 Array#concat
- 15.2.12.4.9 Array#each
- 15.2.12.4.10 Array#each index
- 15.2.12.4.11 Array#empty?
- 15.2.12.4.12 Array#first
- 15.2.12.4.13 Array#initialize
- 15.2.12.4.14 Array#initialize copy
- 15.2.12.4.15 Array#join
- 15.2.12.4.16 Array#last
- 15.2.12.4.17 Array#length
- 15.2.12.4.18 Array#map!
- 15.2.12.4.19 Array#pop
- 15.2.12.4.20 Array#push
- 15.2.12.4.21 Array#replace
- 15.2.12.4.22 Array#reverse
- 15.2.12.4.23 Array#reverse!
- 15.2.12.4.24 Array#shift
- 15.2.12.4.25 Array#size
- 15.2.12.4.26 Array#slice
- 15.2.12.4.27 Array#unshift
- 15.2.13 Hash
- 15.2.13.1 Direct superclass
- 15.2.13.2 Included modules
- 15.2.13.3 Instance methods
- 15.2.13.3.1 Hash#==
- 15.2.13.3.2 Hash#[]
- 15.2.13.3.3 Hash#[]=
- 15.2.13.3.4 Hash#clear
- 15.2.13.3.5 Hash#default
- 15.2.13.3.6 Hash#default=
- 15.2.13.3.7 Hash#default proc
- 15.2.13.3.8 Hash#delete
- 15.2.13.3.9 Hash#each
- 15.2.13.3.10 Hash#each key
- 15.2.13.3.11 Hash#each value
- 15.2.13.3.12 Hash#empty?
- 15.2.13.3.13 Hash#has key?
- 15.2.13.3.14 Hash#has value?
- 15.2.13.3.15 Hash#include?
- 15.2.13.3.16 Hash#initialize
- 15.2.13.3.17 Hash#initialize copy
- 15.2.13.3.18 Hash#key?
- 15.2.13.3.19 Hash#keys
- 15.2.13.3.20 Hash#length
- 15.2.13.3.21 Hash#member?
- 15.2.13.3.22 Hash#merge
- 15.2.13.3.23 Hash#replace
- 15.2.13.3.24 Hash#shift
- 15.2.13.3.25 Hash#size
- 15.2.13.3.26 Hash#store
- 15.2.13.3.27 Hash#value?
- 15.2.13.3.28 Hash#values
- 15.2.14 Range
- 15.2.14.1 Direct superclass
- 15.2.14.2 Included modules
- 15.2.14.3 Instance methods
- 15.2.14.3.1 Range#==
- 15.2.14.3.2 Range#===
- 15.2.14.3.3 Range#begin
- 15.2.14.3.4 Range#each
- 15.2.14.3.5 Range#end
- 15.2.14.3.6 Range#exclude end?
- 15.2.14.3.7 Range#first
- 15.2.14.3.8 Range#include?
- 15.2.14.3.9 Range#initialize
- 15.2.14.3.10 Range#last
- 15.2.14.3.11 Range#member?
- 15.2.15 Regexp
- 15.2.15.1 Direct superclass
- 15.2.15.2 Constants
- 15.2.15.3 Patterns
- 15.2.15.4 Matching process
- 15.2.15.5 Singleton methods
- 15.2.15.5.1 Regexp.compile
- 15.2.15.5.2 Regexp.escape
- 15.2.15.5.3 Regexp.last match
- 15.2.15.5.4 Regexp.quote
- 15.2.15.6 Instance methods
- 15.2.15.6.1 Regexp#initialize
- 15.2.15.6.2 Regexp#initialize copy
- 15.2.15.6.3 Regexp#==
- 15.2.15.6.4 Regexp#===
- 15.2.15.6.5 Regexp#=˜
- 15.2.15.6.6 Regexp#casefold?
- 15.2.15.6.7 Regexp#match
- 15.2.15.6.8 Regexp#source
- 15.2.16 MatchData
- 15.2.16.1 Direct superclass
- 15.2.16.2 Instance methods
- 15.2.16.2.1 MatchData#[]
- 15.2.16.2.2 MatchData#begin
- 15.2.16.2.3 MatchData#captures
- 15.2.16.2.4 MatchData#end
- 15.2.16.2.5 MatchData#initialize copy
- 15.2.16.2.6 MatchData#length
- 15.2.16.2.7 MatchData#offset
- 15.2.16.2.8 MatchData#post match
- 15.2.16.2.9 MatchData#pre match
- 15.2.16.2.10 MatchData#size
- 15.2.16.2.11 MatchData#string
- 15.2.16.2.12 MatchData#to a
- 15.2.16.2.13 MatchData#to s
- 15.2.17 Proc
- 15.2.17.1 Direct superclass
- 15.2.17.2 Singleton methods
- 15.2.17.2.1 Proc.new
- 15.2.17.3 Instance methods
- 15.2.17.3.1 Proc#[]
- 15.2.17.3.2 Proc#arity
- 15.2.17.3.3 Proc#call
- 15.2.17.3.4 Proc#clone
- 15.2.17.3.5 Proc#dup
- 15.2.18 Struct
- 15.2.18.1 Direct superclass
- 15.2.18.2 Singleton methods
- 15.2.18.2.1 Struct.new
- 15.2.18.3 Instance methods
- 15.2.18.3.1 Struct#==
- 15.2.18.3.2 Struct#[]
- 15.2.18.3.3 Struct#[]=
- 15.2.18.3.4 Struct#each
- 15.2.18.3.5 Struct#each pair
- 15.2.18.3.6 Struct#members
- 15.2.18.3.7 Struct#select
- 15.2.18.3.8 Struct#initialize
- 15.2.18.3.9 Struct#initialize copy
- 15.2.19 Time
- 15.2.19.1 Direct superclass
- 15.2.19.2 Time computation
- 15.2.19.2.1 Day
- 15.2.19.2.2 Year
- 15.2.19.2.3 Month
- 15.2.19.2.4 Days of month
- 15.2.19.2.5 Hours, Minutes, and Seconds
- 15.2.19.3 Time zone and Local time
- 15.2.19.4 Daylight saving time
- 15.2.19.5 Singleton methods
- 15.2.19.5.1 Time.at
- 15.2.19.5.2 Time.gm
- 15.2.19.5.3 Time.local
- 15.2.19.5.4 Time.mktime
- 15.2.19.5.5 Time.now
- 15.2.19.5.6 Time.utc
- 15.2.19.6 Instance methods
- 15.2.19.6.1 Time#+
- 15.2.19.6.2 Time#−
- 15.2.19.6.3 Time#<=>
- 15.2.19.6.4 Time#asctime
- 15.2.19.6.5 Time#ctime
- 15.2.19.6.6 Time#day
- 15.2.19.6.7 Time#dst?
- 15.2.19.6.8 Time#getgm
- 15.2.19.6.9 Time#getlocal
- 15.2.19.6.10 Time#getutc
- 15.2.19.6.11 Time#gmt?
- 15.2.19.6.12 Time#gmt offset
- 15.2.19.6.13 Time#gmtime
- 15.2.19.6.14 Time#gmtoff
- 15.2.19.6.15 Time#hour
- 15.2.19.6.16 Time#localtime
- 15.2.19.6.17 Time#mday
- 15.2.19.6.18 Time#min
- 15.2.19.6.19 Time#mon
- 15.2.19.6.20 Time#month
- 15.2.19.6.21 Time#sec
- 15.2.19.6.22 Time#to f
- 15.2.19.6.23 Time#to i
- 15.2.19.6.24 Time#usec
- 15.2.19.6.25 Time#utc
- 15.2.19.6.26 Time#utc?
- 15.2.19.6.27 Time#utc offset
- 15.2.19.6.28 Time#wday
- 15.2.19.6.29 Time#yday
- 15.2.19.6.30 Time#year
- 15.2.19.6.31 Time#zone
- 15.2.19.6.32 Time#initialize
- 15.2.19.6.33 Time#initialize copy
- 15.2.20 IO
- 15.2.20.1 Direct superclass
- 15.2.20.2 Included modules
- 15.2.20.3 Singleton methods
- 15.2.20.3.1 IO.open
- 15.2.20.4 Instance methods
- 15.2.20.4.1 IO#close
- 15.2.20.4.2 IO#closed?
- 15.2.20.4.3 IO#each
- 15.2.20.4.4 IO#each byte
- 15.2.20.4.5 IO#each line
- 15.2.20.4.6 IO#eof?
- 15.2.20.4.7 IO#flush
- 15.2.20.4.8 IO#getc
- 15.2.20.4.9 IO#gets
- 15.2.20.4.10 IO#initialize copy
- 15.2.20.4.11 IO#print
- 15.2.20.4.12 IO#putc
- 15.2.20.4.13 IO#puts
- 15.2.20.4.14 IO#read
- 15.2.20.4.15 IO#readchar
- 15.2.20.4.16 IO#readline
- 15.2.20.4.17 IO#readlines
- 15.2.20.4.18 IO#sync
- 15.2.20.4.19 IO#sync=
- 15.2.20.4.20 IO#write
- 15.2.21 File
- 15.2.21.1 Direct superclass
- 15.2.21.2 Singleton methods
- 15.2.21.2.1 File.exist?
- 15.2.21.3 Instance methods
- 15.2.21.3.1 File#initialize
- 15.2.21.3.2 File#path
- 15.2.22 Exception
- 15.2.22.1 Direct superclass
- 15.2.22.2 Built-in exception classes
- 15.2.22.3 Singleton methods
- 15.2.22.3.1 Exception.exception
- 15.2.22.4 Instance methods
- 15.2.22.4.1 Exception#exception
- 15.2.22.4.2 Exception#message
- 15.2.22.4.3 Exception#to s
- 15.2.22.4.4 Exception#initialize
- 15.2.23 StandardError
- 15.2.23.1 Direct superclass
- 15.2.24 ArgumentError
- 15.2.24.1 Direct superclass
- 15.2.25 LocalJumpError
- 15.2.25.1 Direct superclass
- 15.2.25.2 Instance methods
- 15.2.25.2.1 LocalJumpError#exit value
- 15.2.25.2.2 LocalJumpError#reason
- 15.2.26 RangeError
- 15.2.26.1 Direct superclass
- 15.2.27 RegexpError
- 15.2.27.1 Direct superclass
- 15.2.28 RuntimeError
- 15.2.28.1 Direct superclass
- 15.2.29 TypeError
- 15.2.29.1 Direct superclass
- 15.2.30 ZeroDivisionError
- 15.2.30.1 Direct superclass
- 15.2.31 NameError
- 15.2.31.1 Direct superclass
- 15.2.31.2 Instance methods
- 15.2.31.2.1 NameError#name
- 15.2.31.2.2 NameError#initialize
- 15.2.32 NoMethodError
- 15.2.32.1 Direct superclass
- 15.2.32.2 Instance methods
- 15.2.32.2.1 NoMethodError#args
- 15.2.32.2.2 NoMethodError#initialize
- 15.2.33 IndexError
- 15.2.33.1 Direct superclass
- 15.2.34 StopIteration
- 15.2.34.1 Direct superclass
- 15.2.35 IOError
- 15.2.35.1 Direct superclass
- 15.2.36 EOFError
- 15.2.36.1 Direct superclass
- 15.2.37 SystemCallError
- 15.2.37.1 Direct superclass
- 15.2.38 ScriptError
- 15.2.38.1 Direct superclass
- 15.2.39 SyntaxError
- 15.2.39.1 Direct superclass
- 15.2.40 LoadError
- 15.2.40.1 Direct superclass
- 15.3 Built-in modules
- 15.3.1 Kernel
- 15.3.1.1 Singleton methods
- 15.3.1.1.1 Kernel.‘
- 15.3.1.1.2 Kernel.block given?
- 15.3.1.1.3 Kernel.eval
- 15.3.1.1.4 Kernel.global variables
- 15.3.1.1.5 Kernel.iterator?
- 15.3.1.1.6 Kernel.lambda
- 15.3.1.1.7 Kernel.local variables
- 15.3.1.1.8 Kernel.loop
- 15.3.1.1.9 Kernel.method missing
- 15.3.1.1.10 Kernel.p
- 15.3.1.1.11 Kernel.print
- 15.3.1.1.12 Kernel.puts
- 15.3.1.1.13 Kernel.raise
- 15.3.1.1.14 Kernel.require
- 15.3.1.2 Instance methods
- 15.3.1.2.1 Kernel#==
- 15.3.1.2.2 Kernel#===
- 15.3.1.2.3 Kernel# id
- 15.3.1.2.4 Kernel# send
- 15.3.1.2.5 Kernel#‘
- 15.3.1.2.6 Kernel#block given?
- 15.3.1.2.7 Kernel#class
- 15.3.1.2.8 Kernel#clone
- 15.3.1.2.9 Kernel#dup
- 15.3.1.2.10 Kernel#eql?
- 15.3.1.2.11 Kernel#equal?
- 15.3.1.2.12 Kernel#eval
- 15.3.1.2.13 Kernel#extend
- 15.3.1.2.14 Kernel#global variables
- 15.3.1.2.15 Kernel#hash
- 15.3.1.2.16 Kernel#initialize copy
- 15.3.1.2.17 Kernel#inspect
- 15.3.1.2.18 Kernel#instance eval
- 15.3.1.2.19 Kernel#instance of?
- 15.3.1.2.20 Kernel#instance variable defined?
- 15.3.1.2.21 Kernel#instance variable get
- 15.3.1.2.22 Kernel#instance variable set
- 15.3.1.2.23 Kernel#instance variables
- 15.3.1.2.24 Kernel#is a?
- 15.3.1.2.25 Kernel#iterator?
- 15.3.1.2.26 Kernel#kind of?
- 15.3.1.2.27 Kernel#lambda
- 15.3.1.2.28 Kernel#local variables
- 15.3.1.2.29 Kernel#loop
- 15.3.1.2.30 Kernel#method missing
- 15.3.1.2.31 Kernel#methods
- 15.3.1.2.32 Kernel#nil?
- 15.3.1.2.33 Kernel#object id
- 15.3.1.2.34 Kernel#p
- 15.3.1.2.35 Kernel#print
- 15.3.1.2.36 Kernel#private methods
- 15.3.1.2.37 Kernel#protected methods
- 15.3.1.2.38 Kernel#public methods
- 15.3.1.2.39 Kernel#puts
- 15.3.1.2.40 Kernel#raise
- 15.3.1.2.41 Kernel#remove instance variable
- 15.3.1.2.42 Kernel#require
- 15.3.1.2.43 Kernel#respond to?
- 15.3.1.2.44 Kernel#send
- 15.3.1.2.45 Kernel#singleton methods
- 15.3.1.2.46 Kernel#to s
- 15.3.2 Enumerable
- 15.3.2.1 Instance methods
- 15.3.2.1.1 Enumerable#all?
- 15.3.2.1.2 Enumerable#any?
- 15.3.2.1.3 Enumerable#collect
- 15.3.2.1.4 Enumerable#detect
- 15.3.2.1.5 Enumerable#each with index
- 15.3.2.1.6 Enumerable#entries
- 15.3.2.1.7 Enumerable#find
- 15.3.2.1.8 Enumerable#find all
- 15.3.2.1.9 Enumerable#grep
- 15.3.2.1.10 Enumerable#include?
- 15.3.2.1.11 Enumerable#inject
- 15.3.2.1.12 Enumerable#map
- 15.3.2.1.13 Enumerable#max
- 15.3.2.1.14 Enumerable#min
- 15.3.2.1.15 Enumerable#member?
- 15.3.2.1.16 Enumerable#partition
- 15.3.2.1.17 Enumerable#reject
- 15.3.2.1.18 Enumerable#select
- 15.3.2.1.19 Enumerable#sort
- 15.3.2.1.20 Enumerable#to a
- 15.3.3 Comparable
- 15.3.3.1 Instance methods
- 15.3.3.1.1 Comparable#<
- 15.3.3.1.2 Comparable#<=
- 15.3.3.1.3 Comparable#==
- 15.3.3.1.4 Comparable#>
- 15.3.3.1.5 Comparable#>=
- 15.3.3.1.6 Comparable#between?
- Annex A (informative) Grammar Summary
xvi
Information technology — Programming Languages — Ruby
1
1
Scope
2 3
This document specifies the syntax and semantics of the computer programming language Ruby by specifying requirements for a conforming processor and for a conforming program. This document does not specify:
4
5 6
the size or complexity of a program text that exceeds the capacity of any specific data processing system or the capacity of a particular processor; 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; the method for reporting syntactic and runtime errors.
7 8
9
10
11
2
Normative references
12 13 14
The following referenced documents are indispensable for the application of this document. For dated references, only the edition cited applies. For undated references, the latest edition of the referenced document (including any amendments) applies. ISO/IEC 646:1991 Information technology – ISO 7-bit coded character set for information interchange. IEC 60559:1989 Binary floating-point arithmetic for microprocessor systems.
15 16
17
18
3
Conformance
19
A conforming Ruby program shall:
20
use only those features of the language specified in this document; not rely on implementation dependent features;
21
22
A conforming Ruby processor shall: 1
1
accept any conforming programs and behave as specified in this document; reject any program which does not conform to the syntax described in this document; report any unhandled exceptions raised during execution of the conforming program;
2
3
4 5
A conforming Ruby processor may use an internal model for the Ruby language other than the one specified in this document, if it does not change the meaning of a conforming program.
6
4
Terms and definitions
7 8
For the purposes of this document, the following terms and definitions apply. Other terms are defined where they appear in bold slant face or on the left side of a syntax rule. 4.1 block sequence of statements which is passed to a method invocation 4.2 class object which defines the behavior of a set of other objects called its instances NOTE The behavior is a set of methods which can be invoked on an instance.
9 10 11
12 13 14
15
16 17 18
4.3 class variable variable whose value is shared by all the instances of a class 4.4 constant variable which is defined in a class or a module and is accessible outside the class or module NOTE The value of a constant is regularly expected to remain constant during the execution of a program, but Ruby does not force it. In some implementations, an assignment to a constant which already exists causes a warning, but this document does not specifity it.
19 20 21
22 23 24
25 26 27
4.5 eigenclass special class which defines a behavior for only a single object 4.6 exception object which represents an unexpected event 4.7 global variable variable which is accessible everywhere in a Ruby program 4.8 implementation defined possibly differing between implementations, but defined for every implementation 2
28 29 30
31 32 33
34 35 36
1 2 3 4
4.9 implementation dependent possibly differing between implementations, and not necessarily defined for any particular implementation 4.10 instance method method which can be invoked on all the instances of a class 4.11 instance variable variable which belongs to a single object 4.12 local variable variable which is accessible only in a certain scope introduced by a program construct such as a method definition, a block, a class definition, a module definition, an eigenclass definition, or the toplevel of a program 4.13 method procedure which, when invoked on an object, performs a set of computations on the object 4.14 method visibility attribute of a method which determines the conditions on which a method invocation is allowed 4.15 module object which provides features to be included into a class or another module 4.16 object computational entity which has a state and a behavior 4.17 singleton method instance method of the eigenclass of an object 4.18 variable computational entity which stores a reference to an object
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
5 Notational conventions
5.1 Syntax
35
36 37 38
The syntax of the language is presented as a series of productions. Each production consists of the name of the nonterminal symbol being defined followed by “::”, followed by one or more alternatives separated by “|”.
3
1 2
Terminal symbols are shown in typewriter face, and represent sequences of characters as they appear in a program text. Non-terminal symbols are shown in italic face. Each alternative in a production consists of a sequence of terminal and/or nonterminal symbols separated by whitespace. If the same nonterminal symbol occurs on the right side of a production more than once, each occurrence is subscripted with a number to distinguish it from the other occurrences of the same name. An optional symbol is denoted by postfixing the symbol with “?”. A sequence of zero or more repetitions of a symbol is denoted by postfixing the symbol with “*”. A sequence of one or more repetitions of a symbol is denoted by postfixing the symbol with “+”. Parentheses are used to treat a sequence of symbols as a single symbol. A symbol followed by the phrase but not and another symbol represents all sequences of characters represented by the first symbol except for sequences of characters represented by the second symbol. EXAMPLE 1 The following example means that non-escaped-character is any member of sourcecharacter except escape-character :
3 4
5 6 7
8
9 10
11
12
13 14 15
16 17
18 19
non-escaped-character :: source-character but not escape-character
20 21
Text enclosed by “[” and “]” is used to describe a sequence of characters or a location in a program text. EXAMPLE 2 646: The following example means that source-character is any character specified in ISO/IEC
22 23
24 25
source-character :: [ any character in ISO/IEC 646 ]
26 27 28 29
In particular, the notation “[lookahead ∈ set]” indicates that the token immediately following / the notation shall not begin with a sequence of characters represented by one of the members of set. The set is represented as a list of one or more terminal symbols separated by commas, and the list is enclosed by “{” and “}”. EXAMPLE 3 The following example means that the argument following the method-modifier shall not begin with “{”:
30 31
32
command ::
4
1 2
method-identifier [ lookahead ∈ { { } ] argument /
3 4
In this document, use of the words of and in, when expressing a relationship between nonterminal symbols, has the following meanings: X of Y : refers to the X occurring directly in a production defining Y. X in Y : refers to any X occurring in a sequence which is derived directly or indirectly from Y.
5
6 7
8
5.2 Conceptual name
9 10 11 12 13
A conceptual name is a common name given to a set of semantically related nonterminal symbols in the grammar in order to refer to this set in a semantic description. A conceptual name is defined by a conceptual name definition. A conceptual name definition consists of the conceptual name to be defined followed by “::=”, followed by one or more nonterminal symbols or conceptual names, separated by “|”. EXAMPLE The following example defines the conceptual name assignment, which can be used to refer either assignment-expression or assignment-statement.
14 15
16 17 18
assignment ::= assignment-expression | assignment-statement
19
6 Objects
6.1 General description
20
21 22
Ruby is a pure object-oriented language. It is pure in the sense that every value manipulated in a Ruby program is an object including primitive values such as integers. An object is a computational entity which has a state and a behavior. A variable is a computational entity which stores a reference to an object. A variable has a name. A variable is said to be bound to an object if the variable stores a reference to the object. This association of a variable with an object is called a variable binding. When a variable with name N is bound to an object O, N is called the name of the binding, and O is called the value of the binding. An object has a set of variable bindings. A variable whose binding is in this set is an instance variable of that object. This set of bindings of instance variables represents the state of that object and is encapsulated in that object. A method is a procedure which, when invoked, performs a set of computations. The behavior of an object is defined by a set of methods which can be invoked on that object. A method has one or more (when aliased) names associated with it. An association between a name and 5
23
24 25 26 27 28
29 30 31
32 33 34
1 2 3 4 5
a method is called a method binding. When a name N is bound to a method M, N is called the name of the binding, and M is called the value of the binding. A name to which a method is bound is called the method name. A method can be invoked on an object by specifying one of its names. The object on which the method is invoked is called the receiver of the method invocation. There are three constructs which define the behavior of objects: classes, eigenclasses, and modules. A class defines methods shared by objects of the same class. An eigenclass is a special class which defines methods for only a single object. A module defines, and provides methods to be included into a class or another module. All these three constructs are represented as objects, which are dynamically created and modified at run-time. A class creates objects, and the created objects are called direct instances of the class. A class defines a set of methods which can be invoked on all the instances of the class. These methods are instance methods of the class. A class is itself an object, and created by a class definition (see §13.2.2). A class has two sets of variable bindings besides a set of bindings of instance variables. The one is a set of bindings of constants. The other is a set of bindings of class variables, which represents the state shared by all the instances of the class. Every object, including classes, can be associated with at most one special class to the object. This special class is called the eigenclass of the object. The eigenclass defines methods which can be invoked on that object. Those methods are singleton methods of the object. If the object is not a class, the singleton methods of the object can be invoked on only that object. If the object is a class, the singleton methods of the class are similar to so-called class methods because they can be invoked on only that class and its subclasses. An eigenclass is created, and associated with an object by an eigenclass definition (see §13.4.2) or a singleton method definition (see §13.4.3). A class has a single class or nil as its direct superclass. If a class A has a class B as its direct superclass, A is called a direct subclass of B. Classes form a tree-like hierarchy defined by the direct superclass-subclass relation. There is only one class which has nil as its direct superclass. It is the root of the tree. All the ancestors of a class in the tree are called superclasses of the class. All the descendants of a class in the tree are called subclasses of the class. A class inherits constants, class variables, singleton methods, and instance methods from its superclasses, if any (see §13.2.3). If an object C is a direct instance of a class D, C is called an instance of D and all its superclasses. Ruby does not support multiple inheritance; that is, a class can have only one direct superclass. However, Ruby supports module inclusion, which is a mechanism to append features into a class from multiple sources. A module is an object which has the same structure as a class except that it cannot create an instance of itself and cannot be inherited. As with classes, a module has a set of class variables and instance methods. Instance methods and class variables defined in a module can be used by other classes, modules and eigenclasses by including the module into them. While a class can have only one direct superclass, a class or a module can include multiple modules. Instance methods defined in a module can be invoked on an instance of a class which includes the module. A module is created by a module definition (see §13.1.2). Objects are created at some time during program execution. The lifetime of an object begins when the object is created and ends when all references to it are no longer possible.
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
6
1
6.2 Boolean values
2
An object is classified into either a true value or a false value. Only false and nil are false values. The pseudo variable false is the only instance of the class FalseClass, and is represented by the keyword false. The pseudo variable nil is the only instance of the class NilClass, and represented by the keyword nil. Objects other than false and nil are classified into true values. The pseudo variable true is the only instance of the class TrueClass, and represented by the keyword true.
3 4 5
6 7
8
7 Execution context
7.1 Contextual attributes
9
10
An execution context is a set of attributes which affects an evaluation of a program. An execution context is not a part of the language. It is defined in this document only for the description of the semantics of a program. A conforming processor shall evaluate a program as if it acted upon an execution context in the manner described in this document. An execution context consists of a set of attributes as described below. Each attribute of an execution context except [global-variable-bindings] forms a logical stack. The names of attributes are enclosed in double square brackets “[” and “]”. Attributes of an execution context are changed when a program construct is evaluated. The following are the attributes of an execution context: [self ] : A logical stack of objects, the top of which is the object to which the pseudo variable
11 12 13
14 15 16 17
18
19 20 21
self is bound (see §11.4.3.7.3). The object at the top of the stack is called the current self . [class-module-list] : A logical stack of lists of classes or modules. The class or module at
22 23
the head of the list which is on the top of the stack is called the current class or module. [default-method-visibility] : A logical stack of visibilities of methods, each of which is one
24 25 26
of the public, private, and protected visibility. The top of the stack is called the current visibility. [local-variable-bindings] : A logical stack of sets of bindings of local variables. The element at the top of the stack is called the current set of local variable bindings. A set of bindings is pushed onto the stack on every entry into a local variable scope (see §9.1.1), and the top element is removed from the stack on every exit from the scope. The scope with which an element in the stack is associated is called the scope of the set of local variable bindings. [invoked-method-name] : A logical stack of names by which methods are invoked. [defined-method-name] : A logical stack of names with which the invoked methods are
27 28 29 30 31 32
33
34 35
defined.
7
1 2
[block] : A logical stack of blocks passed to method invocations. An element of the stack
may be block-not-given, which indicates that no block is passed to a method invocation. [global-variable-bindings] : A set of bindings of global variables.
3
4
The term unset is used to describe the state of an attribute which is set to nothing.
5
7.2 The initial state
6 7
Immediately prior to an execution of a program, the attributes of the execution context is initialized as follows: a) Create an empty set of variable bindings, and set [global-variable-bindings] to the set of variable bindings. Create built-in classes and modules as described in §15. Create an empty stack for each attribute of the execution context except [global-variablebindings] . Create a direct instance of the class Object and push it onto [self ] . Create a list containing only the class Object and push the list onto [class-module-list] . Push the private visibility onto [default-visibility] . Push block-not-given onto [block] .
8 9
10
b) c)
11 12
13
d) e) f) g)
14
15
16
17
8 Lexical structure
18 19
When several prefixes of the input under parsing process have matching productions, the production that matches the longest prefix is selected.
20
8.1 Source text
21
Syntax
22 23
source-character :: [ any character in ISO/IEC 646 ]
24 25 26 27
A program is represented as a sequence of characters. A conforming processor shall accept any conforming program which consists of characters in ISO/IEC 646, encoded with the octet values as specified in ISO/IEC 646. The support for any other character sets and encodings is implementation dependent. Terminal symbols are sequences of those characters in ISO/IEC 646. Control characters in ISO/IEC 646 are represented by hexadecimal notation. EXAMPLE “0x0a” represents a line feed character.
28 29
30
8
1
8.2 Line terminators
2
Syntax
3 4
line-terminator :: 0x0d? 0x0a
5 6 7
A line-terminator is ignored when it is used to separate tokens. For this reason, except in §8.4 and §8.5, line-terminator s are omitted from productions. However, in some cases, the presence or absence of a line-terminator changes the meaning of a program. A location of program text where a line-terminator shall occur is indicated by the notation “[ line-terminator here ]”. A location of program text where a line-terminator shall not occur is indicated by the notation “[ no line-terminator here ]”; however, a conforming processor may ignore the notation where the ignorance does not introduce ambiguity. EXAMPLE statements are separated by separator s (see §10.2). The syntax of the separator s is as follows:
8 9 10 11
12 13
14 15 16
separator :: ; | [ line-terminator here ]
17
The source
18 19
x = 1 + 2 puts x is therefore separated to two statements x = 1 + 2 and puts x by a line-terminator. The source
20
21
22 23
x = 1 + 2 is parsed as a single statement x = 1 + 2 because x = is not a valid statement. However, the source
24
25 26
x = 1 + 2 is not a valid Ruby program because a line-terminator shall not occur before = in a single-variableassignment-expression, and = 1 + 2 is not a valid statement. The fact that a line-terminator shall not occur before = is indicated in the syntax of the single-variable-assignment-expression as follows (see §11.3.1.1.1):
27 28 29 30
9
1 2
single-variable-assignment-expression :: variable [ no line-terminator here ] = operator-expression
3
8.3 Whitespace
4
Syntax
5 6 7
whitespace :: 0x09 | 0x0b | 0x0c | 0x0d | 0x20 | \ 0x0d? 0x0a
8 9 10
whitespace is ignored when it is used to separate tokens. For this reason, except in §8.4 and §8.5, whitespace is omitted from productions. However, in some cases, the presence or absence of whitespace changes the meaning of a program. A location of program text where whitespace shall occur is indicated by the notation “[ whitespace here ]”. A location of program text where whitespace shall not occur is indicated by the notation “[ no whitespace here ]”. A line-terminator shall not occur in the location where whitespace shall not occur. Therefore, this notation also indicates that a line-terminator shall not occur.
11 12 13 14
15
8.4 Comments
16
Syntax
17 18 19
comment :: single-line-comment | multi-line-comment single-line-comment :: # comment-content ? comment-content :: line-content line-content :: source-character + multi-line-comment :: multi-line-comment-begin-line multi-line-comment-line ? multi-line-comment-end-line multi-line-comment-begin-line :: [ beginning of a line ] =begin rest-of-begin-end-line ? line-terminator 10
20 21
22 23
24 25
26 27 28
29 30
1 2 3
multi-line-comment-end-line :: [ beginning of a line ] =end rest-of-begin-end-line ? ( line-terminator | [ end of a program ] ) rest-of-begin-end-line :: whitespace + comment-content line :: comment-content line-terminator multi-line-comment-line :: line but not multi-line-comment-end-line
4 5
6 7
8 9
10 11
The notation “[ beginning of a line ]” indicates the beginning of a program or the position immediately after a line-terminator. Any characters that are considered as line-terminator s are not allowed within a line-content. A comment is either a single-line-comment or a multi-line-comment. A comment is considered to be whitespace. A single-line-comment begins with “#” and continues to the end of the line. A line-terminator at the end of the line is not considered to be a part of the comment. A single-line-comment can contain any characters except line-terminator s. A multi-line-comment begins with a line beginning with =begin, and continues until and including a line that begins with =end. Unlike single-line-comments, a line-terminator on a multi-line-comment-end-line, if any, is considered to be part of the comment.
12
13 14
15 16 17
18 19 20
21
8.5 Tokens
22
Syntax
23 24 25 26 27 28
token :: reserved-word | identifier | punctuator | operator | literal
29
8.5.1 Syntax
Reserved words
30
31 32
reserved-word :: __LINE__ | __ENCODING__ | __FILE__ | BEGIN | END | alias | and | begin 11
1 2 3 4
| | | |
break | case | class | def | defined? | do | else | elsif | end ensure | for | false | if | in | module | next | nil | not | or | redo rescue | retry | return | self | super | then | true | undef | unless until | when | while | yield
5
Reserved words are case-sensitive. 8.5.2 Syntax Identifiers
6
7
8 9 10 11 12 13 14
identifier :: local-variable-identifier | global-variable-identifier | class-variable-identifier | instance-variable-identifier | constant-identifier | method-identifier local-variable-identifier :: ( lowercase-character | _ ) identifier-character * global-variable-identifier :: � identifier-start-character identifier-character * class-variable-identifier :: @@ identifier-start-character identifier-character * instance-variable-identifier :: @ identifier-start-character identifier-character * constant-identifier :: uppercase-character identifier-character * method-identifier :: method-only-identifier | assignment-like-method-identifier | constant-identifier | local-variable-identifier method-only-identifier :: ( constant-identifier | local-variable-identifier ) ( ! | ? ) assignment-like-method-identifier :: ( constant-identifier | local-variable-identifier ) = 12
15 16
17 18
19 20
21 22
23 24
25 26 27 28 29
30 31
32 33
1 2 3 4 5
identifier-character :: lowercase-character | uppercase-character | decimal-digit |_ identifier-start-character :: lowercase-character | uppercase-character |_ uppercase-character :: A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z lowercase-character :: a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z decimal-digit :: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
6 7 8 9
10 11 12
13 14 15
16 17
18 19
An identifier is a sequence of identifier-character s optionally prefixed by one of “�”, “@@”, or “@”, and optionally postfixed by one of “?”, “!”, or “=”. A global-variable-identifier begins with “�”. A class-variable-identifier starts with “@@”. An instance-variable-identifier begins with “@”. A constant-identifier begins with an uppercasecharacter. A local-variable-identifier begins with a lowercase-character or “ ”. A method-identifier is a constant-identifier or a local-variable-identifier optionally followed by one of “?”, “!”, or “=”. 8.5.3 Syntax Punctuators
20 21 22
23 24
25
26
27 28
punctuator :: [ | ] | ( | ) | { | } | :: | , | ; | .. | ... | ? | : | =>
29
8.5.4 Syntax
Operators
30
31 32
operator :: operator-method-name 13
1
| assignment-operator operator-method-name :: ^ | & | | | <=> | == | === | !~ | =~ | > | >= | < | <= | << | >> | + | - | * | / | % | ** | ~ | +@ | -@ | [] | []= | ‘ assignment-operator :: assignment-operator-name = assignment-operator-name :: + | - | * | ** | / | ^ | % | << | >> | & | && | || | |
2 3 4
5 6
7 8
9
8.5.5
Literals
10 11 12 13 14 15
literal :: numeric-literal | string-literal | array-literal | regular-expression-literal | symbol
16
8.5.5.1 Syntax
Numeric literals
17
18 19 20
numeric-literal :: signed-number | unsigned-number unsigned-number :: integer-literal | float-literal integer-literal :: decimal-integer-literal | binary-integer-literal | octal-integer-literal | hexadecimal-integer-literal decimal-integer-literal :: digit-decimal-integer-literal | prefixed-decimal-integer-literal
21 22 23
24 25 26 27 28
29 30 31
14
1 2 3
digit-decimal-integer-literal :: 0 | decimal-digit-without-zero ( _? decimal-digit )* prefixed-decimal-integer-literal :: 0 ( d | D ) digit-decimal-part digit-decimal-part :: decimal-digit ( _? decimal-digit )* binary-integer-literal :: 0 ( b | B ) binary-digit ( _? binary-digit )* octal-integer-literal :: 0 ( _ | o | O )? octal-digit ( _? octal-digit )* hexadecimal-integer-literal :: 0 ( x | X ) hexadecimal-digit ( _? hexadecimal-digit )* float-literal :: decimal-float-literal | exponent-float-literal decimal-float-literal :: digit-decimal-integer-literal . digit-decimal-part exponent-float-literal :: base-part exponent-part base-part :: decimal-float-literal | digit-decimal-integer-literal exponent-part :: ( e | E ) ( + | - )? digit-decimal-part signed-number :: ( + | - ) unsigned-number decimal-digit-without-zero :: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 octal-digit :: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7
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
15
1 2
binary-digit :: 0 | 1 hexadecimal-digit :: decimal-digit | a | b | c | d | e | f | A | B | C | D | E | F
3 4
5 6 7
Semantics A numeric-literal evaluates to either an instance of the class Integer or a direct instance of the class Float. An unsigned-number of the form integer-literal evaluates to an instance of the class Integer whose value is the value of one of the alternatives on the right-hand side. An unsigned-number of the form float-literal evaluates to a direct instance of the class Float whose value is the value of one of the alternatives on the right-hand side. A signed-number which begins with “+” evaluates to an instance represented by the unsignednumber. A signed-number which begins with “-” evaluates to an instance of the class Integer or a direct instance of the class Float whose value is the negated value of the instance represented by the unsigned-number. The value of an integer-literal, a decimal-integer-literal, a float-literal, or a base-part is the value of one of the alternatives on the right-hand side. The value of a digit-decimal-integer-literal is either 0 or the value of a sequence of characters, which consist of a decimal-digit-without-zero followed by sequence of decimal-digits, ignoring interleaving “ ”s, computed using base 10. The value of a prefixed-decimal-integer-literal is the value of the digit-decimal-part. The value of a digit-decimal-part is the value of the sequence of decimal-digits, ignoring interleaving “ ”s, computed using base 10. The value of a binary-integer-literal is the value of the sequence of binary-digits, ignoring interleaving “ ”s, computed using base 2. The value of an octal-integer-literal is the value of the sequence of octal-digits, ignoring interleaving “ ”s, computed using base 8. The value of a hexadecimal-integer-literal is the value of the sequence of hexadecimal-digits, ignoring interleaving “ ”s, computed using base 16. The value of a decimal-float-literal is the value of the digit-decimal-integer-literal plus the value of the digit-decimal-part times 10−n where n is the number of decimal-digits of the digit-decimalpart. The value of an exponent-float-literal is the value of the base-part times 10n where n is the value of the exponent-part. The value of an exponent-part is the negative value of the digit-decimal-part if “-” occurs, 16
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
1
otherwise, it is the value of the digit-decimal-part. There is no limitation on the maximum magnitude for the value of an integer-literal. The precision of the value of a float-literal is implementation defined; however, if the underlying platform of a conforming processor supports IEC 60559:1989, the representation of an instance of the class Float should be the 64-bit double format as specified in §3.2.2 of IEC 60559:1989. The value of a float-literal is rounded to fit in the representation of an instance of the class Float in an implementation defined way. 8.5.5.2 Syntax String literals
2 3 4 5 6 7
8
9
10 11 12 13 14 15 16
string-literal :: single-quoted-string | double-quoted-string | quoted-non-expanded-literal-string | quoted-expanded-literal-string | here-document | external-command-execution
17
Semantics A string-literal evaluates to a direct instance of the class String. 8.5.5.2.1 Syntax Single quoted strings
18
19
20
21 22
single-quoted-string :: ’ single-quoted-string-character * ’ single-quoted-string-character :: non-escaped-single-quoted-string-character | single-quoted-escape-sequence single-quoted-escape-sequence :: single-escape-character-sequence | non-escaped-single-quoted-string-character-sequence single-escape-character-sequence :: \ single-escaped-character non-escaped-single-quoted-string-character-sequence :: \ non-escaped-single-quoted-string-character
23 24 25
26 27 28
29 30
31 32
17
1 2
single-escaped-character :: ’ |\ non-escaped-single-quoted-string-character :: source-character but not single-escaped-character
3 4
5
Semantics A single-quoted-string consists of zero or more characters enclosed by single quotes. The sequence of single-quoted-string-character s within the pair of single quotes represents the content of a string as it occurs in program text literally, except for single-escape-character-sequences. The sequence “\\” represents “\”. The sequence “\’” represents “’”. 8.5.5.2.2 Syntax Double quoted strings
6 7 8 9
10
11
12 13
double-quoted-string :: " double-quoted-string-character * " double-quoted-string-character :: source-character but not ( " | \ ) | double-escape-sequence | interpolated-character-sequence double-escape-sequence :: simple-escape-sequence | non-escaped-sequence | line-terminator-escape-sequence | octal-escape-sequence | hex-escape-sequence | control-escape-sequence simple-escape-sequence :: \ double-escaped-character non-escaped-sequence :: \ non-escaped-double-quoted-string-character line-terminator-escape-sequence :: \ line-terminator non-escaped-double-quoted-string-character :: source-character but not ( double-escaped-character | line-terminator )
14 15 16 17
18 19 20 21 22 23 24
25 26
27 28
29 30
31 32
18
1 2
double-escaped-character :: \ | n | t | r | f | v | a | e | b | s octal-escape-sequence :: \ octal-digit ( octal-digit octal-digit ? )? hex-escape-sequence :: \ x hexadecimal-digit hexadecimal-digit ? control-escape-sequence :: \ ( C - | c ) control-escaped-character control-escaped-character :: double-escape-sequence | ? | source-character but not ( \ | ? ) interpolated-character-sequence :: # global-variable-identifier | # class-variable-identifier | # instance-variable-identifier | # { compound-statement }
3 4
5 6
7 8
9 10 11 12
13 14 15 16 17
18
Semantics A double-quoted-string consists of zero or more characters enclosed by double quotes. The sequence of double-quoted-string-character s within the pair of double quotes represents the content of a string. Except for a double-escape-sequence and an interpolated-character-sequence, a double-quotedstring-character represents a character as it occurs in program text. A simple-escape-sequence represents a character as shown in Table 1. An octal-escape-sequence represents a character the code of which is the value of the sequence of octal-digits computed using base 8. A hex-escape-sequence represents a character the code of which is the value of the sequence of hexadecimal-digits computed using base 16. A non-escaped-sequence represents a non-escaped-double-quoted-string-character. A line-terminator-escape-sequence is used to break the content of a string into separate lines in program text without inserting a line-terminator into the string. A line-terminator-escapesequence does not count as a character of the string. A control-escape-sequence represents a character the code of which is computed by performing a bitwise AND operation between 0x9f and the code of the character represented by the control19
19 20 21
22 23
24
25 26
27 28
29
30 31 32
33 34
Table 1 – Simple escape sequences Escape sequence Character code \\ \n \t \r \f \v \a \e \b \s 0x5c 0x0a 0x09 0x0d 0x0c 0x0b 0x07 0x1b 0x08 0x20
1 2
escaped-character, except when the control-escaped-character is ?, in which case, the controlescape-sequence represents a character the code of which is 127. An interpolated-character-sequence is a part of a string-literal which is dynamically evaluated when the string-literal in which it is embedded is evaluated. The interpolated-charactersequences within a string-literal are evaluated in the order in which they occur in program text. The value of a string-literal which contains interpolated-character-sequences is a direct instance of the class String the content of which is made from the string-literal where each occurrence of interpolated-character-sequence is replaced by the content of an instance of the class String which is the dynamically evaluated value of the interpolated-character-sequence. An interpolated-character-sequence is evaluated as follows: a) If it is of the form # global-variable-identifier, evaluate the global-variable-identifier (see §11.4.3.3). Let V be the resulting value. If it is of the form # class-variable-identifier, evaluate the class-variable-identifier (see §11.4.3.4). Let V be the resulting value. If it is of the form # instance-variable-identifier, evaluate the instance-variable-identifier (see §11.4.3.5). Let V be the resulting value. If it is of the form # { compound-statement }, evaluate the compound-statement (see §10.2). Let V be the resulting value. If V is an instance of the class String, V is the value of interpolated-character-sequence. Otherwise, invoke the method to s on V with an empty list of arguments. Let S be the resulting value. If S is an instance of the class String, S is the value of interpolated-character-sequence.
3 4 5 6
7 8 9 10
11
12 13
14 15
b)
16 17
c)
18 19
d)
20
e) f)
21 22
23
g)
20
1 2
h)
Otherwise, the value of interpolated-character-sequence is an instance of the class String, the content of which is implementation defined. Quoted non-expanded literal strings
3
8.5.5.2.3 Syntax
4
5 6
quoted-non-expanded-literal-string :: %q literal-beginning-delimiter non-expanded-literal-string * literal-ending-delimiter non-expanded-literal-string :: non-expanded-literal-character | non-expanded-delimited-string non-expanded-delimited-string :: literal-beginning-delimiter non-expanded-literal-string * literal-ending-delimiter non-expanded-literal-character :: non-escaped-literal-character | non-expanded-literal-escape-sequence non-escaped-literal-character :: source-character but not quoted-literal-escape-character non-expanded-literal-escape-sequence :: non-expanded-literal-escape-character-sequence | non-escaped-non-expanded-literal-character-sequence non-expanded-literal-escape-character-sequence :: \ non-expanded-literal-escaped-character non-expanded-literal-escaped-character :: literal-beginning-delimiter | literal-ending-delimiter | \ quoted-literal-escape-character :: non-expanded-literal-escaped-character non-escaped-non-expanded-literal-character-sequence :: \ non-escaped-non-expanded-literal-character non-escaped-non-expanded-literal-character :: source-character but not non-expanded-literal-escaped-character
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
21
1 2
The literal-beginning-delimiter of a non-expanded-delimited-string shall be the same character as the literal-beginning-delimiter of the quoted-non-expanded-literal-string. A literal-ending-delimiter shall be the same character as the corresponding literal-beginningdelimiter, except when the literal-beginning-delimiter is one of the characters on the left in Table 2. In that case, the literal-ending-delimiter is the corresponding character on the right in Table 2. Table 2 – Matching literal-beginning-delimiter literal-ending-delimiter literal-beginning-delimiter literal-ending-delimiter { ( [ < } ) ] >
3 4 5 6
7 8
The production non-expanded-delimited-string applies only when the literal-beginning-delimiter is one of the characters of matching-literal-beginning-delimiter. Semantics A non-expanded-literal-string represents the content of a string as it occurs in program text literally, except for non-expanded-literal-escape-character-sequences. A non-expanded-literal-escape-character-sequence represents a character as follows. The sequence “\\” represents “\”; the sequence \literal-beginning-delimiter, a literal-beginning-delimiter ; the sequence \literal-ending-delimiter, a literal-ending-delimiter. 8.5.5.2.4 Syntax Quoted expanded literal strings
9 10 11
12 13 14
15
16
17 18
quoted-expanded-literal-string :: % Q? literal-beginning-delimiter expanded-literal-string * literal-ending-delimiter expanded-literal-string :: expanded-literal-character | expanded-delimited-string expanded-literal-character :: non-escaped-literal-character | double-escape-sequence | interpolated-character-sequence expanded-delimited-string :: literal-beginning-delimiter expanded-literal-string * literal-ending-delimiter literal-beginning-delimiter :: source-character but not alpha-numeric-character-or-separator 22
19 20 21
22 23 24 25
26 27
28 29
1 2 3 4 5 6
alpha-numeric-character-or-separator :: whitespace | line-terminator | uppercase-character | lowercase-character | decimal-digit literal-ending-delimiter :: [ depending on the literal-beginning-delimiter ] matching-literal-beginning-delimiter :: ( | { | < | [
7 8
9 10
11 12
The literal-beginning-delimiter of an expanded-delimited-string shall be the same character as the literal-beginning-delimiter of the quoted-expanded-literal-string. The literal-ending-delimiter shall match the literal-beginning-delimiter as described in §8.5.5.2.3. The production expanded-delimited-string applies only when the literal-beginning-delimiter is one of the characters of matching-literal-beginning-delimiter. Semantics A expanded-literal-string represents the content of a string. A character in an expanded-literal-string other than a double-escape-sequence or an interpolatedcharacter-sequence represents a character as it occurs in program text. A double-escape-sequence and an interpolated-character-sequence represent characters as described in §8.5.5.2.2. 8.5.5.2.5 Syntax Here documents
13
14 15
16
17
18 19 20
21
22
23 24
here-document :: heredoc-start-line heredoc-body heredoc-end-line heredoc-start-line :: heredoc-signifier rest-of-line heredoc-signifier :: << heredoc-delimiter-specifier rest-of-line :: line-content ? line-terminator heredoc-body :: heredoc-body-line * 23
25 26
27 28
29 30
31 32
1 2
heredoc-body-line :: line but not heredoc-end-line
3
Semantics A here-document is represented by several lines of program text, and evaluates to a direct instance of the class String or the value of the invocation of the method ‘. The heredoc-signifier, the heredoc-body, and the heredoc-end-line in a here-document are treated as a unit and considered to be a single token occurring at the place where the heredoc-signifier occurs. The first character of the rest-of-line becomes the head of the input after the heredocument has been processed. The object to which here-document evaluates is either a direct instance S of the class String whose content is represented by the heredoc-body or the value of the invocation of the method ‘ with S as the only argument. The form of the heredoc-delimiter-specifier determines both the form of the heredoc-end-line and the way in which the here-document is processed, as described below. Syntax
4 5
6 7 8 9
10 11 12
13 14
15
16 17
heredoc-delimiter-specifier :: -? heredoc-delimiter heredoc-delimiter :: non-quoted-delimiter | single-quoted-delimiter | double-quoted-delimiter | command-quoted-delimiter non-quoted-delimiter :: non-quoted-delimiter-identifier non-quoted-delimiter-identifier :: identifier-character * single-quoted-delimiter :: ’ single-quoted-delimiter-identifier * ’ single-quoted-delimiter-identifier :: source-character but not ’ double-quoted-delimiter :: " double-quoted-delimiter-identifier * "
18 19 20 21 22
23 24
25 26
27 28
29 30
31 32
24
1 2
double-quoted-delimiter-identifier :: source-character but not " command-quoted-delimiter :: ‘ command-quoted-delimiter-identifier * ‘ command-quoted-delimiter-identifier :: source-character but not ‘ heredoc-end-line :: indented-heredoc-end-line | non-indented-heredoc-end-line indented-heredoc-end-line :: [ beginning of a line ] whitespace * heredoc-delimiter-identifier line-terminator non-indented-heredoc-end-line :: [ beginning of a line ] heredoc-delimiter-identifier line-terminator heredoc-delimiter-identifier :: non-quoted-delimiter-identifier | single-quoted-delimiter-identifier | double-quoted-delimiter-identifier | command-quoted-delimiter-identifier
3 4
5 6
7 8 9
10 11
12 13
14 15 16 17 18
19 20 21
Semantics The form of a heredoc-end-line depends on the presence or absence of the beginning “-” of the heredoc-delimiter-specifier. If the heredoc-delimiter-specifier begins with “-”, a line of the form indented-heredoc-end-line is treated as the heredoc-end-line, otherwise, a line of the form non-indented-heredoc-end-line is treated as the heredoc-end-line. In both forms, the heredoc-delimiter-identifier shall be the same sequence of characters as it occurs in the corresponding part of heredoc-delimiter. If the heredoc-delimiter is of the form non-quoted-delimiter, the heredoc-delimiter-identifier shall be the same sequence of characters as the non-quoted-delimiter-identifier ; if it is of the form single-quoted-delimiter, the single-quoted-delimiter-identifier ; if it is of the form of double-quoteddelimiter, the double-quoted-delimiter-identifier ; if it is of the form of command-quoted-delimiter, the command-quoted-delimiter-identifier. The object to which a here-document evaluates is created as follows: a) Create a direct instance of the class String from the heredoc-body, the treatment of which depends on the form of the heredoc-delimiter as follows:
22 23 24 25
26 27 28 29 30
31
32 33
34 35
If heredoc-delimiter is of the form single-quoted-delimiter, the heredoc-body is treated as a sequence of source-character s as it occurs in program text literally. 25
1 2 3
If heredoc-delimiter is in any of the forms non-quoted-delimiter, double-quoted-delimiter, or command-quoted-delimiter, the heredoc-body is treated as a sequence of doublequoted-string-character s as described in §8.5.5.2.2.
4
Let S be that instance of the class String. b) c) If the heredoc-delimiter is not of the form command-quoted-delimiter, let V be S. Otherwise, invoke the method ‘ on the current self with the list of arguments whose only element is S. Let V be the resulting value of the method invocation. V is the object to which the here-document evaluates. External command execution
5
6 7
8
d)
9
8.5.5.2.6 Syntax
10
11 12 13
external-command-execution :: backquoted-external-command-execution | quoted-external-command-execution backquoted-external-command-execution :: ‘ double-quoted-string-character * ‘ quoted-external-command-execution :: %x literal-beginning-delimiter expanded-literal-string * literal-ending-delimiter
14 15
16 17
18
The literal-ending-delimiter shall match the literal-beginning-delimiter as described in §8.5.5.2. Semantics An external-command-execution is a form to invoke the method “‘”. An external-command-execution is evaluated as follows: a) If the external-command-execution is of the form backquoted-external-command-execution, construct a direct instance of the class String S by replacing the two “‘” with “"” and evaluating the resulting double-quoted-string as described in §8.5.5.2.2. If the external-command-execution is of the form quoted-external-command-execution, construct a direct instance of the class String S by replacing “%x” with “%Q” and evaluating the resulting quoted-expanded-literal-string as described in §8.5.5.2.4. Invoke the method “‘” on the current self with a list of arguments whose only element is S. The resulting value is the value of the external-command-execution. Array literals
19 20
21
22 23 24
25 26 27
b)
28
c) d)
29
30
8.5.5.3 Syntax 26
31
1 2 3
array-literal :: quoted-non-expanded-array-constructor | quoted-expanded-array-constructor quoted-non-expanded-array-constructor :: %w literal-beginning-delimiter non-expanded-array-content literal-ending-delimiter non-expanded-array-content :: quoted-array-item-separator-list ? non-expanded-array-item-list ? quoted-array-item-separator-list ? non-expanded-array-item-list :: non-expanded-array-item ( quoted-array-item-separator-list non-expanded-array-item )* quoted-array-item-separator-list :: quoted-array-item-separator + quoted-array-item-separator :: whitespace | line-terminator non-expanded-array-item :: non-expanded-array-item-character + non-expanded-array-item-character :: non-escaped-array-item-character | non-expanded-array-escape-sequence non-escaped-array-item-character :: non-escaped-array-character | matching-literal-delimiter non-escaped-array-character :: non-escaped-literal-character but not quoted-array-item-separator matching-literal-delimiter :: ( | { | < | [ | ) | } | > | ] non-expanded-array-escape-sequence :: non-expanded-literal-escape-sequence but not escaped-quoted-array-item-separator | escaped-quoted-array-item-separator escaped-quoted-array-item-separator :: \ quoted-array-item-separator 27
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
1 2
quoted-expanded-array-constructor :: %W literal-beginning-delimiter expanded-array-content literal-ending-delimiter expanded-array-content :: quoted-array-item-separator-list ? expanded-array-item-list ? quoted-array-item-separator-list ? expanded-array-item-list :: expanded-array-item ( quoted-array-item-separator-list expanded-array-item )* expanded-array-item :: expanded-array-item-character + expanded-array-item-character :: non-escaped-array-item-character | expanded-array-escape-sequence | interpolated-character-sequence expanded-array-escape-sequence :: double-escape-sequence but not escaped-quoted-array-item-separator | escaped-quoted-array-item-separator
3 4 5
6 7
8 9
10 11 12 13
14 15 16
17
The literal-ending-delimiter shall match the literal-beginning-delimiter as described in §8.5.5.2. When the literal-beginning-delimiter is one of the matching-literal-beginning-delimiter, the quotednon-expanded-array-constructor and the quoted-expanded-array-constructor is determined as follows. Let N be 0. For each character C which appears after “%w” or “%W”, take the following steps. a) b) c) If C is a literal-beginning-delimiter which is not prefixed by a “\”, increment N by 1. If C is a literal-ending-delimiter which is not prefixed by a “\”, decrement N by 1. If N is 0 and C is the literal-ending-delimiter, terminate these steps.
18 19 20
21
22
23
24
25 26
The literal-ending-delimiter in Step c is the literal-ending-delimiter of the quoted-non-expandedarray-constructor or the quoted-expanded-array-constructor. Semantics An array-literal evaluates to a direct instance of the class Array. A quoted-non-expanded-array-constructor is evaluated as follows: a) b) 28 Create an empty direct instance of the class Array. Let A be the instance. If non-expanded-array-item-list occurs, for each non-expanded-array-item of the non-expanded-
27
28
29
30
31
1
array-item-list, take the following steps: 1) Create a direct instance of the class String S, the content of which is represented by the sequence of non-expanded-array-item-character s. A non-expanded-array-item-character represents itself, except in the case of a nonexpanded-array-escape-sequence. A non-expanded-array-escape-sequence represents a character as described in §8.5.5.2.3, except in the case of an escaped-quoted-array-itemseparator. An escaped-quoted-array-item-separator represents a quoted-array-item-separator. 2) c) Append S to A.
2 3
4 5 6 7
8
9
The value of the quoted-non-expanded-array-constructor is A.
10
A quoted-expanded-array-constructor is evaluated as follows: a) b) Create an empty direct instance of the class Array. Let A be the instance. If expanded-array-item-list occurs, process each expanded-array-item of the expanded-arrayitem-list as follows: 1) Create a direct instance of the class String S, the content of which is represented by the sequence of expanded-array-item-character s. An expanded-array-item-character represents itself, except in the case of an expandedarray-escape-sequence and an interpolated-character-sequence. An expanded-array-escapesequence represents a character as described in §8.5.5.2.2, except in the case of an escaped-quoted-array-item-separator. An escaped-quoted-array-item-separator represents a quoted-array-item-separator. An interpolated-character-sequence represents a sequence of characters as described in §8.5.5.2.2. 2) c) Append S to A.
11
12 13
14 15
16 17 18 19 20 21
22
23
The value of the quoted-expanded-array-constructor is A. Regular expression literals
24
8.5.5.4 Syntax
25
26 27 28 29
regular-expression-literal :: / regular-expression-body / regular-expression-option * | %r literal-beginning-delimiter expanded-literal-string * literal-ending-delimiter regular-expression-option * regular-expression-body :: regular-expression-character * regular-expression-character :: source-character but not ( / | \ ) | \\ 29
30 31
32 33 34
1 2
| line-terminator-escape-sequence | interpolated-character-sequence regular-expression-option :: i | m
3 4
5 6
Within an expanded-literal-string, a literal-beginning-delimiter shall be the same character as the literal-beginning-delimiter of a regular-expression-literal. The literal-ending-delimiter shall match the literal-beginning-delimiter as described in §8.5.5.2.3. If a regular-expression-literal of the form / regular-expression-body / regular-expression-option* is the first argument (see §11.2.1), the first character of the regular-expression-body shall not be whitespace. Semantics A regular-expression-literal evaluates to a direct instance of the class Regexp. The pattern of an instance of the class Regexp resulting from a regular-expression-literal is the string which regular-expression-character s or expanded-literal-strings represent. If the string cannot be derived from the pattern (see §15.2.15.3), the evaluation of the program shall be terminated and a syntax error shall be reported. A regular-expression-character other than the sequence \ \, a line-terminator-escape-sequence, or interpolated-character-sequence represents themselves. A expanded-literal-string other than a line-terminator-escape-sequence or interpolated-character-sequence represents themselves. The sequence \ \ of regular-expression-character represents a single character \. A line-terminator-escape-sequence in a regular-expression-character and an expanded-literalstring is ignored in the resulting pattern of an instance of the class Regexp. An interpolated-character-sequence in a regular-expression-literal and an expanded-literal-string is evaluated as described in §8.5.5.2.2, and represents a string which is the content of the resulting an instance of the class String. A regular-expression-option specifies the ignorecase and the multiline properties of an instance of the class Regexp resulting from a regular-expression-literal. If i occurs in a regular-expressionoption, the ignorecase property of the resulting instance of the class Regexp is set to true. If m occurs in a regular-expression-option, the multiline property of the resulting instance of the class Regexp is set to true. The grammar for a pattern of an instance of the class Regexp created from a regular-expressionliteral is described in §15.2.15. 8.5.5.5 Syntax Symbol literals
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
30
1 2 3
symbol :: symbol-literal | dynamic-symbol symbol-literal :: : symbol-name dynamic-symbol :: : single-quoted-string | : double-quoted-string | %s literal-beginning-delimiter non-expanded-literal-string * literal-ending-delimiter symbol-name :: method-identifier | operator-method-name | reserved-word | instance-variable-identifier | global-variable-identifier | class-variable-identifier
4 5
6 7 8 9
10 11 12 13 14 15 16
17 18
single-quoted-strings, double-quoted-strings, and non-expanded-literal-strings shall not contain any sequences which represent the character 0x00. Within a non-expanded-literal-string, literal-beginning-delimiter shall be the same character as the literal-beginning-delimiter of the dynamic-symbol. The literal-ending-delimiter shall match the literal-beginning-delimiter as described in §8.5.5.2.3. Semantics A symbol evaluates to a direct instance of the class Symbol. A symbol-literal evaluates to a direct instance of the class Symbol whose name is the symbol-name. A dynamic-symbol evaluates to a direct instance of the class Symbol whose name is the content of an instance of the class String which is the value of the single-quoted-string(see §8.5.5.2.1), double-quoted-string(see §8.5.5.2.2), or non-expanded-literal-string(see §8.5.5.2.3).
19 20
21
22
23 24 25 26 27
28
9 Scope of variables
29
A scope is a region of a program text with which a set of bindings of variables is associated.
30
9.1 Local variables
31
A local variable is referred to by a local-variable-identifier. 9.1.1 Scopes of local variables
32
33
Scopes for local variables are introduced by the following program constructs:
31
1
program (see §10.1) class-body (see §13.2.2) module-body (see §13.1.2) eigenclass-body (see §13.4.2) method-definition (see §13.3.1) and singleton-method-definition (see §13.4.3), for both of which the scope starts with the method-parameter-part and continues up to and including the method-body. block (see §11.2.2)
2
3
4
5 6 7
8
9 10 11
Let P be any of the above program constructs. Let S be the region of P excluding all the regions of any of the above program constructs (except block ) nested within P. Then, S is the local variable scope which corresponds to the program construct P. The scope of a local variable is the local variable scope whose set of local variable bindings contains the binding of the local variable, which is resolved as described below. When a local-variable-identifier which is a reference to a local variable occurs (see §9.1.2), the binding of the local variable is resolved as follows: a) b) c) d) Let N be the local-variable-identifier. Let B be the current set of local variable bindings. Let S be the scope of B. If a binding with name N exists in B, that binding is the resolved binding. If a binding with name N does not exist in B :
12 13
14 15
16
17
18
19
20
If S is a local variable scope which corresponds to a block : 1) If the local-variable-identifier occurs as a left-hand-side of a block-formal-argumentlist, whether to proceed to the next step or not is implementation defined. Replace B with the element immediately below the current B on [local-variablebindings] , and continue searching for a binding with name N from Step b.
21 22
23 24
2)
25
Otherwise, a binding is considered not resolved. References to local variables
26
9.1.2
27 28 29 30
An occurrence of a local-variable-identifier can be a reference to a local variable or a method invocation. In order to determine whether the occurrence of a local-variable-identifier is a reference to a local variable or a method invocation, before the evaluation of a local variable scope, the scope is scanned sequentially for local-variable-identifier s. For each occurrence of a local-variable-identifier I, take the following steps: a) 32 If I occurs in one of the forms below, I is a reference to a local variable.
31
32
1
mandatory-parameter optional-parameter-name array-parameter-name block-parameter-name variable of left-hand-side variable of single-variable-assignment-expression variable of single-variable-assignment-statement variable of abbreviated-variable-assignment-expression variable of abbreviated-variable-assignment-statement
2
3
4
5
6
7
8
9
10
b)
If I occurs in one of the forms below:
11
variable of singleton variable of primary-expression
12
13
and the following condition holds, I is a reference to a local variable.
14 15 16
Let P be the point where I occurs and let S be the innermost local variable scope which encloses P and which does not correspond to a block. Let R be the region of a program between the beginning of S and P. The same identifier as I occurs as a reference to a local variable in R.
17
18
c)
Otherwise, I is a method invocation.
19
9.2 Global variables
20 21
The scope of global variables is global in the sense that they are accesible everywhere in a Ruby program. Global variable bindings are created in [global-variable-bindings] .
22
10 Program structure
10.1 Program
23
24
Syntax program :: compound-statement
25 26
33
1
Semantics A program is evaluated as follows: a) b) c) d) Push an empty set of bindings onto [local-variable-bindings] . Evaluate the compound-statement. The resulting value is the value of the program. Restore the execution context by removing the element from the top of [local-variablebindings] , even when an exception is raised and not handled during Step b.
2
3
4
5
6 7
8
10.2 Compound statement
9
Syntax
10 11
compound-statement :: statement-list ? separator-list ? statement-list :: statement ( separator-list statement )* separator-list :: separator ;* separator :: ; | [ line-terminator here ]
12 13
14 15
16 17 18
19
Semantics A compound-statement is evaluated as follows: a) b) If the statement-list does not occur, the value of the compound-statement is nil. If the statement-list occurs, evaluate each statement of the statement-list in the order it appears in the program text. If one of the statements of the statement-list is terminated by a jump-expression, terminate the evaluation of the statement-list immediately. None of the following statements of the statement-list is evaluated. In this case, the value of the compound-statement is undefined. If none of the statements of the statement-list is terminated by a jump-expression, the value of the compound-statement is the value of the last statement of the statement-list.
20
21
22 23
24 25 26
c)
27 28
d)
34
1
11 Expressions
2
Syntax
3 4
expression :: keyword-logical-expression
5
Semantics See §11.1 for keyword-logical-expression.
6
7
11.1 Logical expressions
8
Syntax
9 10 11 12
keyword-logical-expression :: keyword-NOT-expression | keyword-AND-expression | keyword-OR-expression keyword-NOT-expression :: method-invocation-without-parentheses | operator-expression | logical-NOT-with-method-invocation-without-parentheses | not keyword-NOT-expression logical-NOT-expression ::= logical-NOT-with-method-invocation-without-parentheses | logical-NOT-with-unary-expression logical-NOT-with-method-invocation-without-parentheses :: ! method-invocation-without-parentheses logical-NOT-with-unary-expression :: ! unary-expression keyword-AND-expression :: expression and keyword-NOT-expression keyword-OR-expression :: expression or keyword-NOT-expression logical-OR-expression :: logical-AND-expression | logical-OR-expression || logical-AND-expression 35
13 14 15 16 17
18 19 20
21 22
23 24
25 26
27 28
29 30 31
1 2 3
logical-AND-expression :: equality-expression | logical-AND-expression && equality-expression
4
Semantics A logical-NOT-expression or a keyword-NOT-expression of the form not keyword-NOT-expression is evaluated as follows: a) If it is of the form not keyword-NOT-expression, evaluate the keyword-NOT-expression. Let X be the resulting value. If it is a logical-NOT-expression, evaluate its method-invocation-without-parentheses or unary-expression. Let X be the resulting value. If X is a true value, the value of the keyword-NOT-expression or the logical-NOT-expression is false. Otherwise, the value of the keyword-NOT-expression or the logical-NOT-expression is true.
5 6
7 8
9 10
b)
11 12
c)
13
d)
14 15
Instead of the above process, a conforming processor may evaluate a logical-NOT-expression as follows: a) Evaluate the unary-expression or the method-invocation-without-parentheses. Let V be the resulting value. Create an empty list of arguments L. Invoke the method !@ on V with L as the list of arguments. The resulting value is the value of the logical-NOT-expression.
16 17
18 19
b)
20
In this case, the processor shall:
21
include the operator !@ in operator-method-name. define an instance method !@ in the class Object or one of its superclasses, if any. The method !@ shall not take any arguments. The method !@ shall return true if the receiver is false or nil, and shall return false otherwise.
22 23 24
25 26
A logical-AND-expression of the form logical-AND-expression && equality-expression or a keywordAND-expression is evaluated as follows: a) b) Evaluate the expression or the logical-AND-expression. Let X be the resulting value. If X is a true value, evaluate the keyword-NOT-expression or equality-expression. Let Y be the resulting value. The value of the keyword-AND-expression or the logical-ANDexpression is Y. Otherwise, the value of the keyword-AND-expression or the logical-AND-expression is X.
27
28 29 30
31
c)
32 33
A keyword-OR-expression or a logical-OR-expression of the form logical-OR-expression || logicalAND-expression is evaluated as follows: 36
1
a) b)
Evaluate the expression or the logical-OR-expression. Let X be the resulting value. If X is a false value, evaluate the keyword-NOT-expression or the logical-AND-expression. Let Y be the resulting value. The value of the keyword-OR-expression or logical-ORexpression is Y. Otherwise, the value of the keyword-OR-expression or logical-OR-expression is X.
2 3 4
5
c)
6
11.2 Method invocation expressions
7
Syntax
8 9 10 11 12 13 14 15 16 17 18
primary-method-invocation :: super-with-optional-argument | indexing-method-invocation | method-only-identifier | method-identifier ( [ no whitespace here ] argument-with-parentheses )? block ? | primary-expression [ no line-terminator here ] . method-name ( [ no whitespace here ] argument-with-parentheses )? block ? | primary-expression [ no line-terminator here ] :: method-name [ no whitespace here ] argument-with-parentheses block ? | primary-expression [ no line-terminator here ] :: method-name-without-constant block ? indexing-method-invocation :: primary-expression [ no line-terminator here ] optional-whitespace ? [ indexing-argument-list ? ] optional-whitespace :: [ whitespace here ] method-name-without-constant :: method-name but not constant-identifier method-invocation-without-parentheses :: command | chained-command-with-do-block | chained-command-with-do-block ( . | :: ) method-name argument | return-with-argument | break-with-argument | next-with-argument command :: super-with-argument | yield-with-argument | method-identifier argument | primary-expression [ no line-terminator here ] ( . | :: ) method-name argument
19 20 21
22 23
24 25
26 27 28 29 30 31 32
33 34 35 36 37
37
1 2
chained-command-with-do-block :: command-with-do-block chained-method-invocation * chained-method-invocation :: ( . | :: ) method-name | ( . | :: ) method-name [ no whitespace here ] [ lookahead ∈ { { } ] argument-with-parentheses / command-with-do-block :: super-with-argument-and-do-block | method-identifier argument do-block | primary-expression [ no line-terminator here ] ( . | :: ) method-name argument do-block
3 4 5 6
7 8 9 10 11
12 13
The primary-expression of a primary-method-invocation, command, and indexing-method-invocation shall not be a jump-expression. If the argument-with-parentheses of a primary-method-invocation occurs, and the block-argument of the argument of the argument-with-parentheses occurs, the block of the primary-methodinvocation shall not occur. If the argument of a command-with-do-block occurs, and the block-argument of the argumentin-parentheses of the argument (see §11.2.1) occurs, the do-block of the command-with-do-block shall not occur. The optional-whitespace of an indexing-method-invocation shall not occur if its primary-expression is any of the following construct:
14 15 16
17 18 19
20 21
22 23
A primary-method-invocation of the form method-only-identifier, method-identifier, primaryexpression . method-name, or primary-expression :: method-name-without-constant A method-invocation-without-parentheses of the form chained-command-with-do-block which satisfies all of the following conditions: a) Let M be the chained-command-with-do-block. One or more chained-method-invocation of M occurs. Let I be the last chained-method-invocation of M, in the order they appear in program text. I is of the form (.|::) method-name.
24 25
26 27
28 29
b)
30
Semantics A primary-method-invocation is evaluated as follows: a) If the primary-method-invocation is a super-with-optional-argument or an indexing-methodinvocation, evaluate it. The resulting value is the value of the primary-method-invocation.
31
32 33
34 35
b)
If the primary-method-invocation is a method-only-identifier, let O be the current self and let M be the method-only-identifier. Create an empty list of arguments L.
38
1
If the method-identifier of the primary-method-invocation occurs: 1) 2) Let O be the current self and let M be the method-identifier. If the argument-with-parentheses occurs, construct a list of arguments and a block from the argument-with-parentheses as described in §11.2.1. Let L be the resulting list. Let B be the resulting block, if any. If the argument-with-parentheses does not occur, create an empty list of arguments L. 3) If the block occurs, let B be the block.
2
3 4 5
6 7
8
9
If the . of the primary-method-invocation occurs: 1) Evaluate the primary-expression and let O be the resulting value. Let M be the method-name. If the argument-with-parentheses occurs, construct a list of arguments and a block from the argument-with-parentheses as described in §11.2.1. Let L be the resulting list. Let B be the resulting block, if any. If the argument-with-parentheses does not occur, create an empty list of arguments L. 3) If the block occurs, let B be the block.
10 11
12 13 14
2)
15 16
17
18
If the :: and method-name of the primary-method-invocation occur: 1) Evaluate the primary-expression and let O be the resulting value. Let M be the method-name. Construct a list of arguments and a block from the argument-with-parentheses as described in §11.2.1. Let L be the resulting list. Let B be the resulting block, if any. If the block occurs, let B be the block.
19 20
21 22 23
2)
24
3)
25
If the :: and method-name-without-constant of the primary-method-invocation occur: 1) Evaluate the primary-expression and let O be the resulting value. Let M be the method-name-without-constant. Create an empty list of arguments L. If the block occurs, let B be the block.
26 27
28
2) 3) c)
29
30 31
Invoke the method M on O with L as the list of arguments and B, if any, as the block. (see §13.3.3). The resulting value is the value of the primary-method-invocation.
32
An indexing-method-invocation is evaluated as follows: 39
1
a) b)
Evaluate the primary-expression. Let O be the resulting value. If the indexing-argument-list occurs, construct a list of arguments from the indexingargument-list as described in §11.2.1. Let L be the resulting list. If the indexing-argument-list does not occur, Create an empty list of arguments L. Invoke the method [] on O with L as the list of arguments. The resulting value is the value of the indexing-method-invocation.
2 3
4
c) d)
5 6
7
A method-invocation-without-parentheses is evaluated as follows:
8 9
If the method-invocation-without-parentheses is a command, evaluate it. The resulting value is the value of the method-invocation-without-parentheses. If the method-invocation-without-parentheses is a return-with-argument, break-with-argument or next-with-argument, evaluate it (see §11.4.1.3). If the chained-command-with-do-block of the method-invocation-without-parentheses occurs: a) b) Evaluate the chained-command-with-do-block. Let V be the resulting value. If the method-name and the argument of the method-invocation-without-parentheses occur: 1) 2) Let M be the method-name. Construct a list of arguments from the argument as described in §11.2.1 and let L be the resulting list. If the block-argument of the argument-in-parentheses of the argument occurs, let B be the block to which the block-argument corresponds. Invoke the method M on V with L as the list of arguments and B, if any, as the block. Replace V with the resulting value.
10 11
12
13
14 15
16
17 18 19
20 21
3)
22
4) c)
23
The value of the method-invocation-without-parentheses is V.
24
A command is evaluated as follows: a) b) If the command is a super-with-argument or a yield-with-argument, evaluate it. Otherwise: 1) If the method-identifier of the command occurs: i) ii) Let O be the current self and let M be the method-identifier. Construct a list of arguments from the argument as described in §11.2.1 and let L be the resulting list. If the block-argument of the argument-in-parentheses of the argument occurs, let B be the block to which the block-argument corresponds. 40
25
26
27
28
29 30
31 32
1
2)
If the primary-expression, method-name, and the argument of the command occurs: i) Evaluate the primary-expression. Let O be the resulting value. Let M be the method-name. Construct a list of arguments from the argument as described in §11.2.1 and let L be the resulting list. If the block-argument of the argument-in-parentheses of the argument occurs, let B be the block to which the block-argument corresponds.
2 3
4 5
ii)
6 7
8 9
3)
Invoke the method M on O with L as the list of arguments and B, if any, as the block. The resulting value is the value of the command.
10
A chained-command-with-do-block is evaluated as follows: a) b) Evaluate the command-with-do-block and let V be the resulting value. For each chained-method-invocation, in the order they appears in the program text, take the following steps: 1) 2) Let M be the method-name of the chained-method-invocation. If the argument-with-parentheses occurs, construct a list of arguments and a block from the argument-with-parentheses as described in §11.2.1 and let L be the resulting list. Let B be the resulting block, if any. If the argument-with-parentheses does not occur, create an empty list of arguments L. 3) 4) c) Invoke the method M on V with L as the list of arguments and B, if any, as the block. Replace V with the resulting value.
11
12 13
14
15 16 17
18
19
20
21
The value of the chained-command-with-do-block is V.
22
A command-with-do-block is evaluated as follows:
23 24
If the command-with-do-block is a super-with-argument-and-do-block, evaluate it. The resulting value is the value of the command-with-do-block. Otherwise: a) If the method-identifier of the command occurs, let O be the current self and let M be the method-name. If the method-identifier of the command does not occur, evaluate the primary-expression, let O be the resulting value and let M be the method-name. b) Construct a list of arguments from the arguments of the command-with-do-block and let L be the resulting list. Invoke the method M on O with L as the list of arguments and the do-block as the block. The resulting value is the value of the command-with-do-block. 41
25
26 27
28 29
30 31
32 33
c)
1
11.2.1 Syntax
Method arguments
2
3 4 5 6 7 8
indexing-argument-list :: command | operator-expression-list ,? | operator-expression-list , splatting-argument | association-list ,? | splatting-argument splatting-argument :: * operator-expression operator-expression-list :: operator-expression ( , operator-expression )* argument-with-parentheses :: () | ( argument-in-parentheses ) | ( operator-expression-list , chained-command-with-do-block ) | ( chained-command-with-do-block ) argument :: [ no line-terminator here ] [ lookahead ∈ { { } ] optional-whitespace ? / argument-in-parentheses argument-in-parentheses :: command | ( operator-expression-list | association-list ) ( , splatting-argument )? ( , block-argument )? | operator-expression-list , association-list ( , splatting-argument )? ( , block-argument )? | splatting-argument ( , block-argument )? | block-argument block-argument :: & operator-expression
9 10
11 12
13 14 15 16 17
18 19 20
21 22 23 24 25 26 27 28
29 30
31 32
The operator-expression of a splatting-argument, operator-expression-list, and block-argument shall not be a jump-expression. If the operator-expression-list of an argument-in-parentheses occurs, the first operator-expression of the operator-expression-list is called the first argument. If a splatting-argument is the first argument, whitespaces shall not occur between its * and 42
33 34
35
1 2
operator-expression. If a block-argument is the first argument, whitespaces shall not occur between its & and operator-expression. If the first argument of an argument is other than the following constructs, the optionalwhitespace shall occur.
3 4
5 6
A variable-reference of the form global-variable-identifier, class-variable-identifier or instancevariable-identifier (see §11.4.3). A single-quoted-string or double-quoted-string (see §8.5.5.2). A symbol-literal, or a dynamic-symbol of the form : [no whitespace here] single-quoted-string or : [no whitespace here] double-quoted-string (see §8.5.5.5). An external-command-execution of the form backquoted-external-command-execution (see §8.5.5.2.6). A scoped-constant-reference whose primary-expression occurs and the primary-expression is any of these constructs. A primary-method-invocation whose primary-expression occurs and the primary-expression is any of these constructs.
7
8 9
10 11
12 13
14 15
16
Semantics The list of arguments used for method invocation is constructed from indexing-argument-list, splatting-argument, argument-with-parentheses, or argument. An indexing-argument-list is processed as follows: a) b) Create an empty list of arguments L. Evaluate the command, operator-expressions of operator-expression-lists, and the associationlist and append their values to L in the order they appear in the program text. If the splatting-argument occurs, construct a list of arguments from it and concatenate the resulting list to L.
17 18
19
20
21 22
23 24
c)
25
A splatting-argument is processed as follows: a) b) c) d) Create an empty list of arguments L. Evaluate the operator-expression. Let V be the resulting value. If V is not an instance of the class Array, the behavior is implementation dependent. Append each element of V, in the indexing order, to L.
26
27
28
29
30
An argument-with-parentheses is processed as follows: a) Create an empty list of arguments L.
31
43
1 2 3 4
b)
If the argument-in-parentheses occurs, construct a list of arguments from it and concatenate the resulting list to L. If block-argument of argument-in-parentheses occurs, the block to which the block-argument corresponds is the block which is passed to the method invocation with L. If the operator-expression-list occurs, for each operator-expression of the operator-expressionlist, in the order they appears in the program text, take the following steps: 1) 2) Evaluate the operator-expression. Let V be the resulting value. Append V to L.
5 6
c)
7
8
9
d)
If the chained-command-with-do-block occurs, evaluate it. Append the resulting value to L.
10
An argument is processed as follows: a) b) Evaluate the argument-in-parentheses. Let L be the resulting list.
11
12
13
An argument-in-parentheses is processed as follows: a) b) c) Create an empty list of arguments L. If the command occurs, evaluate it. Append the resulting value to L. If the operator-expression-list occurs, for each operator-expression of the operator-expressionlist, in the order they appears in the program text, take the following steps: 1) 2) d) e) Evaluate the operator-expression. Let V be the resulting value. Append V to L.
14
15
16 17
18
19
20
If the association-list occurs, evaluate it. Append the resulting value to L. If the splatting-argument occurs, construct a list of arguments from it and concatenate the resulting list to L. If the block-argument occurs, construct a block which is passed to a method invocation as described below.
21 22
23 24
f)
25 26
A block which is passed to a method invocation is constructed from the block-argument as follows: a) b) c) Evaluate the operator-expression. Let P be the resulting value. If P is not an instance of the class Proc, the behavior is implementation dependent. Otherwise, the resulting block is the block which P represents. Blocks
27
28
29
30
11.2.2 Syntax 44
31
1 2 3
block :: brace-block | do-block brace-block :: { block-formal-argument ? block-body } do-block :: do block-formal-argument ? block-body end block-formal-argument :: || | || | | block-formal-argument-list | block-formal-argument-list :: left-hand-side | multiple-left-hand-side block-body :: compound-statement
4 5
6 7
8 9 10 11
12 13 14
15 16
17 18
Whether the left-hand-side (see §11.3.1.3) in the block-formal-argument-list is allowed to be of the following forms is implementation defined.
19
constant-identifier global-variable-identifier instance-variable-identifier class-variable-identifier primary-expression [ indexing-argument-list? ] primary-expression (. | ::) (local-variable-identifier | constant-identifier ) :: constant-identifier
20
21
22
23
24
25
26 27
Whether the grouped-left-hand-side in the block-formal-argument-list is allowed to be the following form is implementation defined.
28
( (multiple-left-hand-side-item ,)+ );
29
Semantics A block is a sequence of statements or expressions passed to a method invocation. 45
30
1 2 3
A block can be called either by a yield-expression (see §11.2.4) or by invoking the method call on an instance of the class Proc which is created by an invocation of the method Proc.new to which the block is passed (see §15.2.17.3.3). A block can be called with arguments. If a block is called by a yield-expression, the arguments to the yield-expression are used as the arguments to the block call. If a block is called by an invocation of the method call, the arguments to the method invocation is used as the arguments to the block call. A block is evaluated under the execution context as it exists just before the method invocation to which the block is passed. However, the changes of variable bindings in [local-variable-bindings] after the block is passed to the method invocation affect the execution context. Let Eb be the affected execution context. Both the do-block and the brace-block of the block are evaluated as follows: a) b) c) d) Let Eo be the current execution context. Let L be the list of arguments passed to the block. Set the execution context to Eb . Push an empty set of local variable bindings onto [local-variable-bindings] . If the block-formal-argument-list in the do-block or the brace-block occurs:
4 5 6 7
8 9 10 11
12
13
14
15
16
17
If the block-formal-argument-list is of the form left-hand-side or grouped-left-hand-side: — If the length of L is 0, let X be nil. — If the length of L is 1, let X be the only element of L. — If the length of L is larger than 1, the result of this step is implementation dependent. — If the block-formal-argument-list is of the form left-hand-side, evaluate a singlevariable-assignment-expression E (see §11.3.1.1.1), where the variable of E is the left-hand-side and the value of the operator-expression of E is X. — If the block-formal-argument-list is of the form grouped-left-hand-side, evaluate a many-to-many-assignment-expression E (see §11.3.1.3), where the multiple-lefthand-side of E is the grouped-left-hand-side and the value of the method-invocationwithout-parentheses or operator-expression of E is X.
18
19
20 21
22 23 24
25 26 27 28
29 30
If the block-formal-argument-list is of the form multiple-left-hand-side and the multipleleft-hand-side is not a grouped-left-hand-side: 1) If the length of L is 1: i) If the only element of L is not an instance of the class Array, the result of this step is implementation dependent. Create a list of arguments Y which contains the elements of L, preserving their order.
31
32 33
34 35
ii)
46
1
2) 3)
If the length of L is 0 or larger than 1, let Y be L. Evaluate the many-to-many-assignment-statement E as described in §11.3.1.3, where the multiple-left-hand-side of E is the block-formal-argument-list and the list of arguments constructed from the multiple-right-hand-side of E is Y.
2 3 4
5
e)
Evaluate the block-body. If the evaluation of the block-body:
6
is terminated by a break-expression: — If the method invocation with which block is passed has already terminated when the block is called: 1) 2) Let S be an instance of the class Symbol with name break. If the jump-argument of the break-expression occurs, let V be the value of the jump-argument. Otherwise, let V be nil. Raise a direct instance of the class LocalJumpError which has two instance variable bindings, one named @reason with the value S and the other named @exit value with the value V.
7 8
9
10 11
12 13 14
3)
15 16
— Otherwise, restore the execution context to Eo and terminate Step i and take Step j of the current method invocation (see §13.3.3). If the jump-argument of the break-expression occurs, the value of the current method invocation is the value of the jump-argument. Otherwise, the value of the current method invocation is nil.
17 18 19
20
is terminated by a redo-expression, repeat Step e. is terminated by a next-expression: — If the jump-argument of the next-expression occurs, let V be the value of the jump-argument. — Otherwise, let V be nil.
21
22 23
24
25 26
is terminated by a return-expression, remove the element from the top of [local-variablebindings] . terminates otherwise, let V be the resulting value of the evaluation of the block-body.
27
28 29
f)
Unless Step e is terminated by a return-expression, restore the execution context to Eo , even when an exception is raised and not handled in Step d or e. The value of calling the do-block or the brace-block is V. The super expression
30
g)
31
11.2.3 Syntax
32
47
1 2 3 4
super-expression ::= super-with-optional-argument | super-with-argument | super-with-argument-and-do-block super-with-optional-argument :: super ( [ no whitespace here ] argument-with-parentheses )? block ? super-with-argument :: super argument super-with-argument-and-do-block :: super argument do-block
5 6
7 8
9 10
11 12
The block-argument of the argument-in-parentheses of the argument (see §11.2.1) of a superwith-argument-and-do-block shall not occur. Semantics A super-expression is evaluated as follows: a) If the current self is pushed by an eigenclass-definition (see §13.4.2), or an invocation of one of the following methods, the behavior is implementation dependent:
13
14
15 16
17
the method class eval of the class Module (see §15.2.2.3.15) the method module eval of the class Module (see §15.2.2.3.35) the method instance eval of the class Kernel (see §15.3.1.2.18)
18
19
20
b)
Let A be an empty list. Let B be the top of [block] .
21 22
If the super-expression is a super-with-optional-argument, and neither the argumentwith-parentheses nor the block occurs, construct a list of arguments as follows: 1) Let M be the method which correspond to the current method invocation. Let L be the parameter-list of the method-parameter-part of M. Let S be the set of local variable bindings in [local-variable-bindings] which corresponds to the current method invocation. If the mandatory-parameter-list occurs in L, for each mandatory-parameter p, take the following steps: i) ii) Let v be the value of the binding with name p in S. Append v to A.
23 24 25 26
27 28
2)
29
30
48
1 2
3)
If the optional-parameter-list occurs in L, for each optional-parameter p, take the following steps: i) ii) Let n be the optional-parameter-name of p. Let v be the value of the binding with name n in S.
3
4
5
iii) Append v to A. 4) If the array-parameter occurs in L: i) ii) Let n be the array-parameter-name of the array-parameter. Let v be the value of the binding with name n in S. Append each element of v, in the indexing order, to A.
6
7
8 9
10 11
If the super-expression is a super-with-optional-argument with either or both of the argument-with-parentheses and the block : — If the argument-with-parentheses occurs, construct a list of arguments and a block as described in §11.2.1. Let A be the resulting list. Let B be the resulting block, if any. — If the block occurs, Let B be the block.
12 13 14
15
16 17 18 19
If the super-expression is a super-with-argument, construct the list of arguments from the argument as described in §11.2.1. Let A be the resulting list. If block-argument of the argument-in-parentheses of argument occurs, let B be the block constructed from the block-argument. If the super-expression is a super-with-argument-and-do-block, construct a list of arguments from the argument as described in §11.2.1. Let A be the resulting list. Let B be the do-block.
20 21 22
23
c)
Determine the method to be invoked as follows: 1) 2) Let C be the current class or module. Let N be the top of [defined-method-name] . Search for a method binding with name N from Step b in §13.3.4, assuming that C in §13.3.4 to be C. If a binding is found and its value is not undef (see §13.1.1), let V be the value of the binding. Otherwise, add a direct instance of the class Symbol with name N to the head of A, and invoke the method method missing on the current self with A as arguments and B as the block. Then, terminate the evaluation of the super-expression. The value of the super-expression is the resulting value of the method invocation.
24
25 26
27 28
3)
29 30 31 32
4)
33 34 35
d)
Take Step g, h, i, and j of §13.3.3, assuming that A, B, M, R, and V in §13.3.3 to be A, B, N, the current self, and V in this subclause respectively. The value of the super-expression is the resulting value. 49
1
11.2.4 Syntax
The yield expression
2
3 4 5
yield-expression ::= yield-with-optional-argument | yield-with-argument yield-with-optional-argument :: yield-with-parentheses-and-argument | yield-with-parentheses-without-argument | yield yield-with-parentheses-and-argument :: yield [ no whitespace here ] ( argument-in-parentheses ) yield-with-parentheses-without-argument :: yield [ no whitespace here ] ( ) yield-with-argument :: yield argument
6 7 8 9
10 11
12 13
14 15
16 17
The block-argument of the argument-in-parentheses (see §11.2.1) of a yield-with-parentheses-andargument shall not occur. The block-argument of the argument-in-parentheses of the argument (see §11.2.1) of a yield-withargument shall not occur. Semantics A yield-expression calls the block at the top of [block] . A yield-with-optional-argument is evaluated as follows: a) Let B be the top of [block] . If B is block-not-given: 1) 2) 3) Let S be a direct instance of the class Symbol with name noreason. Let V be an implementation defined value. Raise a direct instance of the class LocalJumpError which has two instance variable bindings, one named @reason with the value S and the other named @exit value with the value V.
18 19
20
21
22
23
24
25
26 27 28
29 30
b)
If the yield-with-optional-argument is of the form yield-with-parentheses-and-argument, create a list of arguments from the argument as described in §11.2.1. Let L be the list.
50
1 2
c)
If the yield-with-optional-argument is of the form yield-with-parentheses-without-argument or yield, create an empty list of argument L. Call B with L as described in §11.2.2. The value of yield-with-optional-argument is the value of the block call.
3
d) e)
4
5
A yield-with-argument is evaluated as follows: a) Let B be the top of [block] . If B is block-not-given: 1) 2) 3) Let S be a direct instance of the class Symbol with name noreason. Let V be an implementation defined value. Raise a direct instance of the class LocalJumpError which has two instance variable bindings, one named @reason with the value S and the other named @exit value with the value V.
6
7
8
9 10 11
12
b) c) d)
Create a list of arguments from the argument as described in §11.2.1. Let L be the list. Call B with L as described in §11.2.2. The value of the yield-with-argument is the value of the block call.
13
14
15
11.3 Operator expressions
16
Syntax operator-expression :: assignment-expression | defined?-without-parentheses | conditional-operator-expression
17 18 19 20
21
11.3.1 Syntax
Assignments
22
23 24 25
assignment ::= assignment-expression | assignment-statement assignment-expression :: single-assignment-expression | abbreviated-assignment-expression | assignment-with-rescue-modifier assignment-statement :: single-assignment-statement 51
26 27 28 29
30 31
1 2
| abbreviated-assignment-statement | multiple-assignment-statement
3
Semantics Assignments create or update variable bindings, or invoke a method whose name ends with =. Evaluation of each construct is described below. 11.3.1.1 Syntax Single assignments
4
5
6
7
8 9 10
single-assignment ::= single-assignment-expression | single-assignment-statement single-assignment-expression :: single-variable-assignment-expression | scoped-constant-assignment-expression | single-indexing-assignment-expression | single-method-assignment-expression single-assignment-statement :: single-variable-assignment-statement | scoped-constant-assignment-statement | single-indexing-assignment-statement | single-method-assignment-statement
11 12 13 14 15
16 17 18 19 20
21
11.3.1.1.1 Syntax
Single variable assignments
22
23 24 25
single-variable-assignment ::= single-variable-assignment-expression | single-variable-assignment-statement single-variable-assignment-expression :: variable [ no line-terminator here ] = operator-expression single-variable-assignment-statement :: variable [ no line-terminator here ] = method-invocation-without-parentheses scoped-constant-assignment ::= scoped-constant-assignment-expression 52
26 27
28 29
30 31
1
| scoped-constant-assignment-statement scoped-constant-assignment-expression :: primary-expression [ no whitespace here ] :: constant-identifier [ no line-terminator here ] = operator-expression | :: constant-identifier [ no line-terminator here ] = operator-expression scoped-constant-assignment-statement :: primary-expression [ no whitespace here ] :: constant-identifier [ no line-terminator here ] = method-invocation-without-parentheses | :: constant-identifier [ no line-terminator here ] = method-invocation-without-parentheses
2 3 4 5
6 7 8 9
10
Semantics A single-variable-assignment is evaluated as follows: a) Evaluate the operator-expression or the method-invocation-without-parentheses. Let V be the resulting value.
11
12 13
14
b)
If the variable is a constant-identifier : 1) 2) Let N be the constant-identifier. If a binding with name N exists in the set of bindings of constants of the current class or module, replace the value of the binding with V. Otherwise, create a variable binding with name N and value V in the set of bindings of constants of the current class or module.
15
16 17
18 19
3)
20
If the variable is a global-variable-identifier : 1) 2) Let N be the global-variable-identifier. If a binding with name N exists in [global-variable-bindings] , replace the value of the binding with V. Otherwise, create a variable binding with name N and value V in [global-variablebindings] .
21
22 23
24 25
3)
26
If the variable is a class-variable-identifier : 1) Let C be the first class or module in the list at the top of [class-module-list] which is not an eigenclass. Let CS be the set of classes which consists of C and all the superclasses of C. Let MS be the set of modules which consists of all the modules in the included module lists of all classes in CS. Let CM be the union of CS and MS. Let N be the class-variable-identifier. 53
27 28
29 30 31
32
1 2
2)
If one of the classes or modules in CM has a binding with name N in the set of bindings of class variables, let B be that binding. If more than one class or module in CM has bindings with name N in the set of bindings of class variables, let B be one of those bindings. Which binding is selected is implementation defined. Replace the value of B with V.
3 4 5
6
7 8 9
3)
If none of the classes or modules in CM has a binding with name N in the set of bindings of class variables, create a variable binding with name N and value V in the set of bindings of class variables of C.
10
If the variable is an instance-variable-identifier : 1) 2) Let N be the instance-variable-identifier. If a binding with name N exists in the set of bindings of instance variables of the current self, replace the value of the binding with V. Otherwise, create a variable binding with name N and value V in the set of bindings of instance variables of the current self.
11
12 13
14 15
3)
16
If the variable is a local-variable-identifier : 1) 2) 3) 4) Let N be the local-variable-identifier. Search for a binding of a local variable with name N as described in §9.1.1. If a binding is found, replace the value of the binding with V. Otherwise, create a variable binding with name N and value V in the current set of local variable bindings.
17
18
19
20 21
22
c)
The value of the single-variable-assignment is V.
23
A scoped-constant-assignment is evaluated as follows: a) If the primary-expression occurs, evaluate it and let M be the resulting value. Otherwise, let M be the class Object. If M is an instance of the class Module: 1) 2) Let N be the constant-identifier. Evaluate the operator-expression or the method-invocation-without-parentheses. Let V be the resulting value. Create a variable binding with name N and value V in the set of bindings of constants of M. The value of the scoped-constant-assignment is V.
24 25
26
b)
27
28 29
30 31
3)
32
4) 54
1
c)
If M is not an instance of the class Module, raise a direct instance of the class TypeError. Single indexing assignments
2
11.3.1.1.2 Syntax
3
4 5 6
single-indexing-assignment ::= single-indexing-assignment-expression | single-indexing-assignment-statement single-indexing-assignment-expression :: primary-expression [ no line-terminator here ] [ indexing-argument-list ? ] [ no line-terminator here ] = operator-expression single-indexing-assignment-statement :: primary-expression [ no line-terminator here ] [ indexing-argument-list ? ] [ no line-terminator here ] = method-invocation-without-parentheses
7 8 9
10 11 12
13
Semantics A single-indexing-assignment is evaluated as follows: a) b) Evaluate the primary-expression. Let O be the resulting value. Construct a list of arguments from the indexing-argument-list as described in §11.2.1. Let L be the resulting list. Evaluate the operator-expression or method-invocation-without-parentheses. Let V be the resulting value. Append V to L. Invoke the method []= on O with L as the list of arguments. The value of the single-indexing-assignment is V. Single method assignments
14
15
16 17
18 19
c)
20
d) e) f)
21
22
23
11.3.1.1.3 Syntax
24
25 26 27
single-method-assignment ::= single-method-assignment-expression | single-method-assignment-statement single-method-assignment-expression :: primary-expression [ no line-terminator here ] ( . | :: ) local-variable-identifier [ no line-terminator here ] = operator-expression 55
28 29 30
1 2
| primary-expression [ no line-terminator here ] . constant-identifier [ no line-terminator here ] = operator-expression single-method-assignment-statement :: primary-expression [ no line-terminator here ] ( . | :: ) local-variable-identifier [ no line-terminator here ] = method-invocation-without-parentheses | primary-expression [ no line-terminator here ] . constant-identifier [ no line-terminator here ] = method-invocation-without-parentheses
3 4 5 6 7
8
Semantics A single-method-assignment is evaluated as follows: a) b) Evaluate the primary-expression. Let O be the resulting value. Evaluate the operator-expression or method-invocation-without-parentheses. Let V be the resulting value. Let M be the local-variable-identifier or constant-identifier. Let N be the concatenation of M and =. Invoke the method whose name is N on O with a list of arguments which contains only one value V. The value of the single-method-assignment is V. Abbreviated assignments
9
10
11 12
13 14
c)
15 16
d)
17
e)
18
11.3.1.2 Syntax
19
20 21 22
abbreviated-assignment ::= abbreviated-assignment-expression | abbreviated-assignment-statement abbreviated-assignment-expression :: abbreviated-variable-assignment-expression | abbreviated-indexing-assignment-expression | abbreviated-method-assignment-expression abbreviated-assignment-statement :: abbreviated-variable-assignment-statement | abbreviated-indexing-assignment-statement | abbreviated-method-assignment-statement
23 24 25 26
27 28 29 30
31
11.3.1.2.1 Syntax 56
Abbreviated variable assignments
32
1 2 3
abbreviated-variable-assignment ::= abbreviated-variable-assignment-expression | abbreviated-variable-assignment-statement abbreviated-variable-assignment-expression :: variable [ no line-terminator here ] assignment-operator operator-expression abbreviated-variable-assignment-statement :: variable [ no line-terminator here ] assignment-operator method-invocation-without-parentheses
4 5
6 7 8
9
Semantics An abbreviated-variable-assignment is evaluated as follows: a) b) Evaluate the variable as a variable reference (see §11.4.3). Let V be the resulting value. Evaluate the operator-expression or the method-invocation-without-parentheses. Let W be the resulting value. Let OP be the assignment-operator-name of the assignment-operator. Evaluate the operator-expression of the form L OP R, where the value of L is V and the value of R is W. Let X be the resulting value. Let I be the variable of the abbreviated-variable-assignment-expression or the abbreviatedvariable-assignment-statement. Evaluate a single-variable-assignment-expression (see §11.3.1.1.1) where its variable is I and the value of the operator-expression is X. The value of the abbreviated-variable-assignment is X. Abbreviated indexing assignments
10
11
12 13
14
c) d)
15 16
17 18
e)
19 20
f)
21
g)
22
11.3.1.2.2 Syntax
23
24 25 26
abbreviated-indexing-assignment ::= abbreviated-indexing-assignment-expression | abbreviated-indexing-assignment-statement abbreviated-indexing-assignment-expression :: primary-expression [ no line-terminator here ] [ indexing-argument-list ? ] [ no line-terminator here ] assignment-operator operator-expression
27 28 29
57
1 2 3
abbreviated-indexing-assignment-statement :: primary-expression [ no line-terminator here ] [ indexing-argument-list ? ] [ no line-terminator here ] assignment-operator method-invocation-without-parentheses
4
Semantics An abbreviated-indexing-assignment is evaluated as follows: a) b) Evaluate the primary-expression. Let O be the resulting value. Construct a list of arguments from the indexing-argument-list as described in §11.2.1. Let L be the resulting list. Invoke the method [] on O with L as the list of arguments. Let V be the resulting value. Evaluate the operator-expression or method-invocation-without-parentheses. Let W be the resulting value. Let OP be the assignment-operator-name of the assignment-operator. Evaluate the operator-expression of the form V OP W. Let X be the resulting value. Append X to L. Invoke the method []= on O with L as the list of arguments. The value of the abbreviated-indexing-assignment is X. Abbreviated method assignments
5
6
7 8
9
c) d)
10 11
12
e) f) g) h) i)
13
14
15
16
17
11.3.1.2.3 Syntax
18
19 20 21
abbreviated-method-assignment ::= abbreviated-method-assignment-expression | abbreviated-method-assignment-statement abbreviated-method-assignment-expression :: primary-expression [ no line-terminator here ] ( . | :: ) local-variable-identifier [ no line-terminator here ] assignment-operator operator-expression | primary-expression [ no line-terminator here ] . constant-identifier [ no line-terminator here ] assignment-operator operator-expression abbreviated-method-assignment-statement :: primary-expression [ no line-terminator here ] ( . | :: ) local-variable-identifier [ no line-terminator here ] assignment-operator method-invocation-without-parentheses | primary-expression [ no line-terminator here ] . constant-identifier [ no line-terminator here ] assignment-operator method-invocation-without-parentheses
22 23 24 25 26
27 28 29 30 31
58
1
Semantics An abbreviated-method-assignment is evaluated as follows: a) b) Evaluate the primary-expression. Let O be the resulting value. Create an empty list of arguments L. Invoke the method whose name is the local-variableidentifier on O with L as the list of arguments. Let V be the resulting value. Evaluate the operator-expression or method-invocation-without-parentheses. Let W be the resulting value. Let OP be the assignment-operator-name of the assignment-operator. Evaluate the single-method-assignment of the form V OP W. Let X be the resulting value. Let M be the local-variable-identifier or the constant-identifier. Let N be the concatenation of M and =. Invoke the method whose name is N on O with X as the argument. The value of the abbreviated-method-assignment is X. Multiple assignments
2
3
4 5
6 7
c)
8
d) e) f)
9
10 11
12
g) h)
13
14
11.3.1.3 Syntax
15
16 17 18 19
multiple-assignment-statement :: many-to-one-assignment-statement | one-to-packing-assignment-statement | many-to-many-assignment-statement many-to-one-assignment-statement :: left-hand-side [ no line-terminator here ] = multiple-right-hand-side one-to-packing-assignment-statement :: packing-left-hand-side [ no line-terminator here ] = ( method-invocation-without-parentheses | operator-expression ) many-to-many-assignment-statement :: multiple-left-hand-side [ no line-terminator here ] = multiple-right-hand-side | ( multiple-left-hand-side but not packing-left-hand-side ) [ no line-terminator here ] = ( method-invocation-without-parentheses | operator-expression ) left-hand-side :: variable | primary-expression [ no line-terminator here ] [ indexing-argument-list ? ] | primary-expression [ no line-terminator here ] 59
20 21
22 23 24
25 26 27 28 29
30 31 32 33
1 2
( . | :: ) ( local-variable-identifier | constant-identifier ) | :: constant-identifier multiple-left-hand-side :: ( multiple-left-hand-side-item , )+ multiple-left-hand-side-item ? | ( multiple-left-hand-side-item , )+ packing-left-hand-side ? | packing-left-hand-side | grouped-left-hand-side packing-left-hand-side :: * left-hand-side ? grouped-left-hand-side :: ( multiple-left-hand-side ) multiple-left-hand-side-item :: left-hand-side | grouped-left-hand-side multiple-right-hand-side :: operator-expression-list ( , splatting-right-hand-side )? | splatting-right-hand-side splatting-right-hand-side :: splatting-argument
3 4 5 6 7
8 9
10 11
12 13 14
15 16 17
18 19
20 21
Any of the operator-expressions in a multiple-assignment-statement or splatting-right-hand-side shall not be a jump-expression. Semantics A many-to-one-assignment-statement is evaluated as follows: a) Construct a list of values from the multiple-right-hand-side (see below). Let L be the resulting list. If the length of L is 0 or 1, let A be an implementation defined value. If the length of L is larger than 1, create a direct instance of the class Array and store the elements of L in it, preserving their order. Let A be the instance of the class Array. Evaluate a single-variable-assignment-expression (see §11.3.1.1.1) where its variable is the left-hand-side and the value of its operator-expression is A. The value of the many-to-one-assignment-statement is A.
22
23
24 25
26
b) c)
27 28
29 30
d)
31
e)
32
A list of values is constructed from a multiple-right-hand-side as follows: 60
1 2 3
a)
If the operator-expression-list occurs, evaluate its operator-expressions in the order they appear in the program text. Let L1 be a list which contains the resulting values, preserving their order. If the operator-expression-list does not occur, create an empty list of values L1. If the splatting-right-hand-side occurs, construct a list of values from its splatting-argument as described in §11.2.1 and let L2 be the resulting list. If the splatting-right-hand-side does not occur, create an empty list of values L2. The result is the concatenation of L1 and L2.
4
b) c)
5 6
7
d) e)
8
9
A one-to-packing-assignment-statement is evaluated as follows: a) Evaluate the method-invocation-without-parentheses or the operator-expression. Let V be the value. If V is an instance of the class Array, let A be an implementation defined value. If V is not an instance of the class Array, create an instance of the class Array A which contains only one value V. If the left-hand-side of the packing-left-hand-side occurs, evaluate a single-variable-assignmentexpression (see §11.3.1.1.1) where its variable is the left-hand-side and the value of the operator-expression is A. The value of the one-to-packing-assignment-statement is A.
10 11
12
b) c)
13 14
15 16 17
d)
18
e)
19
A many-to-many-assignment-statement is evaluated as follows: a) If the multiple-right-hand-side occurs, construct a list of values from it (see above) and let R be the resulting list. If the multiple-right-hand-side does not occur: 1) Evaluate the method-invocation-without-parentheses or the operator-expression. Let V be the resulting value. If V is not an instance of the class Array, the behavior is implementation dependent. Create a list of arguments R which contains all the elements of V, preserving their order.
20 21
22
b)
23 24
25
2) 3)
26 27
28
c)
Create an empty list of variables L. For each multiple-left-hand-side-item, in the order they appear in the program text, append the left-hand-side or the grouped-left-hand-side of the multiple-left-hand-side-item to L. If the packing-left-hand-side of the multiple-left-hand-side occurs, append it to L. If the multiple-left-hand-side is a grouped-left-hand-side, append the grouped-left-hand-side to L. 61
29 30
31
32 33
1
d)
For each element Li of L, in the same order in L, take the following steps:
2
Let i be the index of Li within L. Let NR be the number of elements of R. If Li is a left-hand-side: 1) 2) 3) If i is larger than NR , let V be nil. Otherwise, let V be the i th element of R. Evaluate the single-variable-assignment of the form Li = V.
3
4
5
6
7
If Li is a packing-left-hand-side and its left-hand-side occurs: 1) If i is larger than NR , create an empty direct instance of the class Array. Let A be the instance. Otherwise, create a direct instance of the class Array which contains elements in R whose index is equal to, or larger than i, in the same order they are store in R. let A be the instance. Evaluate a single-variable-assignment-expression (see §11.3.1.1.1) where its variable is the left-hand-side and the value of the operator-expression is A.
8 9
10 11 12
2)
13 14
3)
15
If Li is a grouped-left-hand-side: 1) 2) 3) If i is larger than NR , let V be nil. Otherwise, let V be the i th element of R. Evaluate a many-to-many-assignment-statement where its multiple-left-hand-side is the multiple-left-hand-side of the grouped-left-hand-side and its multiple-righthand-side is V. Assignments with rescue modifiers
16
17
18 19 20
21
11.3.1.4 Syntax
22
23 24 25
assignment-with-rescue-modifier :: left-hand-side [ no line-terminator here ] = operator-expression 1 rescue operator-expression 2
26
Semantics An assignment-with-rescue-modifier is evaluated as follows: a) Evaluate the operator-expression 1 . Let V be the resulting value. If an exception is raised and not handled during the evaluation of the operator-expression 1 , and if the exception is an instance of the class StandardError, evaluate the operatorexpression 2 and let V be the resulting value. 62
27
28
29 30 31
1 2 3
b)
Evaluate a single-variable-assignment-expression (see §11.3.1.1.1) where its variable is the left-hand-side and the value of the operator-expression is V. The value of the assignmentwith-rescue-modifier is the resulting value of the evaluation. Unary operators
4
11.3.2 Syntax
5
6 7 8
unary-minus-expression :: power-expression 1 | - power-expression 2 unary-expression :: primary-expression | logical-NOT-with-unary-expression | ~ unary-expression 1 | + unary-expression 2
9 10 11 12 13
14 15
If a unary-minus-expression of the form - power-expression 2 is the first argument (see §11.2.1), whitespaces shall not occur between its - and power-expression 2 . If a unary-expression of the form + unary-expression 2 is the first argument (see §11.2.1), whitespaces shall not occur between its + and unary-expression 2 . Semantics See §11.1 for logical-NOT-with-unary-expression. An unary-expression of the form ~ unary-expression 1 is evaluated as follows: a) b) Evaluate the unary-expression 1 . Let X be the resulting value. Create an empty list of arguments L. Invoke the method ~ on X with L as the list of arguments. The value of the unary-expression is the resulting value of the invocation.
16 17
18
19
20
21
22 23
24
An unary-expression of the form + unary-expression 2 is evaluated as follows: a) b) Evaluate the unary-expression 2 . Let X be the resulting value. Create an empty list of arguments L. Invoke the method +@ on X with L as the list of arguments. The value of the unary-expression is the resulting value of the invocation. If the unary-expression 2 is a numeric-literal (see §8.5.5.1), instead of the above process, a conforming processor may evaluate the unary-expression to the value of the numeric-literal.
25
26 27
28 29
c)
30
An unary-expression of the form - power-expression 2 is evaluated as follows: a) Evaluate the power-expression 2 . Let X be the resulting value. 63
31
1 2
b)
Create an empty list of arguments L. Invoke the method -@ on X with L as the list of arguments. The value of the unary-expression is the resulting value of the invocation. The defined? expression
3
11.3.2.1 Syntax
4
5 6 7
defined?-expression ::= defined?-with-parentheses | defined?-without-parentheses defined?-with-parentheses :: defined? ( expression ) defined?-without-parentheses :: defined? operator-expression
8 9
10 11
12
Semantics A defined?-expression is evaluated as follows: a) If the defined?-expression is a defined?-with-parentheses, let E be the expression. If the defined?-expression is a defined?-without-parentheses, let E be the operator-expression. b)
13
14
15
16
If E is a constant-identifier : 1) Search for a binding of a constant with name E with the same evaluation steps for constant-identifier as described in §11.4.3.1. However, a direct instance of the class NameError shall not be raised when a binding is not found. If a binding is found, the value of the defined?-expression is an implementation defined value, which shall be a true value. Otherwise, the value of the defined?-expression is nil.
17 18 19
20 21
2)
22
3)
23
If E is a global-variable-identifier : — If a binding with name E exists in [global-variable-bindings] , the value of the defined?-expression is an implementation defined value, which shall be a true value. — Otherwise, the value of the defined?-expression is nil.
24 25
26
27
If E is a class-variable-identifier : 1) Let C be the current class or module. Let CS be the set of classes which consists of C and all the superclasses of C. Let MS be the set of modules which consists of all the modules in the included module lists of all classes in CS. Let CM be the union of CS and MS.
28 29 30 31
64
1 2 3
2)
If any of the classes or modules in CM has a binding with name E in the set of bindings of class variables, the value of the defined?-expression is an implementation defined value, which shall be a true value. Otherwise, the value of the defined?-expression is nil.
4
3)
5
If E is an instance-variable-identifier : — If a binding with name E exists in the set of bindings of instance variables of the current self, the value of the defined?-expression is an implementation defined value, which shall be a true value. — Otherwise, the value of the defined?-expression is nil.
6 7 8
9
10
If E is a local-variable-identifier : 1) If the local-variable-identifier is a reference (see §9.1.2), the value of the defined?expression is an implementation defined value, which shall be a true value. Otherwise, search for a method binding with name E, starting from the current class or module as described in §13.3.4. — If the binding is found and its value is not undef, the value of the defined?expression is an implementation defined value, which shall be a true value. — Otherwise, the value of the defined?-expression is nil.
11 12
13 14
2)
15 16
17
18
Otherwise, the value of the defined?-expression is implementation defined. Binary operators
19
11.3.3 Syntax
20
21 22 23 24 25 26 27 28
equality-expression :: relational-expression | relational-expression | relational-expression | relational-expression | relational-expression | relational-expression | relational-expression
<=> relational-expression == relational-expression === relational-expression != relational-expression =~ relational-expression !~ relational-expression
29 30 31 32 33 34
relational-expression :: bitwise-OR-expression | relational-expression > bitwise-OR-expression | relational-expression >= bitwise-OR-expression | relational-expression < bitwise-OR-expression | relational-expression <= bitwise-OR-expression bitwise-OR-expression :: bitwise-AND-expression 65
35 36
1 2
| bitwise-OR-expression | bitwise-AND-expression | bitwise-OR-expression ^ bitwise-AND-expression bitwise-AND-expression :: bitwise-shift-expression | bitwise-AND-expression whitespace-before-operator ? & bitwise-shift-expression bitwise-shift-expression :: additive-expression | bitwise-shift-expression whitespace-before-operator ? << additive-expression | bitwise-shift-expression >> additive-expression additive-expression :: multiplicative-expression | additive-expression whitespace-before-operator ? + multiplicative-expression | additive-expression whitespace-before-operator ? - multiplicative-expression multiplicative-expression :: unary-minus-expression | multiplicative-expression whitespace-before-operator ? * unary-minus-expression | multiplicative-expression whitespace-before-operator ? / unary-minus-expression | multiplicative-expression whitespace-before-operator ? % unary-minus-expression power-expression :: unary-expression | - ( numeric-literal ) ** power-expression | unary-expression ** power-expression binary-operator ::= <=> | == | === | =~ | > | >= | < | <= | | | ^ | & | << | >> | + | - | * | / | % | **
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
If a whitespace-before-operator occurs, whitespaces shall not occur between the operator after the whitespace-before-operator (i.e. &, <<, +, -, *, /, or %) and the nonterminal after that operator. Semantics An operator-expression of the form x != y is evaluated as follows: a) b) c) Evaluate x. Let X be the resulting value. Evaluate y. Let Y be the resulting value. Invoke the method == on X with a list of arguments which contains only one value Y. If the resulting value is a true value, the value of the operator-expression is false. Otherwise, the value of the operator-expression is true.
28
29
30
31
32 33 34
35
An operator-expression of the form x !~ y is evaluated as follows: 66
1
a) b) c)
Evaluate x. Let X be the resulting value. Evaluate y. Let Y be the resulting value. Invoke the method =~ on X with a list of arguments which contains only one value Y. If the resulting value is a true value, the value of the operator-expression is false. Otherwise, the value of the operator-expression is true.
2
3 4 5
6 7
A conforming processor may include the operators != and !~ in binary-operator. In this case, operator-expressions of the form x != y or x !~ y are evaluated as described below. An operator-expression of the form x binary-operator y is evaluated as follows: a) b) c) Evaluate x. Let X be the resulting value. Evaluate y. Let Y be the resulting value. Invoke the method whose name is the binary-operator on X with a list of arguments which contains only one value Y. The value of the operator-expression is the resulting value of the invocation.
8
9
10
11 12 13
14
11.4 Primary expressions
15
Syntax
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
primary-expression :: class-definition | eigenclass-definition | module-definition | method-definition | singleton-method-definition | yield-with-optional-argument | if-expression | unless-expression | case-expression | while-expression | until-expression | for-expression | return-without-argument | break-without-argument | next-without-argument | redo-expression | retry-expression | rescue-expression | grouping-expression | variable-reference | scoped-constant-reference | array-constructor | hash-constructor | literal | defined?-with-parentheses | primary-method-invocation 67
1
Semantics See §13.2.2 for class-definition. See §13.4.2 for eigenclass-definition. See §13.1.2 for module-definition. See §13.3.1 for method-definition. See §13.4.3 for singleton-method-definition. See §11.2.4 for yield-with-optional-argument. See §11.3.2.1 for defined?-with-parentheses. See §11.2 for primary-method-invocation. 11.4.1 11.4.1.1 11.4.1.1.1 Syntax Control structures Conditional expressions The if expression
2
3
4
5
6
7
8
9
10
11
12
13
14 15
if-expression :: if expression then-clause elsif-clause * else-clause ? end then-clause :: separator compound-statement | separator ? then compound-statement else-clause :: else compound-statement elsif-clause :: elsif expression then-clause
16 17 18
19 20
21 22
23
The expression of an if-expression or elsif-clause shall not be a jump-expression. Semantics The if-expression is evaluated as follows: a) 68 Evaluate expression. Let V be the resulting value.
24
25
26
1 2 3
b)
If V is a true value, evaluate the compound-statement of the then-clause. The value of the if-expression is the resulting value. In this case, elsif-clauses and the else-clause, if any, are not evaluated. If V is a false value, and if there is no elsif-clause and no else-clause, then the value of the if-expression is nil. If V is a false value, and if there is no elsif-clause but there is an else-clause, then evaluate the compound-statement of the else-clause. The value of the if-expression is the resulting value. If V is a false value, and if there are one or more elsif-clauses, evaluate the sequence of elsif-clauses as follows: 1) Evaluate the expression of each elsif-clause in the order they appear in the program text, until there is an elsif-clause for which expression evaluates to a true value. Let T be this elsif-clause. If T exists, evaluate the compound-expression of its then-clause. The value of the ifexpression is the resulting value. Other elsif-clauses and an else-clause following T, if any, are not evaluated. If T does not exist, and if there is an else-clause, then evaluate the compound-statement of the else-clause. The value of the if-expression is the resulting value. If T does not exist, and if there is no else-clause, then the value of the if-expression is nil. The unless expression
4 5
c)
6 7 8
d)
9 10
e)
11 12 13
14 15 16
2)
17 18
3)
19 20
4)
21
11.4.1.1.2 Syntax
22
23 24
unless-expression :: unless expression then-clause else-clause ? end
25
The expression of an unless-expression shall not be a jump-expression. Semantics The unless-expression is evaluated as follows: a) b) Evaluate the expression. Let V be the resulting value. If V is a false value, evaluate the compound-statement of the then-clause. The value of the unless-expression is the resulting value. In this case, the else-clause, if any, is not evaluated. If V is a true value, and if there is no else-clause, then the value of the unless-expression is nil. If V is a true value, and if there is an else-clause, then evaluate the compound-statement of the else-clause. The value of the unless-expression is the resulting value. 69
26
27
28
29 30
31 32
c)
33 34
d)
1
11.4.1.1.3 Syntax
The case expression
2
3 4 5
case-expression :: case-expression-with-expression | case-expression-without-expression case-expression-with-expression :: case expression separator-list ? when-clause + else-clause ? end case-expression-without-expression :: case separator-list ? when-clause + else-clause ? end when-clause :: when when-argument then-clause when-argument :: operator-expression-list ( , splatting-argument )? | splatting-argument
6 7
8 9
10 11
12 13 14
15
The expression of a case-expression-with-expression shall not be a jump-expression. Semantics A case-expression is evaluated as follows: a) If the case-expression is a case-expression-with-expression, evaluate the expression. Let V be the resulting value. The meaning of the phrase “O is matching” in this step is defined as follows:
16
17
18 19
20
b)
21 22 23
If the case-expression is a case-expression-with-expression, invoke the method === on O with a list of arguments which contains only one value V. O is matching if and only if the resulting value is a true value. If the case-expression is a case-expression-without-expression, O is matching if and only if O is a true value.
24 25
26 27
Search the when-clauses in the order they appear in the program text for a matching whenclause as follows: 1) If the operator-expression-list of the when-argument occurs:
28
29 30
For each of its operator-expressions, evaluate it and test if the resulting value is matching. If a matching value is found, other operator-expressions, if any, are not evaluated.
31
70
1
2)
If no matching value is found, and the splatting-argument occurs:
2 3
Construct a list of values from it as described in §11.2.1. For each element of the resulting list, in the indexing order, test if it is matching. If a matching value is found, other values, if any, are not evaluated.
4
5 6
3)
A when-clause is considered to be matching if and only if a matching value is found in its when-argument. Later when-clauses, if any, are not tested in this case.
7 8
c)
If one of the when-clauses is matching, evaluate the compound-statement of the then-clause of this when-clause. The value of the case-expression is the resulting value. If none of the when-clauses is matching, and if there is an else-clause, then evaluate the compound-statement of the else-clause. The value of the case-expression is the resulting value. Otherwise, the value of the case-expression is nil. Conditional operator
9 10 11
d)
12
e)
13
11.4.1.1.4 Syntax
14
15 16 17
conditional-operator-expression :: range-constructor | range-constructor ? operator-expression 1 : operator-expression 2
18
Semantics A conditional-operator-expression of the form range-constructor ? operator-expression 1 : operatorexpression 2 is evaluated as follows: a) b) Evaluate the range-constructor. If the resulting value is a true value, evaluate the operator-expression 1 . The value of the conditional-operator-expression is the resulting value of the evaluation. Otherwise, evaluate the operator-expression 2 . The value of the conditional-operator-expression is the resulting value. Iteration expressions
19 20
21
22 23
24 25
c)
26
11.4.1.2 Syntax
27
28 29 30 31 32 33
iteration-expression ::= while-expression | until-expression | for-expression | while-modifier-statement | until-modifier-statement 71
1 2 3
Each iteration-expression has a body. The body of a while-expression or an until-expression is its compound-statement. The body of a while-modifier-statement or an until-modifier-statement is its statement. See §12.4 for while-modifier-statement. See §12.5 for until-modifier-statement. 11.4.1.2.1 Syntax The while expression
4
5
6
7
8 9
while-expression :: while expression do-clause end do-clause :: separator compound-statement | do compound-statement
10 11 12
13
The expression of a while-expression shall not be a jump-expression. Semantics A while-expression is evaluated as follows: a) b) Evaluate the expression. Let V be the resulting value. If V is a false value, terminate the evaluation of the while-expression. The value of the while-expression is nil. Otherwise, evaluate the compound-statement of the do-clause. If this evaluation: 1) is terminated by a break-expression, terminate the evaluation of the while-expression. If the jump-argument of the break-expression occurs, the value of the while-expression is the value of the jump-argument. Otherwise, the value of the while-expression is nil. 2) 3) is terminated by a next-expression, continue processing from Step a. is terminated by a redo-expression, continue processing from Step c.
14
15
16
17 18
19
c)
20
21 22
23
24
25 26
Otherwise, unless this evaluation is terminated by a return-expression, continue processing from Step a. 11.4.1.2.2 Syntax 72 The until expression
27
28
1 2
until-expression :: until expression do-clause end
3
The expression of an until-expression shall not be a jump-expression. Semantics An until-expression is evaluated as follows: a) b) Evaluate the expression. Let V be the resulting value. If V is a true value, terminate the evaluation of the until-expression. The value of the until-expression is nil. Otherwise, evaluate the compound-statement of the do-clause. If this evaluation: 1) is terminated by a break-expression, terminate the evaluation of the until-expression. If the jump-argument of the break-expression occurs, the value of the until-expression is the value of the jump-argument. Otherwise, the value of the until-expression is nil. 2) 3) is terminated by a next-expression, continue processing from Step a. is terminated by a redo-expression, continue processing from Step c.
4
5
6
7 8
9
c)
10
11 12
13
14
15 16
Otherwise, unless this evaluation is terminated by a return-expression, continue processing from Step a. 11.4.1.2.3 Syntax The for expression
17
18
19 20
for-expression :: for for-variable in expression do-clause end for-variable :: left-hand-side | multiple-left-hand-side
21 22 23
24
The expression of a for-expression shall not be a jump-expression. Semantics A for-expression is evaluated as follows: a) Evaluate the expression. Let O be the resulting value. 73
25
26
27
1 2 3 4
b)
Let E be the primary-method-invocation of the form primary-expression [no line-terminator here] . each do | block-formal-argument-list | block-body end, where the value of the primary-expression is O, the block-formal-argument-list is the for-variable, the block-body is the compound-statement of the do-clause. Evaluate E, but skip Step c of §11.2.2.
5
6
c)
The value of the for-expression is the resulting value of the invocation. Jump expressions
7
11.4.1.3 Syntax
8
9 10 11 12 13 14
jump-expression ::= return-expression | break-expression | next-expression | redo-expression | retry-expression
15
Semantics Jump expressions are used to terminate the evaluation of a method-body, a block-body, the body of an iteration-expression, or the compound-statement 2 of a rescue-clause. In this document, the current block or the current iteration-expression refers to either of the following:
16 17
18 19
20 21 22 23
If the current method invocation exists, the current block or the current iteration-expression is the block or the iteration-expression whose evaluation started most recently among the block s or iteration-expressions which are being evaluated on during the evaluation of the current method invocation. Otherwise, the current block or the current iteration-expression is the block or the iterationexpression whose evaluation started most recently among the block s or iteration-expressions which are under evaluation. The return expression
24 25 26
27
11.4.1.3.1 Syntax
28
29 30 31
return-expression ::= return-without-argument | return-with-argument return-without-argument :: return
32 33
74
1 2
return-with-argument :: return jump-argument jump-argument :: argument
3 4
5 6
The block-argument of the argument-in-parentheses of the argument (see §11.2.1) of a jumpargument shall not occur. Semantics A return-expression is evaluated as follows: a) Let M be the method-body which corresponds to the current method invocation. If such an invocation does not exist, or has already terminated: 1) 2) Let S be a direct instance of the class Symbol with name return. If the jump-argument of the return-expression occurs, let V be the value of the jumpargument. Otherwise, let V be nil. Raise a direct instance of the class LocalJumpError which has two instance variable bindings, one named @reason with the value S and the other named @exit value with the value V.
7
8
9 10
11
12 13
14 15 16
3)
17
b) c)
Evaluate the jump-argument, if any, as described below. If there are block-bodys which include the return-expression and are included in M, terminate the evaluations of such block-bodys, from innermost to outermost (see §11.2.2). Terminate the evaluation of M (see §13.3.3).
18 19
20
d)
21
A jump-argument is evaluated as follows: a) If the jump-argument is a splatting-argument: 1) Construct a list of values from the splatting-argument as described in §11.2.1 and let L be the resulting list. If the length of L is 0 or 1, the value of the jump-argument is an implementation defined value. If the length of L is larger than 1, create a direct instance of the class Array and store the elements of L in it, preserving their order. The value of the jump-argument is the instance of the class Array.
22
23 24
25 26
2)
27 28 29
3)
30
b)
Otherwise: 1) Construct a list of values from the argument as described in §11.2.1 and let L be the resulting list. 75
31 32
1
2) 3)
If the length of L is 1, the value of the jump-argument is the only element of L. If the length of L is larger than 1, create a direct instance of the class Array and store the elements of L in it, preserving their order. The value of the jump-argument is the instance of the class Array. The break expression
2 3 4
5
11.4.1.3.2 Syntax
6
7 8 9
break-expression ::= break-without-argument | break-with-argument break-without-argument :: break break-with-argument :: break jump-argument
10 11
12 13
14
Semantics A break-expression is evaluated as follows: a) b) Evaluate the jump-argument, if any, as described in §11.4.1.3.1. Let E be the current block or the current iteration-expression. If such a block or an iterationexpression does not exist: 1) 2) Let S be a direct instance of the class Symbol with name break. If the jump-argument of the break-expression occurs, let V be the value of the jumpargument. Otherwise, let V be nil. Raise a direct instance of the class LocalJumpError which has two instance variable bindings, one named @reason with the value S and the other named @exit value with the value V.
15
16
17 18
19
20 21
22 23 24
3)
25
c) d)
If E is a block, terminate the evaluation of the block-body of E (see §11.2.2). If E is an iteration-expression, terminate the evaluation of the body of E (see §11.4.1.2). The next expression
26
27
11.4.1.3.3 Syntax
28
29 30 31
next-expression ::= next-without-argument | next-with-argument 76
1 2
next-without-argument :: next next-with-argument :: next jump-argument
3 4
5
Semantics A next-expression is evaluated as follows: a) b) Evaluate the jump-argument, if any, as described in §11.4.1.3.1. Let E be the current block or the current iteration-expression. If such a block or an iterationexpression does not exist: 1) 2) Let S be a direct instance of the class Symbol with name next. If the jump-argument of the next-expression occurs, let V be the value of the jumpargument. Otherwise, let V be nil. Raise a direct instance of the class LocalJumpError which has two instance variable bindings, one named @reason with the value S and the other named @exit value with the value V.
6
7
8 9
10
11 12
13 14 15
3)
16
c) d)
If E is a block, terminate the evaluation of the block-body of E (see §11.2.2). If E is an iteration-expression, terminate the evaluation of the body of E (see §11.4.1.2). The redo expression
17
18
11.4.1.3.4 Syntax
19
20 21
redo-expression :: redo
22
Semantics A redo-expression is evaluated as follows: a) Let E be the current block or the current iteration-expression. If such a block or an iterationexpression does not exist, 1) 2) Let S be a direct instance of the class Symbol with name redo. Raise a direct instance of the class LocalJumpError which has two instance variable bindings, one named @reason with the value S and the other named @exit value with the value nil. 77
23
24 25
26
27 28 29
1
b) c)
If E is a block, terminate the evaluation of the block-body of E (see §11.2.2). If E is an iteration-expression, terminate the evaluation of the body of E (see §11.4.1.2). The retry expression
2
3
11.4.1.3.5 Syntax
4
5 6
retry-expression :: retry
7
Semantics A retry-expression is evaluated as follows: a) If the current method invocation exists, let M be the method-body which corresponds to the current method invocation. Otherwise, let M be the program. Let E be the innermost rescue-clause in M which encloses the retry-expression. If such a rescue-clause does not exist, the behavior is implementation dependent. Terminate the evaluation of the compound-statement 2 of E (see §11.4.1.4.1). Exceptions The rescue expression
8
9 10
11 12
b)
13
c)
14
11.4.1.4 11.4.1.4.1 Syntax
15
16
17 18
rescue-expression :: begin body-statement end body-statement :: compound-statement rescue-clause * else-clause ? ensure-clause ? rescue-clause :: rescue [ no line-terminator here ] exception-class-list ? exception-variable-assignment ? then-clause exception-class-list :: operator-expression | multiple-right-hand-side exception-variable-assignment :: => left-hand-side
19 20
21 22 23
24 25 26
27 28
78
1 2
ensure-clause :: ensure compound-statement
3
The operator-expression of an exception-class-list shall not be a jump-expression. Semantics The value of a rescue-expression is the value of the body-statement. A body-statement is evaluated as follows: a) b) Evaluate the compound-statement of the body-statement. If no exception is raised, or all the raised exceptions are handled during Step a: 1) 2) If the else-clause occurs, evaluate the else-clause as described in §11.4.1.1.1. If the ensure-clause occurs, evaluate its compound-statement. The value of this evaluation is the value of the ensure-clause.
4
5
6
7
8
9
10 11
12 13 14
If both the else-clause and the ensure-clause occur, the value of the body-statement is the value of the ensure-clause. If only one of these clauses occurs, the value of the body-statement is the value of the occurring clause. If neither the else-clause nor the ensure-clause occurs, the value of the body-statement is the value of its compound-statement. c) If an exception is raised and not handled during Step a, test each rescue-clause, if any, in the order it occurs in the program text. The test determines whether the rescue-clause can handle the exception as follows: 1) 2) Let E be the exception raised. If the exception-class-list does not occur in the rescue-clause, and if E is an instance of the class StandardError, the rescue-clause handles E. If the exception-class-list of the rescue-clause occurs:
15 16
17 18 19
20
21 22
23
3)
24 25 26
If the exception-class-list is of the form operator-expression, evaluate the operatorexpression. Create an empty list of values, and append the value of the operatorexpression to the list. If the exception-class-list is of the form multiple-right-hand-side, construct a list of values from the multiple-right-hand-side (see §11.3.1.3).
27 28
29 30
Let L be the list created by evaluating the exception-class-list as above. Compare each element D of L with E as follows:
31 32
If D is neither the class Exception nor a subclass of the class Exception, raise a direct instance of the class TypeError. 79
1 2
If E is an instance of D, the rescue-clause handles E. In this case, any remaining rescue-clauses in the body-statement are not tested.
3
d)
If a rescue-clause R which can handle E is found: 1) If the exception-variable-assignment of R occurs, evaluate it in the same way as a multiple-assignment-statement of the form left-hand-side = multiple-right-hand-side where the value of multiple-right-hand-side is E. Evaluate the compound-statement of the then-clause of R. If this evaluation is terminated by a retry-expression, continue processing from Step a. Otherwise, let V be the value of this evaluation. If the ensure-clause occurs, evaluate it. The value of the body-statement is the value of the ensure-clause. If the ensure-clause does not occur, the value of the body-statement is V.
4 5 6
7 8 9
2)
10 11
3)
12
4) e)
13 14
If no rescue-clause occurs or if a rescue-clause which can handle E is not found, evaluate the ensure-clause. In this case, the value of the body-statement is undefined.
15 16
The ensure-clause of a body-statement, if any, is always evaluated, even when the evaluation of body-statement is terminated by a jump-expression. 11.4.2 Syntax Grouping expression
17
18
19 20 21
grouping-expression :: ( expression ) | ( compound-statement )
22
Semantics A grouping-expression is evaluated as follows: a) b) Evaluate the expression or the compound-statement. The value of the grouping-expression is the resulting value. Variable references
23
24
25
26
11.4.3 Syntax
27
28 29 30
variable-reference :: variable | pseudo-variable
80
1 2 3 4 5 6
variable :: constant-identifier | global-variable-identifier | class-variable-identifier | instance-variable-identifier | local-variable-identifier scoped-constant-reference :: primary-expression [ no whitespace here ] :: constant-identifier | :: constant-identifier
7 8 9
10
11.4.3.1
Constants
11
A constant-identifier is evaluated as follows: a) b) Let N be the constant-identifier. Search for a binding of a constant with name N as described below. As soon as the binding is found in any of the following steps, the evaluation of the constantidentifier is terminated and the value of the constant-identifier is the value of the binding found. c) Let L be the top of [class-module-list] . Search for a binding of a constant with name N in each element of L from start to end, including the first element, which is the current class or module, but except for the last element, which is the class Object. If a binding is not found, let C be the current class or module. Let L be the included module list of C. Search each element of L in the reverse order for a binding of a constant with name N. e) If the binding is not found:
12
13
14 15 16
17 18 19
20
d)
21 22
23
24
If C is a class: 1) 2) Let S be the direct superclass of C. If S is nil, create a direct instance of the class Symbol with name N, and let R be that instance. Invoke the method const missing on the current class or module with R as the only argument. If S is not nil, search for a binding of a constant with name N in S. If the binding is not found, let L be the included module list of S and search each element of L in the reverse order for a binding of a constant with name N. If the binding is not found, let S be the direct superclass of S. Continue processing from Step e-2. 81
25
26 27 28
29
3) 4)
30 31
32 33
5)
1
If C is a module: 1) 2) Search for a binding of a constant with name N in the class Object. If the binding is not found, let L be the included module list of the class Object and search each element of L in the reverse order for a binding of a constant with name N. If the binding is not found, create a direct instance of the class Symbol with name N, and let R be that instance. Invoke the method const missing on the current class or module with R as the only argument. Scoped constants
2
3 4 5
6 7 8
3)
9
11.4.3.2
10
A scoped-constant-reference is evaluated as follows: a) If the primary-expression occurs, evaluate it and let C be the resulting value. Otherwise, let C be the class Object. If C is not an instance of the class Module, raise a direct instance of the class TypeError. Otherwise: 1) 2) Let N be the constant-identifier. If a binding with name N exists in the set of bindings of constants of C, the value of the scoped-constant-reference is the value of the binding. Otherwise: i) Let L be the included module list of C. Search each element of L in the reverse order for a binding of a constant with name N. If the binding is found, the value of the scoped-constant-reference is the value of the binding.
11 12
13
b) c)
14
15
16 17
18
3)
19 20
21 22
ii)
23
iii) Otherwise, search for a binding of a constant with name N from Step e of §11.4.3.1. 11.4.3.3 Global variables
24
25
A global-variable-identifier is evaluated as follows:
26
Let N be the global-variable-identifier. If a binding with name N exists in [global-variable-bindings] , the value of global-variableidentifier is the value of the binding. Otherwise, the value of global-variable-identifier is nil. Class variables
27 28
29
30
11.4.3.4
31
A class-variable-identifier is evaluated as follows: 82
1 2
a)
Let N be the class-variable-identifier. Let C be the first class or module in the list at the top of [class-module-list] which is not an eigenclass. Let CS be the set of classes which consists of C and all the superclasses of C. Let MS be the set of modules which consists of all the modules in the included module list of all classes in CS. Let CM be the union of CS and MS. If a binding with name N exists in the set of bindings of class variables of only one of the classes or modules in CM, let V be the value of the binding. If more than two classes or modules in CM have a binding with name N in the set of bindings of class variables, let V be the value of one of these bindings. Which binding is selected is implementation dependent. If none of the classes or modules in CM has a binding with name N in the set of bindings of class variables, let S be a direct instance of the class Symbol with name N and raise a direct instance of the class NameError which has S as its name property. The value of the class-variable-identifier is V. Instance variables
3 4 5
b)
6 7
c)
8 9 10
d)
11 12 13
e)
14
f)
15
11.4.3.5
16
An instance-variable-identifier is evaluated as follows:
17
Let N be the instance-variable-identifier. If a binding with name N exists in the set of bindings of instance variables of the current self, the value of the instance-variable-identifier is the value of the binding. Otherwise, the value of the instance-variable-identifier is nil. Local variables
18 19
20
21
11.4.3.6
22 23
This subclause describes a local-variable-identifier which is a reference to a local variable (see §9.1.2). A local-variable-identifier is evaluated as follows: a) b) c) d) Let N be the local-variable-identifier. Search for a binding of a local variable with name N as described in §9.1.1. If a binding is found, the value of local-variable-identifier is the value of the binding. Otherwise, the value of local-variable-identifier is nil. Pseudo variables
24
25
26
27
28
29
11.4.3.7 Syntax
30
31 32
pseudo-variable :: nil 83
1 2 3
| true | false | self
4
11.4.3.7.1 Syntax
nil
5
6 7
nil :: nil
8
Semantics The pseudo variable nil evaluates to the only instance of the class NilClass (see §15.2.4). 11.4.3.7.2 Syntax true and false
9
10
11
12 13
true :: true false :: false
14 15
16
Semantics The pseudo variable true is evaluates to the only instance of the class TrueClass (see §15.2.5). The pseudo variable false is evaluates to the only instance of the class FalseClass (see §15.2.6). 11.4.3.7.3 Syntax self
17
18
19
20
21 22
self :: self
23
Semantics The pseudo variable self is evaluates to the value of the current self.
24
84
1
11.4.4 11.4.4.1 Syntax
Object constructors Array constructor
2
3
4 5
array-constructor :: [ indexing-argument-list ? ]
6
Semantics An array-constructor is evaluated as follows: a) If there is an indexing-argument-list, construct a list of arguments from the indexingargument-list as described in §11.2.1. Let L be the resulting list. Otherwise, create an empty list of values L. Create a direct instance of the class Array which stores the values in L in the same order they are stored in L. Let O be the instance. The value of the array-constructor is O. Hash constructor
7
8 9
10
b) c)
11 12
13
d)
14
11.4.4.2 Syntax
15
16 17
hash-constructor :: { ( association-list ,? )? } association-list :: association ( , association )* association :: association-key => association-value association-key :: operator-expression association-value :: operator-expression
18 19
20 21
22 23
24 25
26
The operator-expression of an association-key or association-value shall not be a jump-expression.
85
1
Semantics Both hash-constructor s or association-lists evaluate to a direct instance of the class Hash (see §15.2.13). A hash-constructor is evaluated as follows: a) If there is an association-list, evaluate the association-list. The value of the hash-constructor is the resulting value. Otherwise, create an empty direct instance of the class Hash. The value of the hashconstructor is the resulting instance.
2 3
4
5 6
7 8
b)
9
An association-list is evaluated as follows: a) b) Create a direct instance of the class Hash H. For each association Ai , in the order it appears in the program text, take the following steps: 1) Evaluate the operator-expression of the association-key of Ai . Let Ki be the resulting value. Evaluate the operator-expression of the association-value. Let Vi be the resulting value. Store a pair of Ki and Vi in H, as if by invoking the method []= on H with Ki and Vi as the arguments.
10
11 12
13 14
15
2) 3)
16 17
18
c)
The value of the association-list is H. Range constructor
19
11.4.4.3 Syntax
20
21 22 23
range-constructor :: logical-OR-expression | logical-OR-expression 1 range-operator logical-OR-expression 2 range-operator :: .. | ...
24 25 26
27
Semantics A range-constructor of the form logical-OR-expression 1 range-operator logical-OR-expression 2 is evaluated as follows: a) b) 86 Evaluate the logical-OR-expression 1 . Let A be the resulting value. Evaluate the logical-OR-expression 2 . Let B be the resulting value.
28 29
30
31
1 2
c)
If the range-operator is the terminal .., construct a list L which contains three arguments: A, B, and false. If the range-operator is the terminal ..., construct a list L which contains three arguments: A, B, and true.
3 4
5 6
d)
Invoke the method new on the class Range with L as the list of arguments. The value of the range-constructor is the resulting value. Literals
7
11.4.5
8
See §8.5.5.1 for integer literals. See §8.5.5.1 for float literals. See §8.5.5.2 for string literals. See §8.5.5.5 for symbol literals. See §8.5.5.4 for regular expression literals.
9
10
11
12
13
12 Statements
14
Syntax
15 16 17 18 19 20 21 22 23 24
statement :: expression-statement | alias-statement | undef-statement | if-modifier-statement | unless-modifier-statement | while-modifier-statement | until-modifier-statement | rescue-modifier-statement | assignment-statement
25
See §13.3.6 for alias-statement. See §13.3.7 for undef-statement. See §11.3.1 for assignment-statement. Semantics See §11.3.1 for assignment-statement.
26
27
28
29
30
12.1 The expression statement
31
Syntax 87
1 2
expression-statement :: expression
3 4
Semantics An expression-statement is evaluated as follows: a) b) Evaluate the expression. The resulting value is the value of the expression-statement.
5
6
7
12.2 The if modifier statement
8
Syntax
9 10
if-modifier-statement :: statement [ no line-terminator here ] if expression
11
The expression of an if-modifier-statement shall not be a jump-expression. Semantics An if-modifier-statement of the form S if E, where S is the statement and E is the expression, is evaluated as follows: a) b) Evaluate the if-expression of the form if E then S end. The resulting value is the value of the if-modifier-statement.
12 13 14
15
16
17
12.3 The unless modifier statement
18
Syntax
19 20
unless-modifier-statement :: statement [ no line-terminator here ] unless expression
21
The expression of an unless-modifier-statement shall not be a jump-expression. Semantics An unless-modifier-statement of the form S unless E, where S is the statement and E is the expression, is evaluated as follows: a) b) 88 Evaluate the unless-expression of the form unless E then S end. The resulting value is the value of the unless-modifier-statement.
22 23 24
25
26
1
12.4 The while modifier statement
2
Syntax
3 4
while-modifier-statement :: statement [ no line-terminator here ] while expression
5
The expression of a while-modifier-statement shall not be a jump-expression. Semantics A while-modifier-statement of the form S while E, where S is the statement and E is the expression, is evaluated as follows: a) b) Evaluate the while-expression of the form while E do S end. The resulting value is the value of the while-modifier-statement.
6
7 8
9
10
11
12.5 The until modifier statement
12
Syntax
13 14
until-modifier-statement :: statement [ no line-terminator here ] until expression
15
The expression of an until-modifier-statement shall not be a jump-expression. Semantics An until-modifier-statement of the form S until E, where S is the statement and E is the expression, is evaluated as follows: a) b) Evaluate the until-expression of the form until E do S end. The resulting value is the value of the until-modifier-statement.
16
17 18
19
20
21
12.6 The rescue modifier statement
22
Syntax
23 24 25
rescue-modifier-statement :: main-statement-of-rescue-modifier-statement [ no line-terminator here ] rescue fallback-statement-of-rescue-modifier-statement main-statement-of-rescue-modifier-statement :: statement 89
26 27
1 2
fallback-statement-of-rescue-modifier-statement :: statement but not statement-not-allowed-in-fallback-statement statements-not-allowed-in-fallback-statement :: keyword-AND-expression | keyword-OR-expression | if-modifier-statement | unless-modifier-statement | while-modifier-statement | until-modifier-statement | rescue-modifier-statement
3 4 5 6 7 8 9 10
11
Semantics A rescue-modifier-statement is evaluated as follows: a) b) Evaluate the main-statement-of-rescue-modifier-statement. Let V be the resulting value. If an instance of the class StandardError is raised and not handled in Step a, evaluate fallback-statement-of-rescue-modifier-statement. The resulting value is the value of the rescue-modifier-statement. If no instances of the class Exception are raised in Step a, or all the instances of the class Exception raised in Step a are handled in Step a, the value of the rescue-modifier-statement is V.
12
13
14 15 16
17 18 19
c)
20
13 Classes and modules
13.1 Modules
13.1.1 General description
21
22
23 24 25
Every module is an instance of the class Module (see §15.2.2). However, not every instance of the class Module is a module because the class Module is a superclass of the class Class, an instance of which is not a module, but a class. Modules have the following attributes: Included module list: An ordered list of modules included in the module. Module inclusion is described in §13.1.3. Constants: A set of bindings of constants. A binding of a constant is created by the following program constructs:
26
27 28
29
30
31
Assignments (see §11.3.1) Module definitions (see §13.1.2)
32
90
1
Class definitions (see §13.2.2)
2 3
Class variables: A set of bindings of class variables. A binding of a class variable is created by an assignment (see §11.3.1). Instance methods: A set of method bindings. A method binding is created by a method definition (see §13.3.1) or a singleton method definition (see §13.4.3). The value of a method binding may be undef , which is the flag indicating that a method cannot be invoked (see §13.3.7). 13.1.2 Syntax Module definition
4 5 6 7
8
9
10 11
module-definition :: module module-path module-body end module-path :: top-module-path | module-name | nested-module-path module-name :: constant-identifier top-module-path :: :: module-name nested-module-path :: primary-expression [ no line-terminator here ] :: module-name module-body :: body-statement
12 13 14 15
16 17
18 19
20 21
22 23
24
Semantics A module-definition is evaluated as follows: a) Determine the class or module in which a binding with name module-name is to be created or modified as follows:
25
26 27
28
If the module-path is of the form top-module-path, let C be the class Object. If the module-path is of the form module-name, let C be the current class or module. If the module-path is of the form nested-module-path, evaluate the primary-expression. If the resulting value is an instance of the class Module, let C be the instace. Otherwise, raise a direct instance of the class TypeError. 91
29
30 31 32
1
b)
Let N be the module-name. 1) If a binding with name N exists in the set of bindings of constants of C, let B be this binding. If the value of B is a module, let M be that module. Otherwise, raise a direct instance of the class TypeError. Otherwise, create a direct instance of the class Module and let M be that module. Create a variable binding with name N and value M in the set of bindings of constants of C.
2 3 4
5 6 7
2)
8
c)
Modify the execution context as follows:
9 10 11
Create a new list which has the same members as that of the list at the top of [classmodule-list] , and add M to the head of the newly created list. Push the list onto [class-module-list] . Push M onto [self ] . Push the public visibility onto [default-visibility] . Push an empty set of bindings onto [local-variable-bindings] .
12
13
14
15 16
d)
Evaluate the module-body. The value of the module-definition is the value of the modulebody. Restore the execution context by removing the elements from the tops of [class-modulelist] , [self ] , [default-visibility] , and [local-variable-bindings] , even when an exception is raised and not handled during Step d. Module inclusion
17 18 19
e)
20
13.1.3
21 22 23
Modules and classes can be extended by including other modules into them. When a module is included, the instance methods, the class variables, and the constants of the included module are available to the including class or module (see §11.4.3.4, §13.3.3, and §11.4.3.1). Modules and classes can include other modules by invoking the method include (see §15.2.2.3.27) or the method extend (see §15.3.1.2.13). A module M is included in another module N if and only if M is an element of the included module list of N. A module M is included in a class C if and only if M is an element of the included module list of C, or M is included in one of the superclasses of C.
24 25
26 27 28
29
13.2 Classes 13.2.1 General description
30
31 32
Every class is an instance of the class Class (see §15.2.3), which is a direct subclass of the class Module. Classes have the same set of attributes as modules. In addition, each class has a single direct superclass. 92
33 34
1
13.2.2 Syntax
Class definition
2
3 4
class-definition :: class class-path [ no line-terminator here ] superclass class-body end class-path :: top-class-path | class-name | nested-class-path class-name :: constant-identifier top-class-path :: :: class-name nested-class-path :: primary-expression [ no line-terminator here ] :: class-name superclass :: separator | < expression separator class-body :: body-statement
5 6 7 8
9 10
11 12
13 14
15 16 17
18 19
20
Semantics A class-definition is evaluated as follows: a) Determine the class or module in which the binding with name class-name is to be created or modified as follows:
21
22 23
24
If the class-path is of the form top-class-path, let M be the class Object. If the class-path is of the form class-name, let M be the current class or module. If the class-path is of the form nested-class-path, evaluate the primary-expression. If the resulting value is an instance of the class Module, let M be the instance. Otherwise, raise a direct instance of the class TypeError.
25
26 27 28
29
b)
Let N be the class-name. 1) If a binding with name N exists in the set of bindings of constants of M, let B be that binding. 93
30 31
1 2
i)
If the value of B is an instance of the class Class, let C be the instance. Otherwise, raise a direct instance of the class TypeError. If the superclass occurs, evaluate it. If the resulting value does not correspond to the direct superclass of C, raise a direct instance of the class TypeError.
3 4
ii)
5
2)
Otherwise, create a direct instance of the class Class. Let C be that class. i) If the superclass occurs, evaluate it. If the resulting value is not an instance of the class Class, raise a direct instance of the class TypeError. If the value of superclass is an eigenclass or the class Class, the behavior is implementation dependent. Otherwise, let the direct superclass of C be the value of the superclass. If the superclass of the class-definition does not occur, let the direct superclass of C be the class Object.
6 7 8 9
10 11
ii)
12 13
iii) Create an eigenclass, and associate it with C. The eigenclass shall have the eigenclass of the direct superclass of C as one of its superclasses. iv) Create a variables binding with name N and value C in the set of bindings of constants of M. c) Modify the execution context as follows:
14 15
16
17 18 19
Create a new list which has the same members as that of the list at the top of [classmodule-list] , and add C to the head of the newly created list. Push the list onto [class-module-list] . Push C onto [self ] . Push the public visibility onto [default-visibility] . Push an empty set of bindings onto [local-variable-bindings] .
20
21
22
23
d) e)
Evaluate the class-body. The value of the class-definition is the value of the class-body. Restore the execution context by removing the elements from the tops of [class-modulelist] , [self ] , [default-visibility] , and [local-variable-bindings] , even when an exception is raised and not handled during Step d. Inheritance
24 25 26
27
13.2.3
28 29
A class inherits attributes of its superclasses. Inheritance means that a class implicitly contains all attributes of its superclasses, as described below:
30
Constants and class variables of superclasses can be referenced (see §11.4.3.1 and §11.4.3.4). Singleton methods of superclasses can be invoked (see §13.4). Instance methods defined in superclasses can be invoked on an instance of their subclasses (see §13.3.3).
31
32 33
94
1
13.2.4
Instance creation
2 3
A direct instance of a class can be created by invoking the method new on the class (see §15.2.3.2.3).
4
13.3 Methods
13.3.1 Method definition
5
6
Syntax
7 8 9
method-definition :: def method-name [ no line-terminator here ] method-parameter-part method-body end method-name :: method-identifier | operator-method-name | reserved-word method-body :: body-statement
10 11 12 13
14 15
16
The following constructs shall not occur in the method-parameter-part or the method-body:
17
A class-definition. A module-definition. A single-variable-assignment, where its variable is a constant-identifier. A scoped-constant-assignment. A multiple-assignment-statement, where the form of any of the left-hand-sides which occurs in it is any of the following: — constant-identifier — primary-expression [no line-terminator here] (. | ::) (local-variable-identifier | constantidentifier ) — :: constant-identifier.
18
19
20
21 22
23
24 25
26
27 28
However, those constructs may occur within an eigenclass-definition in the method-parameterpart or the method-body. Semantics A method is defined by a method-definition and has the method-parameter-part and the methodbody of the method-definition. In addition, a method has the following attributes: 95
29
30 31
1 2
Class module list: The list of classes and modules which is the top element of [classmodule-list] when the method is defined. Defined name: The name with which the method is defined. Visibility: The visibility of the method (see §13.3.5). A method-definition is evaluated as follows: a) b) Let N be the method-name. Create a method U defined by the method-definition. Initialize the attributes of U as follows:
3
4
5
6
7 8
9
The class module list is the element at the top of [class-module-list] . The defined name is N. The visibility is: — If the current class or module is an eigenclass, then the current visibility. — Otherwise, if N is initialize or initialize copy, then the private visibility. — Otherwise, the current visibility.
10
11
12
13
14
15 16
c)
If a method binding with name N exists in the set of bindings of instance methods of the current class or module, let V be the value of that binding. 1) 2) If V is undef, the evaluation of the method-definition is implementation defined. Replace the value of the binding with U.
17
18
19 20
d)
Otherwise, create a method binding with name N and value U in the set of bindings of instance methods of the current class or module. The value of the method-definition is implementation defined. Method parameters
21
e)
22
13.3.2 Syntax
23
24 25 26
method-parameter-part :: ( parameter-list ? ) | parameter-list ? separator parameter-list :: mandatory-parameter-list , optional-parameter-list ? , array-parameter ? , block-parameter ? | optional-parameter-list , array-parameter ? , block-parameter ? 96
27 28 29 30
1 2
| array-parameter , block-parameter ? | block-parameter mandatory-parameter-list :: mandatory-parameter | mandatory-parameter-list , mandatory-parameter mandatory-parameter :: local-variable-identifier optional-parameter-list :: optional-parameter | optional-parameter-list , optional-parameter optional-parameter :: optional-parameter-name = default-parameter-expression optional-parameter-name :: local-variable-identifier default-parameter-expression :: operator-expression array-parameter :: * array-parameter-name |* array-parameter-name :: local-variable-identifier block-parameter :: & block-parameter-name block-parameter-name :: local-variable-identifier
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
All the local-variable-identifier s of mandatory-parameter s, optional-parameter-names, arrayparameter-name, and block-parameter-name of a parameter-list shall be pairwise different. Semantics There are four kinds of parameters as described below. How those parameters are bound to the actual arguments is described in §13.3.3. Mandatory parameters: These parameters are represented by mandatory-parameter s. 97
28
29 30
31
1 2
For each mandatory parameter, a corresponding actual argument shall be given when the method is invoked. Optional parameters: These parameters are represented by optional-parameter s. Each optional parameter consists of a parameter name represented by optional-parameter-name and an expression represented by default-parameter-expression. For each optional parameter, when there is no corresponding argument in the list of arguments given to the method invocation, the value of the default-parameter-expression is used as the value of the argument. An array parameter: This parameter is represented by array-parameter-name. Let N be the number of arguments, excluding a block argument, given to a method invocation. If N is more than the sum of the number of mandatory arguments and optional arguments, this parameter is bound to a direct instance of the class Array containing the extra arguments excluding a block argument. Otherwise, the parameter is bound to an empty direct instance of the class Array. If an array-parameter is of the form “*”, those extra arguments are ignored. A block parameter: This parameter is represented by block-parameter-name. The parameter is bound to the block passed to the method invocation. 13.3.3 Method invocation
3 4 5 6 7 8
9 10 11 12 13 14 15
16 17
18
19
The way in which a list of arguments is created are described in §11.2. Given the receiver R, the method name M, and the list of arguments A, take the following steps: a) If the method is invoked with a block, let B be the block. Otherwise, let B be block-notgiven. Let C be the eigenclass of R if R has an eigenclass. Otherwise, let C be the class of R. Search for a method binding with name M, starting from C as described in §13.3.4. If a binding is found and its value is not undef, let V be the value of the binding. Otherwise, if M is method missing, the behavior is implementation dependent. If M is not method missing, add a direct instance of the class Symbol with name M to the head of A, and invoke the method method missing on R with A as arguments and B as the block. Let O be the resulting value, and go to Step j. If the method is not invoked internally by a Ruby processor, check the visibility of V to see whether the method can be invoked (see §13.3.5). If the method cannot be invoked, add a direct instance of the class Symbol with name M to the head of A, and invoke the method method missing on R with A as arguments and B as the block. Let O be the resulting value, and go to Step j. Modify the execution context as follows:
20
21 22
23
b) c) d) e)
24
25
26 27 28 29
30 31 32 33 34
f)
35
g)
36
Push the class module list of V onto [class-module-list] . Push R onto [self ] .
37
98
1
Push M onto [invoked-method-name] . Push the public visibility to [default-visibility] . Push the defined name of V onto [defined-method-name] . Push B onto [block] . Push an empty set of local variable bindings onto [local-variable-bindings] .
2
3
4
5
6
h)
Evaluate the method-parameter-part of V as follows: 1) 2) Let L be the parameter-list of the method-parameter-part. Let Pm , Po , and Pa be the mandatory-parameter s of the mandatory-parameter-list, the optional-parameter s of the optional-parameter-list, and the array-parameter of L, respectively. Let NA , NP m , and NP o be the number of elements of A, Pm , and Po respectively. If there are no mandatory-parameter s or optional-parameter s, let NP m and NP o be 0. Let Sb be the current set of local variable bindings. If NA is smaller than NP m , raise a direct instance of the class ArgumentError. If the method does not have Pa and NA is larger than the sum of NP m and NP o , raise a direct instance of the class ArgumentError. Otherwise, for each argument Ai in A, in the same order in A, take the following steps: i) Let Pi be the mandatory-parameter or the optional-parameter whose position in the L corresponds to the position of Ai in A.
7
8 9 10 11 12
13
3) 4)
14 15
16
5)
17 18
19 20 21
If such Pi exists, let n be the mandatory-parameter if Pi is a mandatory parameter, or optional-parameter-name if Pi is an optional parameter. Create a variable binding with name n and value Ai in Sb . If such Pi does no exist, i.e. if NA is larger than the sum of NP m and NP o , and Pa exists: I) Create a direct instance of the class Array X whose length is the number of extra arguments.
22 23
24 25
26 27
II) Store each extra arguments into X, preserving the order in which they occur in the list of arguments. III) Let n be the array-parameter-name of Pa . IV) Create a variable binding with name n and value X in Sb . ii) If NA is smaller than the sum of NP m and NP o : I) For each optional argument POi to which no argument corresponds, evaluate the default-parameter-expression of POi , and let V be the resulting value. 99
28
29
30
31 32
1
II) Let n be the optional-parameter-name of POi . III) Create a variable binding with name n and value V in Sb . iii) If NA is smaller than or equal to the sum of NP m and NP o , and Pa exists: I) Create an empty direct instance of the class Array V.
2
3
4
5
II) Let n be the array-parameter-name of Pa . III) Create a variable binding with name n and value V in Sb . iv) If the block-parameter of L occurs, let D be the top of [block] . I) If D is block-not-given, let V be nil.
6
7
8
9 10 11
II) Otherwise, invoke the method new on the class Proc with an empty list of arguments and D as the block. Let V be the resulting value of the method invocation. III) Let n be the block-parameter-name of block-parameter. IV) Create a variable binding with name n and value V in Sb . i) Evaluate the method-body of V.
12
13
14
15
If the evaluation of the method-body is terminated by a return-expression: — If the jump-argument of the return-expression occurs, let O be the value of the jump-argument. — Otherwise, let O be nil.
16 17
18
19
Otherwise, let O be the resulting value of the evaluation.
20 21 22
j)
Restore the execution context by removing the elements from the tops of [class-module-list] , [self ] , [invoked-method-name] , [default-visibility] , [defined-method-name] , [block] , and [local-variable-bindings] , even when an exception is raised and not handled in Step i. The value of the method invocation is O.
23
k)
24 25 26
The method invocation or the super expression (see Step d of §11.2.3) which corresponds to the set of items on the tops of all the attributes of the execution context modified in Step g, except [local-variable-bindings] , is called the current method invocation. 13.3.4 Method lookup
27
28
Method lookup is the process by which a binding of an instance method is resolved. Given a method name M and a class or a module C which is initially searched for the binding of the method, the method binding is resolved as follows: 100
29 30
1 2
a)
If a method binding with name M exists in the set of bindings of instance methods of C, let B be that binding. Otherwise, let Lm be the list of included modules of C. Search for a method binding with name M in the set of bindings of instance methods of each module in Lm . Examine modules in Lm in reverse order. 1) 2) If a binding is found, let B be this binding. Otherwise:
3 4 5
b)
6
7
8
If the direct superclass of C is nil, the binding is considered not resolved. Otherwise, replace C with the direct superclass of C, and continue processing from Step a.
9 10
11
c)
B is the resolved method binding. Method visibility
12
13.3.5
13 14 15
Methods are categorized into one of public, private, or protected methods according to the conditions on which the method invocation is allowed. The attribute of a method which determines these conditions is called the visibility of the method. 13.3.5.1 Public methods
16
17
A public method is a method whose visibility is the public visibility. A public method can be invoked on an object anywhere within a program. 13.3.5.2 Private methods
18
19
20
A private method is a method whose visibility is the private visibility. A private method can not be invoked with explicit receiver, i.e. method invocations of the forms where primary-expression or chained-method-invocation occurs at the position which corresponds to the method receiver are not allowed, except for method invocations of the following forms where the primary-expression is self.
21 22 23 24
25
single-method-assignment abbreviated-method-assignment single-indexing-assignment abbreviated-indexing-assignment Protected methods
26
27
28
29
13.3.5.3
30
A protected method is a method whose visibility is the protected visibility. A protected method can be invoked if and only if the following condition holds: 101
31
1
Let M be an instance of the class Module in which the binding of the method exists. M is included in the current self, or M is the class of the current self or one of its superclasses.
2
3 4
If M is an eigenclass, whether the method can be invoked or not may be determined in a implementation defined way. 13.3.5.4 Visibility change
5
6 7
The visibility of methods can be changed with built-in methods public (§15.2.2.3.38), private (§15.2.2.3.36), and protected (§15.2.2.3.37), which are defined in the class Module. 13.3.6 Syntax The alias statement
8
9
10 11
alias-statement :: alias new-name aliased-name new-name :: method-name | symbol aliased-name :: method-name | symbol
12 13 14
15 16 17
18
Semantics An alias-statement is evaluated as follows: a) Evaluate the new-name as follows:
19
20
21
If the new-name is of the form method-name, let N be the method-name. If the new-name is of the form symbol, evaluate it. Let N be the name of the resulting instance of the class Symbol.
22 23
24
b)
Evaluate the aliased-name as follows:
25
If aliased-name is of the form method-name, let A be the method-name. If aliased-name is of the form symbol, evaluate it. Let A be the name of the resulting instance of the class Symbol.
26 27
28
c) d) 102
Let C be the current class or module. Search for a method binding with name A, starting from C as described in §13.3.4.
29
1
e) f)
If a binding is found and its value is not undef, let V be the value of the binding. Otherwise, let S be a direct instance of the class Symbol with name A and raise a direct instance of the class NameError which has S as its name property. If a method binding with name N exists in the set of bindings of instance methods of the current class or module, replace the value of the binding with V. Otherwise, create a method binding with name N and value V in the set of bindings of instance methods of the current class or module. The value of alias-statement is nil. The undef statement
2 3
4 5
g)
6 7
h)
8
i)
9
13.3.7 Syntax
10
11 12
undef-statement :: undef undef-list undef-list :: method-name-or-symbol ( , method-name-or-symbol )* method-name-or-symbol :: method-name | symbol
13 14
15 16 17
18
Semantics An undef-statement is evaluated as follows: a) For each method-name-or-symbol of the undef-list, take the following steps: 1) 2) Let C be the current class or module. If the method-name-or-symbol is of the form method-name, let N be the method-name. Otherwise, evaluate the symbol. Let N be the name of the resulting instance of the class Symbol. Search for a method binding with name N, starting from C as described in §13.3.4. If a binding is found and its value is not undef: i) ii) If the binding is found in C, replace the value of the binding with undef. Otherwise, create a method binding with name N and value undef in the set of bindings of instance methods of C.
19
20
21
22 23 24
25
3) 4)
26
27
28 29
30 31
5)
Otherwise, let S be a direct instance of the class Symbol with name N and raise a direct instance of the class NameError which has S as its name property. 103
1
b)
The value of undef-statement is nil.
2
13.4 Eigenclass
13.4.1 General description
3
4 5 6 7
An eigenclass is a class which is associated with a single object. An object, unless it is an instances of the class Class, becomes associated with an eigenclass through a singleton-methoddefinition or an eigenclass-definition. An instance of the class Class becomes associated with an eigenclass when it is created. The direct superclass of an eigenclass is implementation defined. However, an eigenclass shall be a subclass of the class of the object with which it is associated. NOTE 1 For example, the eigenclass of the class Object is a subclass of the class Class because the class Object is a direct instance of the class Class. Therefore, the instance methods of the class Class can be invoked on the class Object.
8 9
10 11 12
13
The eigenclass of a class whose direct superclass is not nil shall satisfy the following condition:
14 15
Let Ec be the eigenclass of a class C, and let S be the direct superclass of C, and let Es be the eigenclass of S. Then, Ec have Es as one of its superclasses.
16 17 18
NOTE 2 This requirement enables classes to inherit singleton methods from its superclasses. For example, the eigenclass of the class File has the eigenclass of the class IO as its superclass. Thereby, the class File inherits the singleton method open of the class IO.
19 20
The eigenclass of an object is unique in the sense that no two objects become associated with the same eigenclass. Although eigenclasses are instances of the class Class, they cannot create an instance of themselves. When the method new is invoked on an eigenclass, a direct instance of the class TypeError shall be raised (see Step a of §15.2.3.2.3). Whether an eigenclass can be a superclass of other classes is implementation dependent (see Step b-2-i of §13.2.2 and Step c of §15.2.3.2.1). Whether an eigenclass can have class variables or not is implementation defined. 13.4.2 Syntax Eigenclass definition
21 22 23
24 25
26
27
28
29 30
eigenclass-definition :: class << expression separator eigenclass-body end eigenclass-body :: body-statement
31 32
104
1
Semantics An eigenclass-definition is evaluated as follows: a) Evaluate the expression. Let O be the resulting value. A conforming processor may specify the set of classes such that if O is an instance of one of the classes in the set, a direct instance of the class TypeError is raised. If O is one of nil, true, or false, let E be the class of O and go to Step e. If O is not associated with an eigenclass, create a new eigenclass. Let E be the newly created eigenclass. If O is associated with an eigenclass, let E be that eigenclass. Modify the execution context as follows:
2
3 4 5
6
b) c)
7 8
9
d) e)
10
11 12 13
Create a new list which consists of the same elements as the list at the top of [classmodule-list] and add E to the head of the newly created list. Push the list onto [class-module-list] . Push E onto [self ] . Push the public visibility onto [default-visibility] . Push an empty set of bindings onto [local-variable-bindings] .
14
15
16
17 18
f)
Evaluate the eigenclass-body. The value of the eigenclass-definition is the value of the eigenclass-body. Restore the execution context by removing the elements from the tops of [class-modulelist] , [self ] , [default-visibility] , and [local-variable-bindings] , even when an exception is raised and not handled during Step f. Singleton method definition
19 20 21
g)
22
13.4.3 Syntax
23
24 25 26
singleton-method-definition :: def singleton ( . | :: ) method-name [ no line-terminator here ] method-parameter-part method-body end singleton :: variable | pseudo-variable | ( expression )
27 28 29 30
31
Semantics A singleton-method-definition is evaluated as follows: 105
32
1
a) b) c)
Evaluate the singleton. Let S be the resulting value. If S is one of nil, true, or false, let E be the class of O and go to Step e. If S is not associated with an eigenclass, create a new eigenclass. Let E be the newly created eigenclass. If S is associated with an eigenclass, Let E be that eigenclass. Let N be the method-name. Create a method U defined by the method-definition. Initialize the attributes of U as follows:
2
3 4
5
d) e) f)
6
7 8
9
The class module list is the element at the top of [class-module-list] . The defined name is N. The visibility is the public visibility.
10
11
12 13
g)
If a method binding with name N exists in the set of bindings of instance methods of E, let V be the value of that binding. 1) If V is undef, the evaluation of the singleton-method-definition is implementation defined. Replace the value of the binding with U.
14 15
16
2) h)
17 18
Otherwise, create a method binding with name N and value U in the set of bindings of instance methods of E. The value of the singleton-method-definition is implementation defined.
19
i)
20
14 Exceptions
21 22
If an instance of the class Exception is raised, the current evaluation process stops, and the evaluation process is transfered to a program construct that can handle this exception.
23
14.1 Cause of exceptions
24
An exception is raised when:
25
the method raise (see §15.3.1.1.13) is invoked. a certain exceptional condition occurs as described in various parts of this document.
26
27
Only instances of the class Exception shall be raised.
106
1
14.2 Exception handling
2 3 4 5
Exceptions are handled by a body-statement, an assignment-with-rescue-modifier, or a rescuemodifier-statement. These program constructs are called exception handlers. When an exception handler is handling an exception, the exception being handled is called the current exception. When an exception is raised, it is handled by an exception handler. This exception handler is determined as follows: a) Let S be the innermost local variable scope which lexically encloses the location where the exception is raised, and which corresponds to one of a program, a method-definition, a singleton-method-definition, or a block. Test each exception handler in S which lexically encloses the location where the exception is raised from the innermost to the outermost.
6 7
8 9 10
11 12
b)
13 14 15 16
An assignment-with-rescue-modifier is considered to handle the exception if the exception is an instance of the class StandardError (see §11.3.1.4), except when the exception is raised in its operator-expression 2 . In this case, assignment-with-rescuemodifier does not handle the exception. A rescue-modifier-statement is considered to handle the exception if the exception is an instance of the class StandardError (see §12.6), except when the exception is raised in its fallback-statement-of-rescue-modifier-statement In this case, rescuemodifier-statement does not handle the exception. A body-statement is considered to handle the exception if one of its rescue-clauses is considered to handle the exception (see §11.4.1.4.1), except when the exception is raised in one of its rescue-clauses, else-clauses, or ensure-clauses. In this case, body-statement does not handle the exception. If an ensure-clause of a body-statement occurs, it is evaluated even if the handler does not handle the exception (see §11.4.1.4.1).
17 18 19 20
21 22 23 24 25
26 27 28
c)
If an exception handler which can handle the exception is found in S, terminate the search for the exception handler. Continue evaluating the program as defined for the relevant construct (see §11.4.1.4.1, §11.3.1.4, and §12.6). If none of the exception handlers in S can handle the exception: 1) If S corresponds to a method-definition or a singleton-method-definition, terminate Step h or Step i , and take Step j of the current method invocation. Continue the search from Step a, under the assumption that the exception is raised at the location where the method is invoked. If S corresponds to a block, terminate the evaluation of the current block. Continue the search from Step a, under the assumption that the exception is raised at the location where the block is called. Otherwise, terminate the evaluation of the program.
29
d)
30 31 32 33
34 35 36
2)
37
3)
107
1
15 Built-in classes and modules
2 3
Built-in classes and modules are specified in this clause. Those classes and modules shall be defined in the class Object as constants.
4
15.1 General description
5 6
Each built-in class or module is specified by describing its attribute values, as described in §13.1 and §13.2. When a clause specifying a built-in class or module contains a subclause titled “Included modules”, the built-in class or module shall include the modules listed in that subclause in the order of that listing. Each subclause in the subclause titled “Singleton methods” with the title of the form C.m specifies the singleton method m of the class C. Each subclause in the subclause titled “Instance methods” with the title of the form C #m specifies the instance method m of the class C. The parameter specification of a method is described in the form of method-parameter-part (see §13.3.1). EXAMPLE 1 The following example defines the parameter specification of a method sample.
7 8 9
10 11
12 13
14 15
16
17
sample( arg1, arg2, opt =expr, *ary, &blk )
18 19
For a singleton method, the method name is prefixed by the name of the class or the module, and a dot (.). EXAMPLE 2 The following example defines the parameter specification of a singleton method sample of a class SampleClass:
20 21
22
SampleClass.sample( arg1, arg2, opt =expr, *ary, &blk )
23
Next to the parameter specification, the visibility and the behavior of the method are specified. The visibility, which is any one of public, protected or private, is specified after the label named “Visibility:”. The behavior, which is the steps which shall be taken while evaluating the method-body of the method (see Step i of §13.3.3), is specified after the label named “Behavior:”. In these steps, a reference to the name of an argument in the parameter specification is considered to be the object bound to the local variables of the same name. The phrase “call the block with X as the argument” indicates that the block corresponding to the block parameter block shall be called as described in §11.2.2 with X as the argument to the block call. The phrase “return X ” indicates that the evaluation of the method-body shall be terminated at that point, and X shall be the value of the method-body. The phrase “the name designated by N ” means the result of the following steps: 108
24 25
26 27
28 29 30 31 32 33 34
1
a) b) c)
If N is an instance of the class Symbol, the name of N. If N is an instance of the class String, the content of N. Otherwise, the behavior of the method is implementation dependent.
2
3
4 5 6 7 8
The class module list of an instance method of a built-in class or module shall be a list which consists of two elements: the first is the built-in class or module; the second is the class Object. The class module list of a singleton method of a built-in class or module shall be a list which consists of two elements: the first is the eigenclass of the built-in class or module; the second is the class Object. A conforming processor may provide additional attributes and/or values: a specific initial value for a predefined attribute whose initial value is not specified in this document, constants, singleton methods, instance methods, and additional inclusion of modules into built-in classes/modules.
9 10 11 12
13
15.2 Built-in classes
15.2.1 Object
14
15 16 17
The class Object is an implicit direct superclass for other classes; that is, if the direct superclass of a class is not specified explicitly in the class definition, the direct superclass of the class is the class Object (see §13.2.2). All built-in classes and modules can be referenced through constants of the class Object. 15.2.1.1 Direct superclass
18
19
20
The value nil. A conforming processor may define a sequence of superclasses of the class Object. However, the direct superclass of the class at the top of the class hierarchy shall always be nil. 15.2.1.2 Included modules
21 22
23
24
The following module is included in the class Object.
25
X
Kernel Constants
26
15.2.1.3
27
The following constants are defined in the class Object. STDIN: An implementation defined instance of the class IO, which shall be readable. STDOUT: An implementation defined instance of the class IO, which shall be writable. STDERR: An implementation defined instance of the class IO, which shall be writable. Besides, every built-in class or module, including the class Object itself, shall be defined in the class Object as a constant, whose name is the name of the class or module, and whose value is the class or module. 109
28
29
30
31 32 33
1
15.2.1.4 15.2.1.4.1
Instance methods Object#initialize
2
3
initialize( *args ) Visibility: private Behavior: The method initialize is the default object initialization method, which is invoked when an instance is created (see §13.2.4). It returns an implementation defined value. 15.2.2 Module
4
5 6 7
8
9 10
All modules are instances of the class Module. Therefore, behaviors defined in the class Module are shared by all modules. The binary relation on the instances of the class Module denoted A
11
B is defined as follows:
12
B is a module and B is included in A (see §13.1.3) or both A and B are classes and B is a superclass of A. Direct superclass
13
14
15.2.2.1
15
The class Object 15.2.2.2 15.2.2.2.1 Singleton methods Module.constants
16
17
18
Module.constants Visibility: public Behavior: a) b) Create an empty direct instance of the class Array. Let A be the instance. Let C be the current class or module. Let L be the list which consists of the same elements as the list at the second element from the top of [class-module-list] , except the last element, which is the class Object. Let CS be the set of classes which consists of C and all the superclasses of C except the class Object, but when C is the class Object, it shall be included in CS. Let MS be the set of modules which consists of all the modules in the included module list of all classes in CS. Let CM be the union of L, CS and MS. c) For each class or module c in CM, and for each name N of a constant defined in c, take the following steps:
19
20
21
22 23 24
25 26 27 28
29 30
110
1 2 3
1)
Let S be either a direct instance of the class String whose content is N or a direct instance of the class Symbol whose name is N. Which of the these classes of instance is chosen as the value of S is implementation defined. Unless A contains the element of the same name as S, when S is an instance of the class Symbol, or the same content as S, when S is an instance of the class String, append S to A.
4 5 6
2)
7
d)
Return A. Module.nesting
8
15.2.2.2.2
9
Module.nesting Visibility: public Behavior: The method returns a new direct instance of the class Array which contains all but the last element of the list at the second element from the top of the [class-module-list] in the same order. 15.2.2.3 15.2.2.3.1 Instance methods Module#<
10
11 12 13
14
15
16
<( other ) Visibility: public Behavior: Let A be the other . Let R be the receiver of the method. a) b) c) d) e) If A is not an instance of the class Module, raise a direct instance of the class TypeError. If A and R is the same object, return false. If R If A A, return true. R, return false.
17
18
19
20
21
22
23
Otherwise, return nil. Module#<=
24
15.2.2.3.2
25
< =( other ) Visibility: public Behavior: 111
26
27
1
a) b)
If the other and the receiver are the same object, return true. Otherwise, the behavior is the same as the method < (see §15.2.2.3.1). Module#<=>
2
3
15.2.2.3.3
4
< =>( other ) Visibility: public Behavior: Let A be the other . Let R be the receiver of the method. a) b) If A is not an instance of the class Module, return nil. If A and R is the same object, return an instance of the class Integer whose value is 0. If R If A A, return an instance of the class Integer whose value is -1. R, return an instance of the class Integer whose value is 1.
5
6
7
8 9
10
c) d) e)
11
12
Otherwise, return nil. Module#==
13
15.2.2.3.4
14
= =( other ) Visibility: public Behavior: Same as the method == of the module Kernel (see §15.3.1.2.1). 15.2.2.3.5 Module#===
15
16
17
18
= = =( object ) Visibility: public Behavior: The method behaves as if the method kind of? were invoked on the object with the receiver as the only argument (see §15.3.1.2.26). 15.2.2.3.6 Module#>
19
20 21
22
23
>( other ) Visibility: public 112
24
1
Behavior: Let A be the other . Let R be the receiver of the method. a) b) c) d) e) If A is not an instance of the class Module, raise a direct instance of the class TypeError. If A and R is the same object, return false. If R If A A, return false. R, return true.
2
3
4
5
6
Otherwise, return nil. Module#>=
7
15.2.2.3.7
8
> =( other ) Visibility: public Behavior: a) b) If the other and the receiver are the same object, return true. Otherwise, the behavior is the same as the method > (see §15.2.2.3.6). Module#alias method
9
10
11
12
13
15.2.2.3.8
14
alias method( new name, aliased name ) Visibility: private Behavior: Let C be the receiver of the method. a) Let N be the name designated by the new name. Let A be the name designated by the aliased name. Take Step d through h of §13.3.6, assuming that A, C, and N in §13.3.6 to be A, C, and N in the above steps. Return C. Module#ancestors
15
16
17 18
19 20
b)
21
c)
22
15.2.2.3.9
23
ancestors Visibility: public Behavior: 113
24
25
1
a) b) c) d) e) f) g)
Create an empty direct instance of the class Array A. Let C be the receiver of the method. If C is not an eigenclass, append C to A. Append each element of the included module list of C, in the order in the list, to A. If C is a class, replace C with the direct superclass of the current C. If C is not nil, repeat from Step c. Return A. Module#append features
2
3
4
5
6
7
8
15.2.2.3.10
9
append features( module ) Visibility: private Behavior: Let L1 and L2 be the included modules list of the receiver and the module respectively. a) If the module and the receiver is the same object, the behavior is implementation dependent. If the receiver is an element of L2, the behavior is implementation defined. Otherwise, for each module M in L1, in the same order in L1, take the following steps: 1) If M and the module are the same object, the behavior is implementation dependent. If M is not in L2, append M to the end of L2.
10
11 12
13 14
15
b) c)
16
17 18
19
2) d) e)
20
Append the receiver to L2. Return the receiver. Module#attr
21
22
15.2.2.3.11
23
attr( symbol, writable =false ) Visibility: private Behavior: Let C be the method receiver. a) If the symbol is not an instance of the class Symbol, the behavior is implementation dependent.
24
25
26 27
114
1
b) c)
Let N be the name of the symbol . If N is not of the form local-variable-identifier or constant-identifier, raise a direct instance of the class NameError which has the symbol as its name property. Define an instance method in C as if by evaluating the following method definition at the location of the invocation. In the following method definition, N is N, and @N is the name which is N prefixed by “@”. def N @N end
2 3
4 5 6
d)
7 8 9 10
11 12 13 14 15
e)
If the writable is true, define an instance method in C as if by evaluating the following method definition at the location of the invocation. In the following method definition, N= is the name N postfixed by =, and @N is the name which is N prefixed by “@”. The choice of the parameter name is arbitrary, and val is chosen only for the expository purpose. def N=(val) @N = val end
16 17 18 19
20
f)
Return nil. Module#attr accessor
21
15.2.2.3.12
22
attr accessor( *symbol list ) Visibility: private Behavior: a) For each element S of the symbol list, invoke the method attr with S as the first argument and true as the second argument (see §15.2.2.3.11). Return nil. Module#attr reader
23
24
25 26
27
b)
28
15.2.2.3.13
29
attr reader( *symbol list ) Visibility: private Behavior:
30
31
115
1 2
a)
For each element S of the symbol list, invoke the method attr with S as the first argument and false as the second argument (see §15.2.2.3.11). Return nil. Module#attr writer
3
b)
4
15.2.2.3.14
5
attr writer( *symbol list ) Visibility: private Behavior: a) For each element S of the symbol list, invoke the method attr with S as the first argument and true as the second argument, but skip Step d (see §15.2.2.3.11). Return nil. Module#class eval
6
7
8 9
10
b)
11
15.2.2.3.15
12
class eval( string = nil, &block ) Visibility: public Behavior: a) b) Let M be the receiver. If the block is given: 1) 2) If the string is given, raise a direct instance of the class ArgumentError. Call the block with implementation defined arguments as described in §11.2.2, and let V be the resulting value. A conforming processor shall modify the execution context just before Step d of §11.2.2 as follows:
13
14
15
16
17
18 19 20
21 22 23
Create a new list which has the same members as those of the list at the top of [class-module-list] , and add M to the head of the newly created list. Push the list onto [class-module-list] . Push the receiver onto [self ] . Push the public visibility onto [default-visibility] .
24
25
26 27 28 29
In Step d and e of §11.2.2, a conforming processor may ignore M which is added to the head of the top of [class-module-list] as described above, except when referring to the current class or module in a method-definition (see §13.3.1), an alias-statement (see §13.3.6), or an undef-statement (see §13.3.7). 116
1
3) c)
Return V.
2
If the block is not given: 1) If the string is not an instance of the class String, the behavior is implementation dependent. Modify the execution context as follows:
3 4
5
2)
6 7 8
Create a new list which has the same members as those of the list at the top of [class-module-list] , and add M to the head of the newly created list. Push the list onto [class-module-list] . Push the receiver onto [self ] . Push the public visibility onto [default-visibility] .
9
10
11 12
3)
Parse the content of the string as a program (see §10.1). If it fails, raise a direct instance of the class SyntaxError. Evaluate the program. Let V be the resulting value of the evaluation. Restore the execution context by removing the elements from the tops of [classmodule-list] , [self ] , and [default-visibility] , even when an exception is raised and not handled in Step c-3 or c-4. Return V.
13
4) 5)
14 15 16
17
6)
18 19 20
In Step c-4, the string is evaluated under the new local variable scope in which references to local-variable-identifier s are resolved in the same way as in scopes created by block s (see §9.1.1). 15.2.2.3.16 Module#class variable defined?
21
22
class variable defined?( symbol ) Visibility: public Behavior: Let C be the receiver of the method. a) b) Let N be the name designated by the symbol . If N is not of the form class-variable-identifier, raise a direct instance of the class NameError which has the symbol as its name property. Search for a binding of the class variable with name N by taking Step b through d of §11.4.3.4, assuming that C and N in §11.4.3.4 to be C and N in the above steps. If a binding is found, return true. Otherwise, return false. 117
23
24
25
26 27
28 29
c)
30
d) e)
31
1
15.2.2.3.17
Module#class variable get
2
class variable get( symbol ) Visibility: private Behavior: Let C be the receiver of the method. a) b) Let N be the name designated by the symbol . If N is not of the form class-variable-identifier, raise a direct instance of the class NameError which has the symbol as its name property. Search for a binding of the class variable with name N by taking Step b through d of §11.4.3.4, assuming that C and N in §11.4.3.4 to be C and N in the above steps. If a binding is found, return the value of the binding. Otherwise, raise a direct instance of the class NameError which has the symbol as its name property. Module#class variable set
3
4
5
6 7
8 9
c)
10
d) e)
11 12
13
15.2.2.3.18
14
class variable set( symbol, obj ) Visibility: private Behavior: Let C be the receiver of the method. a) b) Let N be the name designated by the symbol . If N is not of the form class-variable-identifier, raise a direct instance of the class NameError which has the symbol as its name property. Search for a binding of the class variable with name N by taking Step b through d of §11.4.3.4, assuming that C and N in §11.4.3.4 to be C and N in the above steps. If a binding is found, replace the value of the binding with the obj . Otherwise, create a variable binding with name N and value obj in the set of bindings of class variables of C. Module#class variables
15
16
17
18 19
20 21
c)
22
d) e)
23 24
25
15.2.2.3.19
26
class variables Visibility: public 118
27
1 2 3 4
Behavior: The methods returns a direct instance of the class Array which consists of names of all class variables of the receiver. These names are represented by direct instances of either the class String or the class Symbol. Which of those classes is chosen is implementation defined. 15.2.2.3.20 Module#const defined?
5
6
const defined?( symbol ) Visibility: public Behavior: a) b) c) Let C be the receiver of the method. Let N be the name designated by the symbol . If N is not of the form constant-identifier, raise a direct instance of the class NameError which has the symbol as its name property. If a binding with name N exists in the set of bindings of constants of C, return true. Otherwise, return false. Module#const get
7
8
9
10
11 12
13
d) e)
14
15
15.2.2.3.21
16
const get( symbol ) Visibility: public Behavior: a) b) Let N be the name designated by the symbol . If N is not of the form constant-identifier, raise a direct instance of the class NameError which has the symbol as its name property. Search for a binding of a constant with name N from Step e of §11.4.3.1, assuming that C in §11.4.3.1 to be the receiver of the method. If a binding is found, return the value of the binding. Otherwise, return the value of the invocation of the method const missing (See Step e-2 of §11.4.3.1). Module#const missing
17
18
19
20 21
22 23
c)
24
d) e)
25 26
27
15.2.2.3.22
119
1
const missing( symbol ) Visibility: public Behavior: The method const missing is invoked when a binding of a constant does not exist on a constant reference (see §11.4.3.1). When the method is invoked, take the following steps: a) b) Take Step a through c of §15.2.2.3.20. Raise a direct instance of the class NameError which has the symbol as its name property. Module#const set
2
3 4
5
6
7 8
9
15.2.2.3.23
10
const set( symbol, obj ) Visibility: public Behavior: Let C be the receiver of the method. a) b) Let N be the name designated by the symbol . If N is not of the form constant-identifier, raise a direct instance of the class NameError which has the symbol as its name property. If a binding with name N exists in the set of bindings of constants of C, replace the value of the binding with the obj . Otherwise, create a variable binding with N and value obj in the set of bindings of constants of C. Return the obj . Module#constants
11
12
13
14 15
16 17
c)
18 19
d)
20
e)
21
15.2.2.3.24
22
constants Visibility: public Behavior: The method returns a new direct instance of the class Array which consists of names of all constants defined in the receiver. These names are represented by direct instances of either the class String or the class Symbol. Which of those classes is chosen is implementation defined. 15.2.2.3.25 120 Module#extend object
23
24 25 26 27
28
1
extend object( object ) Visibility: private Behavior: Let S be the eigenclass of the object. The method behaves as if by invoking the method append features on the receiver with S as the only argument. 15.2.2.3.26 Module#extended
2
3 4
5
6
extended( object ) Visibility: private Behavior: The method returns nil. 15.2.2.3.27 Module#include
7
8
9
10
include( *module list ) Visibility: private Behavior: Let C be the receiver of the method. a) For each element A of the module list, in the reverse order in the module list, take the following steps: 1) If A is not an instance of the class Module, raise a direct instance of the class TypeError. If A is an instance of the class Class, raise a direct instance of the class TypeError. Invoke the method append features on A with C as the only argument. Invoke the method included on A with C as the only argument.
11
12
13 14
15 16
17
2) 3) 4) b)
18
19
20
Return C. Module#include?
21
15.2.2.3.28
22
include?( module ) Visibility: public Behavior: Let C be the receiver of the method. 121
23
24
1 2
a)
If the module is not an instance of the class Module, raise a direct instance of the class TypeError. If the module is an element of the included module list of C, return true. Otherwise, if C is an instance of the class Class, and if the module is an element of the included module list of one of the superclasses of C, then return true. Otherwise, return false. Module#included
3
b) c)
4 5
6
d)
7
15.2.2.3.29
8
included( module ) Visibility: private Behavior: The method returns nil. 15.2.2.3.30 Module#included modules
9
10
11
12
included modules Visibility: public Behavior: Let C be the receiver of the method. a) b) c) Create an empty direct instance of the class Array A. Append each element of the included module list of C, in the reverse order, to A. If C is an instance of the class Class, replace C with the direct superclass of the current C If C is not nil, repeat from Step b. Return A. Module#initialize
13
14
15
16
17 18
19
d) e)
20
21
15.2.2.3.31
22
initialize( &block ) Visibility: private Behavior: a) If the block is given, call the block as if invoking the method class eval of the class Module on the receiver with no arguments and the block as the block. Return an implementation defined value.
23
24
25 26
27
b) 122
1
15.2.2.3.32
Module#initialize copy
2
initialize copy( original ) Visibility: private Behavior: a) Invoke the instance method initialize copy defined in the module Kernel on the receiver with the original as the argument. If the receiver is associated with an eigenclass, let Eo be the eigenclass, and take the following steps: 1) Create an eigenclass whose direct superclass is the direct superclass of Eo . Let En be the eigenclass. For each binding Bv1 of the constants of Eo , create a variable binding with the same name and value as Bv1 in the set of bindings of constants of En . For each binding Bv2 of the class variables of Eo , create a variable binding with the same name and value as Bv2 in the set of bindings of class variables of En . For each binding Bm of the instance methods of Eo , create a method binding with the same name and value as Bm in the set of bindings of instance methods of En . Associate the receiver with En .
3
4
5 6
7 8
b)
9 10
11 12
2)
13 14
3)
15 16
4)
17
5) c)
18 19
If the receiver is an instance of the class Class, set the direct superclass of the receiver to the direct superclass of the original . Append each element of the included module list of the original , in the same order, to the receiver. For each binding Bv3 of the constants of the original , create a variable binding with the same name and value as Bv3 in the set of bindings of constants of the receiver. For each binding Bv4 of the class variables of the original , create a variable binding with the same name and value as Bv4 in the set of bindings of class variables of the receiver. For each binding Bm2 of the instance methods of the original , create a method binding with the same name and value as Bm2 in the set of bindings of instance methods of the receiver. Return an implementation defined value. Module#instance methods
20 21
d)
22 23
e)
24 25 26
f)
27 28 29
g)
30
h)
31
15.2.2.3.33
123
1
instance methods( include super =true ) Visibility: public Behavior: Let C be the receiver of the method. a) b) Create an an empty direct instance of the class Array. Let A be the instance. Let I be the set of bindings of instance methods of C. For each binding B of I, let N be the name of B, and let V be the value of B, and take the following steps: 1) 2) If V is undef, or the visibility of V is private, skip the next two steps. Let S be either a direct instance of the class String whose content is N or a direct instance of the class Symbol whose name is N. Which of the these classes of instance is chosen as the value of S is implementation defined. Unless A contains the element of the same name (if S is an instance of the class Symbol) or the same content (if S is an instance of the class String) as S, append S to A.
2
3
4
5 6
7
8 9 10
11 12 13
3)
14
c)
If the include super is a true value: 1) For each module M in included module list of C, take Step b, assuming that C in that step to be M. Replace C with the direct superclass of C. If C is not nil, repeat from Step b.
15 16
17
2) 3) d)
18
19
Return A. Module#method defined?
20
15.2.2.3.34
21
method defined?( symbol ) Visibility: public Behavior: Let C be the receiver of the method. a) b) Let N be the name designated by the symbol . Search for a binding of an instance method named N starting from C as described in §13.3.4. If a binding is found and its value is not undef, return true. Otherwise, return false.
22
23
24
25 26
27
c) d) 124
28
1
15.2.2.3.35
Module#module eval
2
module eval( string = nil, &block ) Visibility: public Behavior: Same as the method class eval (see §15.2.2.3.15) 15.2.2.3.36 Module#private
3
4
5
6
private( *symbol-list ) Visibility: private Behavior: Same as the method public (see §15.2.2.3.38), except that the method changes current visibility or visibilities of methods corresponding to each element of the symbol list to private. 15.2.2.3.37 Module#protected
7
8 9 10
11
12
protected( *symbol-list ) Visibility: private Behavior: Same as the method public (see §15.2.2.3.38), except that the method changes current visibility or visibilities of methods corresponding to each element of the symbol list to protected. 15.2.2.3.38 Module#public
13
14 15 16
17
18
public( *symbol list ) Visibility: private Behavior: Let C be the receiver of the method. a) b) If the length of symbol list is 0, change the current visibility to public and return C Otherwise, for each element S of the symbol list, take the following steps: 1) 2) 3) Let N be the name designated by S. Search for a method binding with name N starting from C as described in §13.3.4. If a binding is found and its value is not undef, let V the value of the binding. 125
19
20
21
22
23
24
25
1 2
4)
Otherwise, raise a direct instance of the class NameError which has S as its name property. If C is the class or module in which the binding is found, change the visibility of V to the public visibility. Otherwise, define an instance method in C as if by evaluating the following method definition. In the definition, N is N. The choice of the parameter name is arbitrary, and args is chosen only for the expository purpose. def N(*args) super end
3 4
5)
5 6 7
6)
8 9 10 11
12 13
The attributes of the method created by the above definition are initialized as follows: i) ii) The class module list is the element at the top of [class-module-list] . The defined name is the defined name of V.
14
15
16
iii) The visibility is the public visibility. c) Return C. Module#remove class variable
17
18
15.2.2.3.39
19
remove class variable( symbol ) Visibility: private Behavior: Let C be the receiver of the method. a) b) Let N be the name designated by the symbol . If N is not of the form class-variable-identifier, raise a direct instance of the class NameError which has the symbol as its name property. If a binding with name N exists in the set of bindings of class variables of C, let V be the value of the binding. 1) 2) d) Remove the binding from the set of bindings of class variables of C. Return V.
20
21
22
23 24
25 26
c)
27
28
29 30
Otherwise, raise a direct instance of the class NameError which has the symbol as its name property. Module#remove const
31
15.2.2.3.40 126
1
remove const( symbol ) Visibility: private Behavior: Let C be the receiver of the method. a) b) Let N be the name designated by the symbol . If N is not of the form constant-identifier, raise a direct instance of the class NameError which has the symbol as its name property. If a binding with name N exists in the set of bindings of constants of C, let V be the value of the binding. 1) 2) d) Remove the binding from the set of bindings of constants of C. Return V.
2
3
4
5 6
7 8
c)
9
10
11 12
Otherwise, raise a direct instance of the class NameError which has the symbol as its name property. Module#remove method
13
15.2.2.3.41
14
remove method( *symbol list ) Visibility: private Behavior: Let C be the receiver of the method. a) For each element S of the symbol list, take the following steps: 1) 2) Let N be the name designated by S. If a binding with name N exists in the set of bindings of instance methods of C, remove the binding from the set. Otherwise, raise a direct instance of the class NameError which has S as its name property. In this case, the remaining elements of the symbol list are not processed.
15
16
17
18
19 20
21 22
3)
23
b)
Return C. Module#undef method
24
15.2.2.3.42
25
undef method( *symbol list ) Visibility: private 127
26
1
Behavior: Let C be the receiver of the method. a) For each element S of the symbol list, take following steps: 1) 2) Let N be the name designated by S. Take Step a-3 and a-4 of §13.3.7, assuming that C in §13.3.7 to be C and N in the above steps.
2
3
4 5
6
b) 15.2.3
Return C. Class
7
8 9
All classes are instances of the class Class. Therefore, behaviors defined in the class Class are shared by all classes. A conforming processor shall undefine the instance methods append features and extend object of the class Class, as if by invoking the method undef method on the class Class with instances of the class Symbol whoses names are “append features” and “extend object” as the arguments (see §15.2.2.3.42). 15.2.3.1 Direct superclass
10 11 12 13
14
15
The class Module 15.2.3.2 15.2.3.2.1 Instance methods Class#initialize
16
17
18
initialize( superclass =Object, &block ) Visibility: private Behavior: a) b) If the receiver has its direct superclass, raise a direct instance of the class TypeError. If the superclass is not an instance of the class Class, raise a direct instance of the class TypeError. If the superclass is an eigenclass or the class Class, the behavior is implementation dependent. Set the direct superclass of the receiver to the superclass. Create an eigenclass, and associate it with the receiver. The eigenclass shall have the eigenclass of the superclass as one of its superclasses. If the block is given, call the block as if invoking the method class eval of the class Module on the receiver with no arguments and the block as the block. Return an implementation defined value.
19
20
21
22 23
24 25
c)
26
d) e)
27 28
29 30
f)
31
g) 128
1
15.2.3.2.2
Class#initialize copy
2
initialize copy( original ) Visibility: private Behavior: a) If the direct superclass of the reciver has already been set, raise a direct instance of the class TypeError. If the receiver is an eigenclass, raise a direct instance of the class TypeError. Invoke the instance method initialize copy defined in the class Module on the receiver with the original as the argument. Return an implementation defined value. Class#new
3
4
5 6
7
b) c)
8 9
10
d)
11
15.2.3.2.3
12
new( *args, &block ) Visibility: public Behavior: a) b) If the receiver is an eigenclass, raise a direct instance of the class TypeError. Create a direct instance of the receiver which has no bindings of instance variables. Let O be the newly created instance. Invoke the method initialize on O with all the elements of the args as arguments and the block as the block. Return O. Class#superclass
13
14
15
16 17
18 19
c)
20
d)
21
15.2.3.2.4
22
superclass Visibility: public Behavior: Let C be the receiver of the methods. a) b) If C is an eigenclass, return an implementation defined value. Otherwise, return the direct superclass of C. 129
23
24
25
26
1
15.2.4
NilClass
2
The class NilClass has only one instance, which is represented by the pseudo variable nil. Instances of the class NilClass shall not be created by the method new of the class NilClass. Therefore, a conforming processor shall undefine the singleton method new of the class NilClass, as if by invoking the method undef method on the eigenclass of the class NilClass with a direct instance of the class Symbol whose name is “new” as the argument (see §15.2.2.3.42). 15.2.4.1 Direct superclass
3 4 5 6
7
8
The class Object 15.2.4.2 15.2.4.2.1 Instance methods NilClass#&
9
10
11
&( other ) Visibility: public Behavior: The method returns false. 15.2.4.2.2 NilClass#ˆ
12
13
14
15
^( other ) Visibility: public Behavior: a) b) If the other is a false value, return false. Otherwise, return true. NilClass#nil?
16
17
18
19
20
15.2.4.2.3
21
nil? Visibility: public Behavior: The method returns true. 15.2.4.2.4 NilClass#|
22
23
24
130
1
|( other ) Visibility: public Behavior: a) b) 15.2.5 If the other is a false value, return false. Otherwise, return true. TrueClass
2
3
4
5
6
7 8
The class TrueClass has only one instance, which is represented by the pseudo variable true. true represents a logical true value. Instances of the class TrueClass shall not be created by the method new of the class TrueClass. Therefore, a conforming processor shall undefine the singleton method new of the class TrueClass, as if by invoking the method undef method on the eigenclass of the class TrueClass with a direct instance of the class Symbol whose name is “new” as the argument (see §15.2.2.3.42). 15.2.5.1 Direct superclass
9 10 11 12
13
14
The class Object 15.2.5.2 15.2.5.2.1 Instance methods TrueClass#&
15
16
17
&( other ) Visibility: public Behavior: a) b) If the other is a false value, return false. Otherwise, return true. TrueClass#ˆ
18
19
20
21
22
15.2.5.2.2
23
^( other ) Visibility: public Behavior: a) b) If the other is a false value, return true. Otherwise, return false. 131
24
25
26
27
1
15.2.5.2.3
TrueClass#to s
2
to s Visibility: public Behavior: The method returns an instance of the class String, the content of which is “true”. 15.2.5.2.4 TrueClass#|
3
4 5
6
7
|( other ) Visibility: public Behavior: The method returns true. 15.2.6 FalseClass
8
9
10
11 12
The class FalseClass has only one instance, which is represented by the pseudo variable false. false represents a logical false value. Instances of the class FalseClass shall not be created by the method new of the class FalseClass. Therefore, a conforming processor shall undefine the singleton method new of the class FalseClass, as if by invoking the method undef method on the eigenclass of the class FalseClass with a direct instance of the class Symbol whose name is “new” as the argument (see §15.2.2.3.42). 15.2.6.1 Direct superclass
13 14 15 16
17
18
The class Object 15.2.6.2 15.2.6.2.1 Instance methods FalseClass#&
19
20
21
&( other ) Visibility: public Behavior: The method returns false. 15.2.6.2.2 FalseClass#ˆ
22
23
24
25
^( other ) Visibility: public 132
26
1
Behavior: a) b) If the other is a false value, return false. Otherwise, return true. FalseClass#to s
2
3
4
15.2.6.2.3
5
to s Visibility: public Behavior: The method returns an instance of the class String, the content of which is “false”. 15.2.6.2.4 FalseClass#|
6
7 8
9
10
|( other ) Visibility: public Behavior: a) b) 15.2.7 If the other is a false value, return false. Otherwise, return true. Numeric
11
12
13
14
15
16 17
Instances of the class Numeric represent numbers. The class Numeric is a superclass of all the other built-in classes which represent numbers. The notation “the value of the instance N of the class Numeric” means the number which N represent. 15.2.7.1 Direct superclass
18 19
20
21
The class Object 15.2.7.2 Included modules
22
23
The following module is included in the class Numeric.
24
X
Comparable Instance methods Numeric#+@ 133
25
15.2.7.3 15.2.7.3.1
26
1
+@ Visibility: public Behavior: The method returns the receiver. 15.2.7.3.2 Numeric#−@
2
3
4
5
-@ Visibility: public Behavior: a) Invoke the method coerce on the receiver with an instance of the class Integer whose value is 0 as the only argument. Let V be the resulting value. 1) If V is an instance of the class Array which contains two elements, let F and S be the first and the second element of V respectively. i) ii) 2) 15.2.7.3.3 Invoke the method - on F with S as the only argument. Return the resulting value.
6
7
8 9
10 11
12
13
14
Otherwise, raise a direct instance of the class TypeError. Numeric#abs
15
16
abs Visibility: public Behavior: a) Invoke the method < on the receiver with an instance of the class Integer whose value is 0. If this invocation results in a true value, invoke the method -@ on the receiver and return the resulting value. Otherwise, return the receiver. 15.2.7.3.4 Numeric#coerce
17
18
19 20
21 22
b)
23
24
134
1
coerce( other ) Visibility: public Behavior: a) If the class of the receiver and the class of the other are the same class, let X and Y be the other and the receiver, respectively. Otherwise, let X and Y be instances of the class Float which are converted from the other and the receiver, respectively. the other and the receiver are converted as follows: 1) 2) 3) Let O be the other or the receiver. If O is an instance of the class Float, let F be O. Otherwise: i) If an invocation of the method respond to? on O with a direct instance of the class Symbol whose name is to f as the argument results in a false value, raise a direct instance of the class TypeError. Invoke the method to f on O with no arguments, and let F be the resulting value.
2
3
4 5
6 7
b)
8
9
10
11 12 13
14 15
ii)
16 17
iii) If F is not an instance of the class Float, raise a direct instance of the class TypeError. 4) 5) c) If the value of F is NaN, the behavior is implementation dependent. The converted value of O is F.
18
19
20 21
Create a direct instance of the class Array which consists of two elements: the first is X ; the second is Y. Return the instance of the class Array. Integer
22
d) 15.2.8
23
24
Instances of the class Integer represent integers. The ranges of these integers are unbounded. Instances of the class Integer shall not be created by the method new of the class Integer. Therefore, a conforming processor shall undefine the singleton method new of the class Integer, as if by invoking the method undef method on the eigenclass of the class Integer with a direct instance of the class Symbol whose name is “new” as the argument (see §15.2.2.3.42). A conforming processor may define subclasses of the class Integer which differ only in the ranges of the representing integer values. In this case, a conforming processor:
25 26 27 28
29 30
31
shall define methods +, -, *, /, and % in all of these classes. 135
1 2
shall not create a direct instance of the class Integer, but shall create a direct instance of one of these subclasses, instead of the class Integer.
3 4
If a conforming processor does not define any subclass of the class Integer, it shall define methods +, -, *, / and % in the class Integer. 15.2.8.1 Direct superclass
5
6
The class Numeric 15.2.8.2 15.2.8.2.1 Instance methods Integer#+
7
8
9
+( other ) Visibility: public Behavior: a) If the other is an instance of the class Integer, return an instance of the class Integer whose value is the sum of the values of the receiver and the other . If the other is an instance of the class Float, let R be the value of the receiver as a floating point number. Return a direct instance of the class Float whose value is the sum of R and the value of the other . c) Otherwise, invoke the method coerce on the other with the receiver as the only argument. Let V be the resulting value. 1) If V is an instance of the class Array which contains two elements, let F and S be the first and the second element of V respectively. i) ii) 2) 15.2.8.2.2 Invoke the method + on F with S as the only argument. Return the resulting value.
10
11
12 13
14 15
b)
16 17
18 19
20 21
22
23
24
Otherwise, raise a direct instance of the class TypeError. Integer#−
25
26
-( other ) Visibility: public Behavior: 136
27
28
1 2 3
a)
If the other is an instance of the class Integer, return an instance of the class Integer whose value is the result of subtracting the value of the other from the value of the receiver. If the other is an instance of the class Float, let R be the value of the receiver as a floating point number. Return a direct instance of the class Float whose value is the result of subtracting the value of the other from R.
4 5
b)
6 7
8 9
c)
Otherwise, invoke the method coerce on the other with the receiver as the only argument. Let V be the resulting value. 1) If V is an instance of the class Array which contains two elements, let F and S be the first and the second element of V respectively. i) ii) 2) Invoke the method - on F with S as the only argument. Return the resulting value.
10 11
12
13
14
Otherwise, raise a direct instance of the class TypeError. Integer#*
15
15.2.8.2.3
16
- ( other ) Visibility: public Behavior: a) If the other is an instance of the class Integer, return an instance of the class Integer whose value is the result of multiplication of the values of the receiver and the other . If the other is an instance of the class Float, let R be the value of the receiver as a floating point number. Return a direct instance of the class Float whose value is the result of multiplication of R and the value of the other . c) Otherwise, invoke the method coerce on the other with the receiver as the only argument. Let V be the resulting value. 1) If V is an instance of the class Array which contains two elements, let F and S be the first and the second element of V respectively. i) ii) 2) Invoke the method * on F with S as the only argument. Return the resulting value.
17
18
19 20
21 22
b)
23 24
25 26
27 28
29
30
31
Otherwise, raise a direct instance of the class TypeError. 137
1
15.2.8.2.4
Integer#/
2
/( other ) Visibility: public Behavior: a) If the other is an instance of the class Integer: 1) 2) If the value of the other is 0, raise a direct instance of the class ZeroDivisionError. Otherwise, let n be the value of the receiver divided by the value of the other . Return an instance of the class Integer whose value is the largest integer smaller than or equal to n.
3
4
5
6
7 8 9
10 11
b)
Otherwise, invoke the method coerce on the other with the receiver as the only argument. Let V be the resulting value. 1) If V is an instance of the class Array which contains two elements, let F and S be the first and the second element of V respectively. i) ii) 2) Invoke the method / on F with S as the only argument. Return the resulting value.
12 13
14
15
16
Otherwise, raise a direct instance of the class TypeError. Integer#%
17
15.2.8.2.5
18
%( other ) Visibility: public Behavior: a) If the other is an instance of the class Integer: 1) 2) If the value of the other is 0, raise a direct instance of the class ZeroDivisionError. Otherwise, let x and y be the values of the receiver and the other . i) ii) Let t be the largest integer smaller than or equal to x divided by y. Let m be x - t × y.
19
20
21
22
23
24
25
26
iii) If m × y < 0, return an instance of the class Integer whose value is m + y. 138
1
iv) Otherwise, return an instance of the class Integer whose value is m. b) Otherwise, invoke the method coerce on the other with the receiver as the only argument. Let V be the resulting value. 1) If V is an instance of the class Array which contains two elements, let F and S be the first and the second element of V respectively. i) ii) 2) 15.2.8.2.6 Invoke the method % on F with S as the only argument. Return the resulting value.
2 3
4 5
6
7
8
Otherwise, raise a direct instance of the class TypeError. Integer#<=>
9
10
< =>( other ) Visibility: public Behavior: a) If the other is an instance of the class Integer: 1) If the value of the receiver is larger than the value of the other , return an instance of the class Integer whose value is 1. If the values of the receiver and the other are the same integer, return an instance of the class Integer whose value is 0. If the value of the receiver is smaller than the value of the other , return an instance of the class Integer whose value is -1.
11
12
13
14 15
16 17
2)
18 19
3)
20 21
b)
Otherwise, invoke the method coerce on the other with the receiver as the only argument. Let V be the resulting value. 1) If V is an instance of the class Array which contains two elements, let F and S be the first and the second element of V respectively. i) ii) Invoke the method <=> on F with S as the only argument. If this invocation does not result in an instance of the class Integer, the behavior is implementation dependent.
22 23
24
25 26
27
iii) Otherwise, return the value of this invocation. 2) 15.2.8.2.7 Otherwise, return nil. Integer#== 139
28
29
1
= =( other ) Visibility: public Behavior: a) If the other is an instance of the class Integer: 1) 2) b) If the values of the receiver and the other are the same integer, return true. Otherwise, return false.
2
3
4
5
6
7 8
Otherwise, invoke the method == on the other with the receiver as the argument. Return the resulting value of this invocation. Integer#˜
9
15.2.8.2.8
10
~ Visibility: public Behavior: The method returns an instance of the class Integer whose two’s complement representation is the one’s complement of the two’s complement representation of the receiver. 15.2.8.2.9 Integer#&
11
12 13 14
15
16
&( other ) Visibility: public Behavior: a) If the other is not an instance of the class Integer, the behavior is implementation dependent. Otherwise, return an instance of the class Integer whose two’s complement representation is the bitwise AND of the two’s complement representations of the receiver and the other . Integer#|
17
18
19 20
21 22 23
b)
24
15.2.8.2.10
25
|( other ) Visibility: public 140
26
1
Behavior: a) If the other is not an instance of the class Integer, the behavior is implementation dependent. Otherwise, return an instance of the class Integer whose two’s complement representation is the bitwise inclusive OR of the two’s complement representations of the receiver and the other . Integer#ˆ
2 3
4 5 6
b)
7
15.2.8.2.11
8
^( other ) Visibility: public Behavior: a) If the other is not an instance of the class Integer, the behavior is implementation dependent. Otherwise, return an instance of the class Integer whose two’s complement representation is the bitwise exclusive OR of the two’s complement representations of the receiver and the other . Integer#<<
9
10
11 12
13 14 15
b)
16
15.2.8.2.12
17
<<( other ) Visibility: public Behavior: a) If the other is not an instance of the class Integer, the behavior is implementation dependent. Otherwise, let x and y be the values of the receiver and the other . Return an instance of the class Integer whose value is the largest integer smaller than or equal to x × 2y . Integer#>>
18
19
20 21
22
b) c)
23 24
25
15.2.8.2.13
26
>>( other ) Visibility: public Behavior: 141
27
28
1 2
a)
If the other is not an instance of the class Integer, the behavior is implementation dependent. Otherwise, let x and y be the values of the receiver and the other . Return an instance of the class Integer whose value is the largest integer smaller than or equal to x × 2−y . Integer#ceil
3
b) c)
4 5
6
15.2.8.2.14
7
ceil Visibility: public Behavior: The method returns the receiver. 15.2.8.2.15 Integer#downto
8
9
10
11
downto( num, &block ) Visibility: public Behavior: a) If the num is not an instance of the class Integer, or the block is not given, the behavior is implementation dependent. Let i be the value of the receiver. If i is smaller than the value of the num, return the receiver. Call the block with an instance of the class Integer whose value is i. Decrement i by 1 and continue processing from Step c. Integer#eql?
12
13
14 15
16
b) c) d) e)
17
18
19
20
15.2.8.2.16
21
eql?( other ) Visibility: public Behavior: a) b) c) 142 If the other is not an instance of the class Integer, return false. Otherwise, invoke the method == on the other with the receiver as the argument. If this invocation results in a true value, return true. Otherwise, return false.
22
23
24
25
26
1
15.2.8.2.17
Integer#floor
2
floor Visibility: public Behavior: The method returns the receiver. 15.2.8.2.18 Integer#hash
3
4
5
6
hash Visibility: public Behavior: The method returns an implementation defined instance of the class Integer, which satisfies the following condition: a) b) Let I1 and I2 be instances of the class Integer. Let H1 and H2 be the resulting values of invocations of the method hash on I1 and I2 , respectively. The values of the H1 and H2 shall be the same integer, if and only if the values of I1 and I2 are the same integer. Integer#next
7
8 9
10
11 12
13 14
c)
15
15.2.8.2.19
16
next Visibility: public Behavior: The method returns an instance of the class Integer, whose value is the value of the receiver plus 1. 15.2.8.2.20 Integer#round
17
18 19
20
21
round Visibility: public Behavior: The method returns the receiver. 15.2.8.2.21 Integer#succ
22
23
24
143
1
succ Visibility: public Behavior: Same as the method next (see §15.2.8.2.19). 15.2.8.2.22 Integer#times
2
3
4
5
times( &block ) Visibility: public Behavior: a) b) c) d) e) If the block is not given, the behavior is implementation dependent. Let i be 0. If i is larger than or equal to the value of the receiver, return the receiver. Call the block with an instance of the class Integer whose value is i. Increment i by 1 and continue processing from Step c. Integer#to f
6
7
8
9
10
11
12
13
15.2.8.2.23
14
to f Visibility: public Behavior: The method returns a direct instance of the class Float whose value is the value of the receiver as a floating point number. 15.2.8.2.24 Integer#to i
15
16 17
18
19
to i Visibility: public Behavior: The method returns the receiver. 15.2.8.2.25 Integer#truncate
20
21
22
144
1
truncate Visibility: public Behavior: The method returns the receiver. 15.2.8.2.26 Integer#upto
2
3
4
5
upto( num, &block ) Visibility: public Behavior: a) If the num is not an instance of the class Integer, or the block is not given, the behavior is implementation dependent. Let i be the value of the receiver. If i is larger than the value of the num, return the receiver. Call the block with an instance of the class Integer whose value is i. Increment i by 1 and continue processing from Step c. Float
6
7
8 9
10
b) c) d) e) 15.2.9
11
12
13
14
15 16
Instances of the class Float represent floating point numbers. A conforming processor should use the native binary floating point representation of the underlying platform. When an arithmetic operation involving floating point numbers results in a value which cannot be represented exactly as an instance of the class Float, how the result is rounded to fit in the representation of an instance of the class Float is implementation defined. If the underlying platform of a conforming processor supports IEC 60559:1989:
17 18 19
20
21 22
The representation of an instance of the class Float should be the 64-bit double format as specified in §3.2.2 of IEC 60559:1989. If an arithmetic operation involving floating point numbers results in NaN while invoking a method of the class Float, the behavior of the method is implementation dependent.
23 24
25 26 27 28
Instances of the class Float shall not be created by the method new of the class Float. Therefore, a conforming processor shall undefine the singleton method new of the class Float, as if by invoking the method undef method on the eigenclass of the class Float with a direct instance of the class Symbol whose name is “new” as the argument (see §15.2.2.3.42). 15.2.9.1 Direct superclass
29
30
The class Numeric 145
1
15.2.9.2 15.2.9.2.1
Instance methods Float#+
2
3
+( other ) Visibility: public Behavior: a) If the other is an instance of the class Float, return a direct instance of the class Float whose value is the sum of the values of the receiver and the other . If the other is an instance of the class Integer, let R be the value of the other as a floating point number. Return a direct instance of the class Float whose value is the sum of R and the value of the receiver. c) Otherwise, invoke the method coerce on the other with the receiver as the only argument. Let V be the resulting value. 1) If V is an instance of the class Array which contains two elements, let F and S be the first and the second element of V respectively. i) ii) 2) 15.2.9.2.2 Invoke the method + on F with S as the only argument. Return the resulting value.
4
5
6 7
8 9
b)
10 11
12 13
14 15
16
17
18
Otherwise, raise a direct instance of the class TypeError. Float#−
19
20
-( other ) Visibility: public Behavior: a) If the other is an instance of the class Float, return a direct instance of the class Float whose value is the result of subtracting the value of the other from the value of the receiver. If the other is an instance of the class Integer, let R be the value of the other as a floating point number. Return a direct instance of the class Float whose value is the result of subtracting R from the value of the receiver. 146
21
22
23 24 25
26 27
b)
28 29
1 2
c)
Otherwise, invoke the method coerce on the other with the receiver as the only argument. Let V be the resulting value. 1) If V is an instance of the class Array which contains two elements, let F and S be the first and the second element of V respectively. i) ii) 2) Invoke the method - on F with S as the only argument. Return the resulting value.
3 4
5
6
7
Otherwise, raise a direct instance of the class TypeError. Float#*
8
15.2.9.2.3
9
- ( other ) Visibility: public Behavior: a) If the other is an instance of the class Float, return a direct instance of the class Float whose value is the result of multiplication of the values of the receiver and the other . If the other is an instance of the class Integer, let R be the value of the other as a floating point number. Return a direct instance of the class Float whose value is the result of multiplication of R and the value of the receiver. c) Otherwise, invoke the method coerce on the other with the receiver as the only argument. Let V be the resulting value. 1) If V is an instance of the class Array which contains two elements, let F and S be the first and the second element of V respectively. i) ii) 2) 15.2.9.2.4 Invoke the method * on F with S as the only argument. Return the resulting value.
10
11
12 13
14 15
b)
16 17
18 19
20 21
22
23
24
Otherwise, raise a direct instance of the class TypeError. Float#/
25
26
/( other ) Visibility: public Behavior: 147
27
28
1 2
a)
If the other is an instance of the class Float, return a direct instance of the class Float whose value is the value of the receiver divided by the value of the other . If the other is an instance of the class Integer, let R be the value of the other as a floating point number. Return a direct instance of the class Float whose value is the value of the receiver divided by R.
3 4
b)
5 6
7 8
c)
Otherwise, invoke the method coerce on the other with the receiver as the only argument. Let V be the resulting value. 1) If V is an instance of the class Array which contains two elements, let F and S be the first and the second element of V respectively. i) ii) 2) Invoke the method / on F with S as the only argument. Return the resulting value.
9 10
11
12
13
Otherwise, raise a direct instance of the class TypeError. Float#%
14
15.2.9.2.5
15
%( other ) Visibility: public Behavior: In the following steps, binary operators +, −, and ∗ represent floating point arithmetic operations addition, subtraction, and multiplication which are used in the instance methods +, -, and * of the class Float, respectively. The operator ∗ has a higher precedence than the operators + and −. a) If the other is an instance of the class Integer or the class Float: Let x be the value of the receiver. 1) If the other is an instance of the class Float, let y be the value of the other . If the other is an instance of the class Integer, let y be the value of the other as a floating point number. i) ii) Let t be the largest integer smaller than or equal to x divided by y. Let m be x − t ∗ y.
16
17 18 19 20
21
22
23 24 25
26
27
28 29
iii) If m ∗ y < 0, return a direct instance of the class Float whose value is m + y. iv) Otherwise, return a direct instance of the class Float whose value is m. b) Otherwise, invoke the method coerce on the other with the receiver as the only argument. Let V be the resulting value.
30
31 32
148
1 2
1)
If V is an instance of the class Array which contains two elements, let F and S be the first and the second element of V respectively. i) ii) Invoke the method % on F with S as the only argument. Return the resulting value.
3
4
5
2) 15.2.9.2.6
Otherwise, raise a direct instance of the class TypeError. Float#<=>
6
7
< =>( other ) Visibility: public Behavior: a) If the other is an instance of the class Integer or the class Float: 1) Let a be the value of the receiver. If the other is an instance of the class Float, let b be the value of the other . Otherwise, let b be the value of the other as a floating point number. If a conforming processor supports IEC 60559:1989, and if a or b is NaN, then return an implementation defined value. If a > b, return an instance of the class Integer whose value is 1. If a = b, return an instance of the class Integer whose value is 0. If a < b, return an instance of the class Integer whose value is -1.
8
9
10
11 12 13
14 15
2)
16
3) 4) 5) b)
17
18
19 20
Otherwise, invoke the method coerce on the other with the receiver as the only argument. Let V be the resulting value. 1) If V is an instance of the class Array which contains two elements, let F and S be the first and the second element of V respectively. i) ii) Invoke the method <=> on F with S as the only argument. If this invocation does not result in an instance of the class Integer, the behavior is implementation dependent.
21 22
23
24 25
26
iii) Otherwise, return the value of this invocation. 2) 15.2.9.2.7 Otherwise, return nil. Float#==
27
28
149
1
= =( other ) Visibility: public Behavior: a) If the other is an instance of the class Float: 1) If a conforming processor supports IEC 60559:1989, and if the value of the receiver is NaN, then return false. If the values of the receiver and the other are the same number, return true. Otherwise, return false.
2
3
4
5 6
7
2) 3) b)
8
9
If the other is an instance of the class Integer: 1) If the values of the receiver and the other are the mathematically the same, return true. Otherwise, return false.
10 11
12
2) c)
13 14
Otherwise, invoke the method == on the other with the receiver as the argument and return the resulting value of this invocation. Float#ceil
15
15.2.9.2.8
16
ceil Visibility: public Behavior: The method returns an instance of the class Integer whose value is the smallest integer larger than or equal to the value of the receiver. 15.2.9.2.9 Float#finite?
17
18 19
20
21
finite? Visibility: public Behavior: a) b) If the value of the receiver is a finite number, return true. Otherwise, return false. Float#floor
22
23
24
25
26
15.2.9.2.10 150
1
floor Visibility: public Behavior: The method returns an instance of the class Integer whose value is the largest integer smaller than or equal to the value of the receiver. 15.2.9.2.11 Float#infinite?
2
3 4
5
6
infinite? Visibility: public Behavior: a) If the value of the receiver is the positive infinite, return an instance of the class Integer whose value is 1. If the value of the receiver is the negative infinite, return an instance of the class Integer whose value is -1. Otherwise, return nil. Float#round
7
8
9 10
11 12
b)
13
c)
14
15.2.9.2.12
15
round Visibility: public Behavior: The method returns an instance of the class Integer whose value is the nearest integer to the value of the receiver. If there are two integers equally distant from the value of the receiver, the one which has the larger absolute value is chosen. 15.2.9.2.13 Float#to f
16
17 18 19
20
21
to f Visibility: public Behavior: The method returns the receiver. 15.2.9.2.14 Float#to i
22
23
24
151
1
to i Visibility: public Behavior: The method returns an instance of the class Integer whose value is the integer part of the receiver. 15.2.9.2.15 Float#truncate
2
3 4
5
6
truncate Visibility: public Behavior: Same as the method to i (see §15.2.9.2.14). 15.2.10 String
7
8
9
10
Instances of the class String represent sequences of characters. An instance of the class String which does not contain any character is said to be empty. An instance of the class String shall be empty when it is created by Step b of the method new of the class Class. The notation “an instance of the class Object which represents the character C ” means either of the following:
11 12 13
14 15
16
An instance of the class Integer whose value is the character code of C. An instance of the class String whose content is the single character C.
17
18 19
A conforming processor shall choose one of the above representations and use the same representation wherever this notation is used. The notation “the nth character of a string” means the character whose index is n counted up from 0. 15.2.10.1 Direct superclass
20 21
22
23
The class Object 15.2.10.2 Included modules
24
25
The following modules are included in the class String.
26
X
Comparable Instance methods String#*
27
15.2.10.3 15.2.10.3.1 152
28
1
- ( num ) Visibility: public Behavior: a) If the num is not an instance of the class Integer, the behavior is implementation dependent. Let n be the value of the num. If n is smaller than 0, raise a direct instance of the class ArgumentError. Otherwise, let C be the content of the receiver. Create a direct instance of the class String S the content of which is C repeated n times. Return S. String#+
2
3
4 5
6
b) c) d) e)
7
8
9 10
11
f)
12
15.2.10.3.2
13
+( other ) Visibility: public Behavior: a) If the other is not an instance of the class String, the behavior is implementation dependent. Let S and O be the contents of the receiver and the other respectively. Return a newly created instance of the class String the content of which is the concatenation of S and O. String#<=>
14
15
16 17
18
b) c)
19 20
21
15.2.10.3.3
22
< =>( other ) Visibility: public Behavior: a) If the other is not an instance of the class String, the behavior is implementation dependent. 153
23
24
25 26
1
b) c) d) e) f)
Let S1 and S2 be the contents of the receiver and the other respectively. If both S1 and S2 are empty, return an instance of the class Integer whose value is 0. If S1 is empty, return an instance of the class Integer whose value is -1. If S2 is empty, return an instance of the class Integer whose value is 1. Let a, b be the character codes of the first characters of S1 and S2 respectively. 1) 2) 3) If a > b, return an instance of the class Integer whose value is 1. If a < b, return an instance of the class Integer whose value is -1. Otherwise, replace S1 and S2 with S1 and S2 excluding their first characters, respectively. Continue processing from Step c. String#==
2
3
4
5
6
7
8 9
10
15.2.10.3.4
11
= =( other ) Visibility: public Behavior: a) If the other is not an instance of the class String, the behavior is implementation dependent. If the other is an instance of the class String: 1) 2) 15.2.10.3.5 If the content of the receiver and the other is the same, return true. Otherwise, return false. String#=˜
12
13
14 15
16
b)
17
18
19
20
=~( regexp ) Visibility: public Behavior: a) If the regexp is not an instance of the class Regexp, the behavior is implementation dependent. Otherwise, behave as if the method match is invoked on the regexp with the receiver as the argument (see §15.2.15.6.7).
21
22
23 24
25 26
b)
154
1
15.2.10.3.6
String#[]
2
[]( *args ) Visibility: public Behavior: a) If the length of the args is 0 or larger than 2, raise a direct instance of the class ArgumentError. Let P be the first element of the args. Let n be the length of the receiver. If P is an instance of the class Integer, let b be the value of P. 1) If the length of the args is 1: i) If b is smaller than 0, increment b by n. If b is still smaller than 0, return nil. If b ≥ n, return nil.
3
4
5 6
7
b) c)
8
9
10 11
12
ii)
13 14
iii) Create an instance of the class Object which represents the bth character of the receiver and return this instance. 2) If the length of the args is 2: i) If the last element of the args is an instance of the class Integer, let l be the value of the instance. Otherwise, the behavior is implementation dependent. If l is smaller than 0, or b is larger than n, return nil.
15
16 17
18
ii)
19 20
iii) If b is smaller than 0, increment b by n. If b is still smaller than 0, return nil. iv) If b + l is larger than n, let l be n - b. v) If l is smaller than or equal to 0, create an empty direct instance of the class String and return the instance.
21
22 23
24 25 26
vi) Otherwise, create a direct instance of the class String whose content is the (n-l ) characters of the receiver, from the bth index, preserving their order. Return the instance. d) If P is an instance of the class Regexp: 1) 2) If the length of the args is 1, let i be 0. If the length of the args is 2, and the last element of args is an instance of the class Integer, let i be the value of the instance. Otherwise, the behavior is implementation dependent. 155
27
28
29 30 31
1 2
3)
Match the pattern of P against the content of the receiver. (see §15.2.15.3 and Step 15.2.15.4). Let M be the result of the matching process. If M is nil, return nil. If i is larger than the length of the match result of M, return nil. If i is smaller than 0, increment i by the length of the match result of M. If i is still smaller than or equal to 0, return nil. Let m be the i th element of the match result of M. Create a direct instance of the class String whose content is the first element of m and return the instance.
3
4) 5) 6)
4
5 6
7 8
7)
9
e)
If P is an instance of the class String: 1) 2) If the length of the args is 2, the behavior is implementation dependent. If the receiver includes the content of P as a substring, create a direct instance of the class String whose content is equal to the content P and return the instance. Otherwise, return nil.
10
11 12
13
3) f)
14
Otherwise, the behavior is implementation dependent. String#capitalize
15
15.2.10.3.7
16
capitalize Visibility: public Behavior: The method returns a newly created instance of the class String which contains all the characters of the receiver, except:
17
18 19
20 21
If the first character of the receiver is a lower-case character, the first character of the resulting instance is the corresponding upper-case character. If the i th character of the receiver (where i > 0) is an upper case character, the i th character of the resulting instance is the corresponding lower-case character. String#capitalize!
22 23
24
15.2.10.3.8
25
capitalize! Visibility: public Behavior: a) Let s be the content of the instance of the class String returned when the method capitalize is invoked on the receiver.
26
27
28 29
156
1 2
b)
If the content of the receiver and s are the same, return nil. Otherwise, change the content of the receiver to s, and return the receiver. String#chomp
3
15.2.10.3.9
4
chomp( rs =”\n” ) Visibility: public Behavior: a) If the rs is nil, return a newly created instance of the class String whose content is the same as the receiver. If the receiver is empty, return a newly created empty instance of the class String. If rs is not an instance of the class String, the behavior is implementation dependent. Otherwise, return a newly created instance of the class String whose content is the same as the receiver, except the following characters: 1) If the rs consists of only one character 0x0a, the line-terminator on the end, if any, is excluded. If the rs is empty, the sequence of line-terminator s on the end, if any, is excluded. Otherwise, if the receiver ends with the content of rs, this sequence of the characters at the end of the receiver is excluded. String#chomp!
5
6
7 8
9
b) c) d)
10
11 12
13 14
15
2) 3)
16 17
18
15.2.10.3.10
19
chomp!( rs =”\n” ) Visibility: public Behavior: a) Let s be the content of the instance of the class String returned when the method chomp is invoked on the receiver with the rs as the argument. If the content of the receiver and s are the same, return nil. Otherwise, change the content of the receiver to s, and return the receiver. String#chop
20
21
22 23
24 25
b)
26
15.2.10.3.11
157
1
chop Visibility: public Behavior: a) b) If the receiver is empty, return a newly created empty instance of the class String. Otherwise, return a newly created instance of the class String whose content is the receiver without the last character. If the last character is 0x0a, and the character just before the 0x0a is 0x0d, the 0x0d is also dropped. String#chop!
2
3
4
5 6 7
8
15.2.10.3.12
9
chop! Visibility: public Behavior: a) Let s be the content of the instance of the class String returned when the method chop is invoked on the receiver. If the content of the receiver and s are the same, return nil. Otherwise, change the content of the receiver to s, and return the receiver. String#downcase
10
11
12 13
14 15
b)
16
15.2.10.3.13
17
downcase Visibility: public Behavior: The method returns a newly created instance of the class String which contains all the characters of the receiver, with the upper-case characters replaced with the corresponding lower-case characters. 15.2.10.3.14 String#downcase!
18
19 20 21
22
23
downcase! Visibility: public Behavior: a) Let s be the content of the instance of the class String returned when the method downcase is invoked on the receiver.
24
25
26 27
158
1 2
b)
If the content of the receiver and s are the same, return nil. Otherwise, change the content of the receiver to s, and return the receiver. String#each line
3
15.2.10.3.15
4
each line( &block ) Visibility: public Behavior: Let s be the content of the receiver. Let c be the first character of s. a) b) If the block is not given, the behavior is implementation dependent. Find the first 0x0a in s from c. If there is such a 0x0a: 1) 2) Let d be that 0x0a. Create a direct instance of the class String S whose content is the sequence of the characters from c to d. Call the block with S as the argument. If d is the last character of s, return the receiver. Otherwise, let new c be the character just after d and continue processing from Step b.
5
6
7
8
9
10 11
12
3) 4)
13 14
15 16 17
c)
If there is not such a 0x0a, create a direct instance of the class String whose content is the sequence of the characters from c to the last character of s. Call the block with this instance as the argument. Return the receiver. String#empty?
18
d)
19
15.2.10.3.16
20
empty? Visibility: public Behavior: a) b) If the receiver is empty, return true. Otherwise, return false. String#eql?
21
22
23
24
25
15.2.10.3.17
159
1
eql?( other ) Visibility: public Behavior: a) If the other is an instance of the class String: 1) 2) b) If the contents of the receiver and the other are the same, return true. Otherwise, return false.
2
3
4
5
6
7
If the other is not an instance of the class String, return false. String#gsub
8
15.2.10.3.18
9
gsub( *args, &block ) Visibility: public Behavior: a) If the length of the args is 0 or larger than 2, or the length of the args is 1 and the block is not given, raise a direct instance of the class ArgumentError. Let P be the first element of the args. If P is not an instance of the class Regexp, or the length of the args is 2 and the last element of the args is not an instance of the class String, the behavior is implementation dependent. Let S be the content of the receiver, and let l be the length of S. Let L be an empty list and let n be an integer 0. Match the pattern of P against S at the offset n (see §15.2.15.3 and Step 15.2.15.4). Let M be the result of the matching process. If M is nil, append to L the substring of S beginning at the nth character up to the last character of S. Otherwise: 1) If the length of the args is 1: i) Call the block with a direct instance of the class String whose content is the matched substring of M as the argument. Let V be the resulting value of this call. If V is not an instance of the class String, the behavior is implementation dependent.
10
11
12 13
14 15 16
b)
17
c) d) e)
18
19 20
21 22
f)
23
g)
24
25 26
27 28
ii)
160
1 2 3
2)
Let pre be the pre-match of M. Append to L the substring of pre beginning at the nth character up to the last character of pre, unless n is larger than the offset of the last character of pre. If the length of the args is 1, append the content of V to L. If the length of the args is 2, append to L the content of the last element of the args. Let post be the post-match of M. Let i be the offset of the first character of post within S. i) If i is equal to n, i.e. if P matched an empty string: I) Append to L a string whose content is the i th character of S.
4 5
3)
6 7
4)
8
9
10
II) Increment n by 1. ii) 5) h) Otherwise, replace n with i.
11
12
If n < l, continue processing from Step e.
13 14
Create a direct instance of the class String whose content is the concatenation of all the elements of L, and return the instance. String#gsub!
15
15.2.10.3.19
16
gsub!( *args, &block ) Visibility: public Behavior: a) Let s be the content of the instance of the class String returned when the method gsub is invoked on the receiver with the same arguments. If the content of the receiver and s are the same, return nil. Otherwise, change the content of the receiver to s, and return the receiver. String#hash
17
18
19 20
21 22
b)
23
15.2.10.3.20
24
hash Visibility: public Behavior: The method returns an implementation defined instance of the class Integer which satisfies the following condition: a) Let S1 and S2 be two distinct instances of the class String.
25
26 27
28
161
1 2
b)
Let H1 and H2 be the resulting values of the invocations of the method hash on S1 and S2 respectively. If and only if S1 and S2 has the same content, the values of H1 and H2 shall be the same integer. String#include?
3 4
c)
5
15.2.10.3.21
6
include?( obj ) Visibility: public Behavior: a) If the obj is an instance of the class Integer: If the receiver includes the character whose character code is the obj , return true. Otherwise, return false. b) If the obj is an instance of the class String: If there exists a substring of the receiver whose sequence of characters is the same as the obj , return true. Otherwise, return false. c) Otherwise, the behavior is implementation dependent. String#initialize
7
8
9
10 11
12
13 14
15
16
15.2.10.3.22
17
initialize( str =”” ) Visibility: private Behavior: a) If the str is not an instance of the class String, the behavior is implementation dependent. Otherwise, initialize the content of the receiver to the same sequence of characters as the content of the str . Return an implementation defined value. String#initialize copy
18
19
20 21
22 23
b)
24
c)
25
15.2.10.3.23
26
initialize copy( original ) Visibility: private 162
27
1
Behavior: a) If the original is not an instance of the class String, the behavior is implementation dependent. Change the content of the receiver to the content of the original . Return an implementation defined value. String#intern
2 3
4
b) c)
5
6
15.2.10.3.24
7
intern Visibility: public Behavior: a) If the length of the receiver is 0, or the receiver contains 0x00, raise a direct instance of the class ArgumentError. Otherwise, return a direct instance of the class Symbol whose name is the content of the receiver. String#length
8
9
10 11
12 13
b)
14
15.2.10.3.25
15
length Visibility: public Behavior: The method returns the number of characters of the content of the receiver. 15.2.10.3.26 String#match
16
17
18
19
match( regexp ) Visibility: public Behavior: a) b) If the regexp is an instance of the class Regexp, let R be the regexp. If the regexp is an instance of the class String, create a direct instance of the class Regexp as if the method new is invoked on the class Regexp with the regexp as the argument. Let R be the instance of the class Regexp. Otherwise, the behavior is implementation dependent. 163
20
21
22
23 24 25
26
c)
1
d) e)
Invoke the method match on R with the receiver as the argument. Return the resulting value of the invocation. String#replace
2
3
15.2.10.3.27
4
replace( other ) Visibility: public Behavior: Same as the method initialize copy (see §15.2.10.3.23). 15.2.10.3.28 String#reverse
5
6
7
8
reverse Visibility: public Behavior: The method returns a newly created instance of the class String which contains all the characters of the content of the receiver in the reverse order. 15.2.10.3.29 String#reverse!
9
10 11
12
13
reverse! Visibility: public Behavior: a) Change the content of the receiver to the content of the resulting instance of the class String when the method reverse is invoked on the receiver. Return the receiver. String#scan
14
15
16 17
18
b)
19
15.2.10.3.30
20
scan( reg, &block ) Visibility: public Behavior: a) If the reg is not an instance of the class Regexp, the behavior is implementation dependent. If the block is not given, create an empty direct instance of the class Array A.
21
22
23 24
25
b) 164
1
c) d) e)
Let S be the content of the receiver, and let l be the length of S. Let n be an integer 0. Match the pattern of the reg against S at the offset n (see §15.2.15.3 and Step 15.2.15.4). Let M be the result of the matching process. If M is not nil: 1) 2) Let L be the match result of M. If the length of L is 1, create a direct instance of the class String V whose content is the matched substring of M. If the length of L is larger than 1: i) ii) Create an empty direct instance of the class Array V. Except for the first element, for each element e of L, in the same order in the list, append to V a direct instance of the class String whose content is the first element of e.
2
3 4
5
f)
6
7 8
9
3)
10
11 12 13
14 15
4)
If the block is given, call the block with V as the argument. Otherwise, append V to A. Let post be the post-match of M. Let i be the offset of the first character of post within S. i) If i and n are the same, i.e. if the reg matched the empty string, increment n by 1. Otherwise, replace n with i.
16 17
5)
18 19
20
ii) 6) g)
21
If n < l, continue processing from Step e.
22
If the block is given, return the receiver. Otherwise, return A. String#size
23
15.2.10.3.31
24
size Visibility: public Behavior: Same as the method length (see §15.2.10.3.25). 15.2.10.3.32 String#slice
25
26
27
165
1
slice( *args ) Visibility: public Behavior: Same as the method [] (see §15.2.10.3.6). 15.2.10.3.33 String#split
2
3
4
5
split( sep ) Visibility: public Behavior: a) If the sep is not an instance of the class Regexp, the behavior is implementation dependent. Create an empty direct instance of the class Array A. Let S be the content of the receiver, and let l be the length of S. Let both sp and bp be 0, and let was-empty be false. Match the pattern of the sep against S at the offset sp (see §15.2.15.3 and Step 15.2.15.4). Let M be the result of the matching process. If M is nil, append to A a direct instance of the class String whose content is the substring of S beginning at the spth character up to the last character of S. Otherwise: 1) If the matched substring of M is an empty string: i) If was-empty is true, append to A a direct instance of the class String whose content is the bpth character of S. Otherwise, increment sp by 1. If sp < l, replace was-empty with true and continue processing from Step e.
6
7
8 9
10
b) c) d) e)
11
12
13 14
15 16
f)
17
g)
18
19 20
21 22
ii)
23 24 25 26
2)
Otherwise, replace was-empty with false. Let pre be the pre-match of M. Append to A an instance of the class String whose content is the substring of pre beginning at the bpth character up to the last character of pre, unless bp is larger than the offset of the last character of pre. Let L be the match result of M. If the length of L is larger than 1, except for the first element, for each element e of L, in the same order in the list, take the following steps:
27
3) 4)
28 29
166
1
i) ii)
Let c be the first element of e. If c is not nil, append to A a direct instance of the class String whose content is c.
2 3
4 5
5)
Let post be the post-match of M, and replace both sp and bp with the offset of the first character of post. If sp > l, continue processing from Step e.
6
6) h)
7 8
If the last element of A is an instance of the class String whose content is empty, remove the element. Repeat this step until the this condition does not hold. return A. String#sub
9
i)
10
15.2.10.3.34
11
sub( *args, &block ) Visibility: public Behavior: a) If the length of the args is 1 and the block is given, or the length of the args is 2: 1) If the first element of the args is not an instance of the class Regexp, the behavior is implementation dependent. Match the pattern of the first element of the args against the content of the receiver (see §15.2.15.3 and Step 15.2.15.4). Let M be the result of the matching process. If M is nil, create a direct instance of the class String whose content is the same as the receiver and return the instance. Otherwise: i) If the length of the args is 1, call the block with a direct instance of the class String whose content is the matched substring of M as the argument. Let S be the resulting value of this call. If S is not an instance of the class String, the behavior is implementation dependent. If the length of the args is 2, let S be the last element of the args. If S is not an instance of the class String, the behavior is implementation dependent.
12
13
14
15 16
17 18
2)
19 20
3)
21
4)
22 23 24 25
26 27
ii)
28 29 30
iii) Create a direct instance of the class String whose content is the concatenation of pre-match of M, the content of S, and post-match of M, and return the instance. b) Otherwise, raise a direct instance of the class ArgumentError.
31
167
1
15.2.10.3.35
String#sub!
2
sub!( *args, &block ) Visibility: public Behavior: a) Let s be the content of the instance of the class String returned when the method sub is invoked on the receiver with the same arguments. If the content of the receiver and s are the same, return nil. Otherwise, change the content of the receiver to s, and return the receiver. String#upcase
3
4
5 6
7 8
b)
9
15.2.10.3.36
10
upcase Visibility: public Behavior: The method returns a newly created instance of the class String which contains all the characters of the receiver, with all the lower-case characters replaced with the corresponding upper-case characters. 15.2.10.3.37 String#upcase!
11
12 13 14
15
16
upcase! Visibility: public Behavior: a) Let s be the content of the instance of the class String returned when the method upcase is invoked on the receiver. If the content of the receiver and s are the same, return nil. Otherwise, change the content of the receiver to s, and return the receiver. String#to i
17
18
19 20
21 22
b)
23
15.2.10.3.38
24
to i( base =10 ) Visibility: public Behavior: 168
25
26
1 2
a)
If the base is not an instance of the class Integer whose value is 2, 8, 10, nor 16, the behavior is implementation dependent. Otherwise, let b be the value of the base. If the receiver is empty, return an instance of the class Integer whose value is 0. Let i be 0. Increment i by 1 while the i th character of the receiver is a whitespace. If the i th character of the receiver is “+” or “-”, increment i by 1. If the i th character of the receiver is “0”, and any of the following conditions holds, increment i by 2: Let c be the character of the receiver whose index is i plus 1.
3
b) c) d) e)
4
5
6 7
8
9
b is 2, and c is “b” or “B”. b is 8, and c is “o” or “O”. b is 10, and c is “d” or “D”. b is 16, and c is “x” or “X”.
10
11
12
13
f)
Let s be a sequence of the following characters of the receiver from the i th index:
14
If b is 2, binary-digit and “ ”. If b is 8, octal-digit and “ ”. If b is 10, decimal-digit and “ ”. If b is 16, hexadecimal-digit and “ ”.
15
16
17
18
g) h)
If the length of s is 0, return an instance of the class Integer whose value is 0. If s starts with “ ”, or s contains successive “ ”s, the behavior is implementation dependent. Let n be the value of s, computed in base b. If the “-” occurrs in Step d, return an instance of the class Integer whose value is −n. Otherwise, return an instance of the class Integer whose value is n.
19 20
21
i)
22 23
24
15.2.10.3.39
String#to f
25
to f Visibility: public Behavior: a) If the receiver is empty, return a direct instance of the class Float whose value is 0.0. 169
26
27
28
1 2 3
b)
If the receiver starts with the sequence of the characters which is a float-literal, return a direct instance of the class Float whose value is the value of the float-literal (see §8.5.5.1). If the receiver starts with the sequence of the characters which is a digit-decimalinteger-literal, return a direct instance of the class Float whose value is the value of the digit-decimal-integer-literal as a floating point number (see §8.5.5.1). Otherwise, return a direct instance of the class Float whose value is implementation defined. String#to s
4 5 6
c)
7 8
d)
9
15.2.10.3.40
10
to s Visibility: public Behavior: The method returns the receiver. 15.2.10.3.41 String#to sym
11
12
13
14
to sym Visibility: public Behavior: Same as the method intern (see §15.2.10.3.24). 15.2.11 Symbol
15
16
17
18 19
Instances of the class Symbol represent names (see §8.5.5.5). No two instances of the class Symbol have the same name. Instances of the class Symbol shall not be created by the method new of the class Symbol. Therefore, a conforming processor shall undefine the singleton method new of the class Symbol, as if by invoking the method undef method on the eigenclass of the class Symbol with a direct instance of the class Symbol whose name is “new” as the argument (see §15.2.2.3.42). 15.2.11.1 Direct superclass
20 21 22 23
24
25
The class Object 15.2.11.2 15.2.11.2.1 Instance methods Symbol#===
26
27
28
= = =( other ) Visibility: public 170
29
1
Behavior: Same as the method == of the module Kernel (see §15.3.1.2.1). 15.2.11.2.2 Symbol#id2name
2
3
id2name Visibility: public Behavior: The method returns an instance of the class String, the content of which represents the name of the receiver. 15.2.11.2.3 Symbol#to s
4
5 6
7
8
to s Visibility: public Behavior: Same as the method id2name (see §15.2.11.2.2). 15.2.11.2.4 Symbol#to sym
9
10
11
12
to sym Visibility: public Behavior: The method returns the receiver. 15.2.12 Array
13
14
15
16 17 18
Instances of the class Array represent arrays, which are unbounded. An instance of the class Array which has no element is said to be empty. The number of elements in an instance of the class Array is called its length. Instances of the class Array shall be empty when they are created by Step b of the method new of the class Class. Elements of an instance of the class Array has their indexes counted up from 0. Given an array A, operations append, prepend, remove are defined as follows: append: To append an object O to A is defined as follows: Insert O after the last element of A. Appending an object to A increases its length by 1. prepend: To prepend an object O to A is defined as follows: 171
19 20
21
22
23
24
25
26
1 2
Insert O to the first index of A. Original elements of A are moved toward the end of A by one position. Prepending an object to A increases its length by 1. remove: To remove an element X from A is defined as follows: a) b) Remove X from A. If X is not the last element of A, move the elements after X toward the head of A by one position.
3
4
5
6 7
8
Removing an object to A decreases its length by 1. 15.2.12.1 Direct superclass
9
10
The class Object 15.2.12.2 Included modules
11
12
The following module is included in the class Array.
13
X
Enumerable Singleton methods Array.[]
14
15.2.12.3 15.2.12.3.1
15
16
Array.[]( *items ) Visibility: public Behavior: The method returns a newly created instance of the class Array which contains the elements of the items, preserving their order. 15.2.12.4 15.2.12.4.1 Instance methods Array#*
17
18 19
20
21
22
- ( num ) Visibility: public Behavior: a) If the num is not an instance of the class Integer, the behavior is implementation dependent. If the value of the num is smaller than 0, raise a direct instance of the class ArgumentError.
23
24
25 26
27
b) 172
1
c) d)
If the value of the num is 0, return an empty direct instance of the class Array. Otherwise, create a direct instance of the class Array A and repeat the following for the num times: Append all the elements of the receiver to A, preserving their order.
2 3
4
5
e)
Return A. Array#+
6
15.2.12.4.2
7
+( other ) Visibility: public Behavior: a) If the other is an instance of the class Array, let A be the other . Otherwise, the behavior is implementation dependent. Create an empty direct instance of the class Array R. For each element of the receiver, in the indexing order, append the element to R. Then, for each element of A, in the indexing order, append the element to R. Return R. Array#<<
8
9
10 11
12
b) c)
13 14
15
d)
16
15.2.12.4.3
17
<<( obj ) Visibility: public Behavior: The method appends the obj to the receiver and return the receiver. 15.2.12.4.4 Array#[]
18
19
20
21
[]( *args ) Visibility: public Behavior: a) b) Let n be the length of the receiver. If the length of the args is 0, raise a direct instance of the class ArgumentError. 173
22
23
24
25
1
c)
If the length of the args is 1: 1) If the only argument is an instance of the class Integer, let k be the value of the only argument. Otherwise, the behavior is implementation dependent. If k < 0, increment k by n. If k is still smaller than 0, return nil. If k ≥ n, return nil. Otherwise, return the k th element of the receiver.
2 3
4
2) 3) 4) d)
5
6
7
If the length of the args is 2: 1) If the elements of the args are instances of the class Integer, let b and l be the values of the first and the last element of the args, respectively. Otherwise, the behavior is implementation dependent. If b < 0, increment b by n. If b is still smaller than 0, return nil. If b = n, create an empty direct instance of the class Array and return this instance. If b > n or l < 0, return nil. If l > n − b, let new l be n − b. Create an empty direct instance of the class Array A. Append the l elements of the receiver to A, from the bth index, preserving their order. Return A.
8 9 10
11
2) 3) 4) 5) 6)
12
13
14
15 16
17
e)
If the length of the args is larger than 2, raise a direct instance of the class ArgumentError. Array#[]=
18
15.2.12.4.5
19
[] =( *args ) Visibility: public Behavior: a) b) c) Let n be the length of the receiver. If the length of the args is smaller than 2, raise a direct instance of the class ArgumentError. If the length of the args is 2: 1) If the first element of the args is an instance of the class Integer, let k be the value of the element and let V be the last element of the args. Otherwise, the behavior is implementation dependent. If k < 0, increment k by n. If k is still smaller than 0, raise a direct instance of the class IndexError.
20
21
22
23
24
25 26 27
28 29
2)
174
1
3) 4)
If k < n, replace the k th element of the receiver with V. Otherwise, expand the length of the receiver to k + 1. The last element of the receiver is V. If k > n, the elements whose index is from n to k - 1 is nil. Return V.
2 3
4
5) d) e)
5
If the length of the args is 3, the behavior is implementation dependent. If the length of the args is larger than 3, raise a direct instance of the class ArgumentError. Array#clear
6
7
15.2.12.4.6
8
clear Visibility: public Behavior: The method removes all the elements from the receiver and return the receiver. 15.2.12.4.7 Array#collect!
9
10
11
12
collect!( &block ) Visibility: public Behavior: a) If the block is given: 1) For each element of the receiver in the indexing order, call the block with the element as the only argument and replace the element with the resulting value. Return the receiver.
13
14
15
16 17
18
2) b)
19
If the block is not given, the behavior is implementation dependent. Array#concat
20
15.2.12.4.8
21
concat( other ) Visibility: public Behavior: a) If the other is not an instance of the class Array, the behavior is implementation dependent. 175
22
23
24 25
1
b) c)
Otherwise, append all the elements of the other to the receiver, preserving their order. Return the receiver. Array#each
2
3
15.2.12.4.9
4
each( &block ) Visibility: public Behavior: a) If the block is given: 1) For each element of the receiver in the indexing order, call the block with the element as the only argument. Return the receiver.
5
6
7
8 9
10
2) b)
11
If the block is not given, the behavior is implementation dependent. Array#each index
12
15.2.12.4.10
13
each index( &block ) Visibility: public Behavior: a) If the block is given: 1) For each element of the receiver in the indexing order, call the block with an argument, which is an instance of the class Integer whose value is the index of the element. Return the receiver.
14
15
16
17 18 19
20
2) b)
21
If the block is not given, the behavior is implementation dependent. Array#empty?
22
15.2.12.4.11
23
empty? Visibility: public Behavior: 176
24
25
1
a) b)
If the receiver is empty, return true. Otherwise, return false. Array#first
2
3
15.2.12.4.12
4
first( *args ) Visibility: public Behavior: a) If the length of the args is 0: 1) 2) b) If the receiver is empty, return nil. Otherwise, return the first element of the receiver.
5
6
7
8
9
10
If the length of the args is 1: 1) If the only argument is not an instance of the class Integer, the behavior is implementation dependent. Otherwise, let n be the value of the only argument. If n is smaller than 0, raise a direct instance of the class ArgumentError. Otherwise, let N be the smaller of n and the length of the receiver. Return a newly created instance of the class Array which contains the first N elements of the receiver, preserving their order.
11 12
13
2) 3) 4)
14
15 16
17
c)
If the length of args is larger than 1, raise a direct instance of the class ArgumentError. Array#initialize
18
15.2.12.4.13
19
initialize( size =0, obj =nil, &block ) Visibility: private Behavior: a) If the size is not an instance of the class Integer, the behavior is implementation dependent. Otherwise, let n be the value of the size. If n is smaller than 0, raise a direct instance of the class ArgumentError. If n is 0, return an implementation defined value. If n is larger than 0: 177
20
21
22 23
24
b) c) d)
25
26
1
1)
If the block is given: i) ii) Let k be 0. Call the block with an argument, which is an instance of the class Integer whose value is k. Append the resulting value of this call to the receiver.
2
3 4
5 6
iii) Increase k by 1. If k is equal to n, terminate this process. Otherwise, repeat from Step d-1-ii. 2) 3) 15.2.12.4.14 Otherwise, append the obj to the receiver n times. Return an implementation defined value. Array#initialize copy
7
8
9
10
initialize copy( original ) Visibility: private Behavior: a) If the original is not an instance of the class Array, the behavior is implementation dependent. Remove all the elements from the receiver. Append all the elements of the original to the receiver, preserving their order. Return an implementation defined value. Array#join
11
12
13 14
15
b) c) d)
16
17
18
15.2.12.4.15
19
join( sep =nil ) Visibility: public Behavior: a) If the sep is neither nil nor an instance of the class String, the behavior is implementation dependent. Let S be an empty direct instance of the class String. For each element X of the receiver, in the indexing order: 1) If the sep is not nil, and X is not the first element of the receiver, append the content of the sep to S.
20
21
22 23
24
b) c)
25
26 27
178
1
2) 3)
If X is an instance of the class String, append the content of X to S. If X is an instance of the class Array: i) If X is the receiver, i.e. if the receiver contains itself, append an implementation defined sequence of characters to S. Otherwise, append the content of the instance of the class String returned as if by the invocation of the method join on X with the sep as the argument.
2
3 4
5 6
ii)
7
4) d)
Otherwise, the behavior is implementation dependent.
8
Return S. Array#last
9
15.2.12.4.16
10
last( *args ) Visibility: public Behavior: a) If the length of the args is 0: 1) 2) b) If the receiver is empty, return nil. Otherwise, return the last element of the receiver.
11
12
13
14
15
16
If the length of the args is 1: 1) If the only argument is not an instance of the class Integer, the behavior is implementation dependent. Otherwise, let n be the value of the only argument. If n is smaller than 0, raise a direct instance of the class ArgumentError. Otherwise, let N be the smaller of n and the length of the receiver. Return a newly created instance of the class Array which contains the last N elements of the receiver, preserving their order.
17 18
19
2) 3)
20
21 22
23
c)
If the length of args is larger than 1, raise a direct instance of the class ArgumentError. Array#length
24
15.2.12.4.17
25
length Visibility: public Behavior: The method returns the number of elements of the receiver. 179
26
27
1
15.2.12.4.18
Array#map!
2
map!( &block ) Visibility: public Behavior: Same as the method collect! (see §15.2.12.4.7). 15.2.12.4.19 Array#pop
3
4
5
6
pop Visibility: public Behavior: a) b) If the receiver is empty, return nil. Otherwise, remove the last element from the receiver and return that element. Array#push
7
8
9
10
11
15.2.12.4.20
12
push( *items ) Visibility: public Behavior: a) b) For each element of the items, in the indexing order, append it to the receiver. Return the receiver. Array#replace
13
14
15
16
17
15.2.12.4.21
18
replace( other ) Visibility: public Behavior: Same as the method initialize copy (see §15.2.12.4.14). 15.2.12.4.22 Array#reverse
19
20
21
180
1
reverse Visibility: public Behavior: The method returns a newly created instance of the class Array which contains all the elements of the receiver in the reverse order. 15.2.12.4.23 Array#reverse!
2
3 4
5
6
reverse! Visibility: public Behavior: The method reverses the order of the elements of the receiver and return the receiver. 15.2.12.4.24 Array#shift
7
8 9
10
11
shift Visibility: public Behavior: a) b) If the receiver is empty, return nil. Otherwise, remove the first element from the receiver and return that element. Array#size
12
13
14
15
16
15.2.12.4.25
17
size Visibility: public Behavior: Same as the method length (see §15.2.12.4.17). 15.2.12.4.26 Array#slice
18
19
20
21
slice( *args ) Visibility: public Behavior: Same as the method [] (see §15.2.12.4.4). 181
22
23
1
15.2.12.4.27
Array#unshift
2
unshift( *items ) Visibility: public Behavior: a) b) 15.2.13 For each element of the items, in the reverse indexing order, prepend it to the receiver. Return the receiver. Hash
3
4
5
6
7
8
Instances of the class Hash represent hashes, which are sets of key/value pairs. An instance of the class Hash which has no key/value pair is said to be empty. Instances of the class Hash shall be empty when they are created by Step b of the method new of the class Class. A hash cannot contain more than one key/value pair for each key. An instance of the class Hash has the following property: default value or proc: Either of the followings:
9 10 11
12
13
14
15 16
A default value, which is returned by the method [] when the specified key is not found in the hash. A default proc, which is called to generate the return value of the method [] when the specified key is not found in the hash.
17 18
19 20
An instance of the class Hash shall not have both a default value and a default proc simultaneously. Given two keys K1 and K2 , the notation “K1 ≡ K2 ” means that all of the following conditions hold:
21 22
23 24
An invocation of the method eql? on K1 with K2 as the only argument evaluates to a true value. Let H1 and H2 be the results of invocations of the method hash on K1 and K2 , respectively. H1 and H2 are the instances of the class Integer which represents the same integer. A conforming processor may define a certain range of integers, and when the values of H1 or H2 lies outside of this range, an implementation shall convert H1 or H2 to another instance of the class Integer whose value is within the range. Let I1 and I2 be each of the resulting instances respectively. The values of I1 and I2 are the same integer.
25
26
27 28 29 30
31
182
1 2
If H1 or H2 is not an instance of the class Integer, whether K1 and K2 are considered to be the same is implementation dependent. Note that K1 ≡ K2 is not equivalent to K2 ≡ K1 . 15.2.13.1 Direct superclass
3
4
5
The class Object 15.2.13.2 Included modules
6
7
The following module is included in the class Hash.
8
X
Enumerable Instance methods Hash#==
9
15.2.13.3 15.2.13.3.1
10
11
= =( other ) Visibility: public Behavior: a) If the other is not an instance of the class Hash, the behavior is implementation dependent. If all of the following conditions hold, return true:
12
13
14 15
16
b)
17
The receiver and the other have the same number of key/value pairs. For each key/value pair P in the receiver, the other has a corresponding key/value pair Q which satisfies the following conditions: — The key of P ≡ the key of Q. — An invocation of the method == on the value of P with the value of Q results in a true value.
18 19
20
21 22
23
c)
Otherwise, return false. Hash#[]
24
15.2.13.3.2
25
[]( key ) Visibility: public 183
26
1
Behavior: a) If the receiver has a key/value pair P where the key ≡ the key of P, return the value of P. Otherwise, invoke the method default on the receiver with the key as the argument and return the resulting value. Hash#[]=
2 3
4 5
b)
6
15.2.13.3.3
7
[] =( key, value ) Visibility: public Behavior: a) If the receiver has a key/value pair P where the key ≡ the key of P, replace the value of P with the value. Otherwise: 1) If the key is a direct instance of the class String, create a copy of the key, i.e. create a direct instance of the class String K whose content is the same as the key. If the key is not an instance of the class String, let K be the key. If the key is an instance of a subclass of the class String, whether to create a copy or not is implementation defined. Store a pair of K and the value into the receiver.
8
9
10 11
12
b)
13 14 15
16
2) 3)
17 18
19
4) c)
20
Return the value. Hash#clear
21
15.2.13.3.4
22
clear Visibility: public Behavior: a) b) Remove all the key/value pairs from the receiver. Return the receiver. Hash#default
23
24
25
26
27
15.2.13.3.5
184
1
default( *args ) Visibility: public Behavior: a) b) c) If the length of the args is larger than 1, raise a direct instance of the class ArgumentError. If the receiver has the default value, return the value. If the receiver has the default proc: 1) 2) If the length of the args is 0, return nil. If the length of the args is 1, invoke the method call on the default proc of the receiver with two arguments, the receiver and the only element of the args. Return the resulting value of this invocation.
2
3
4
5
6
7
8 9 10
11
d)
Otherwise, return nil. Hash#default=
12
15.2.13.3.6
13
default =( value ) Visibility: public Behavior: a) b) c) If the receiver has the default proc, remove the default proc. Set the default value of the receiver to the value. Return the value. Hash#default proc
14
15
16
17
18
19
15.2.13.3.7
20
default proc Visibility: public Behavior: a) b) If the receiver has the default proc, return the default proc. Otherwise, return nil.
21
22
23
24
185
1
15.2.13.3.8
Hash#delete
2
delete( key, &block ) Visibility: public Behavior: a) If the receiver has a key/value pair P where the key ≡ the key of P, remove P from the receiver and return the value of P. Otherwise: 1) If the block is given, call the block with the key as the argument. Return the resulting value of this call. Otherwise, return nil. Hash#each
3
4
5 6
7
b)
8 9
10
2) 15.2.13.3.9
11
12
each( &block ) Visibility: public Behavior: a) If the block is given, call the block for each key/value pair of the receiver with an instance of the class Array as the argument, which contains two elements, the key and the value of that pair. The order of key/value pairs are implementation defined. Return the receiver. If the block is not given, the behavior is implementation dependent. Hash#each key
13
14
15 16 17 18
19
b)
20
15.2.13.3.10
21
each key( &block ) Visibility: public Behavior: a) If the block is given, for each key/value pair of the receiver, in an implementation defined order, call the block with the key of the pair as the argument. Return the receiver. If the block is not given, the behavior is implementation dependent.
22
23
24 25 26
27
b) 186
1
15.2.13.3.11
Hash#each value
2
each value( &block ) Visibility: public Behavior: a) If the block is given, call the block for each key/value pair of the receiver, with the value as the argument, in an implementation defined order. Return the receiver. If the block is not given, the behavior is implementation dependent. Hash#empty?
3
4
5 6
7
b)
8
15.2.13.3.12
9
empty? Visibility: public Behavior: a) b) If the receiver is empty, return true. Otherwise, return false. Hash#has key?
10
11
12
13
14
15.2.13.3.13
15
has key?( key ) Visibility: public Behavior: a) b) If the receiver has a key/value pair P where the key ≡ the key of P, return true. Otherwise, return false. Hash#has value?
16
17
18
19
20
15.2.13.3.14
21
has value?( value ) Visibility: public Behavior: 187
22
23
1 2
a)
If the receiver has a key/value pair whose value holds the following condition, return true.
3 4
An invocation of the method == on the value with the value as the argument result in a true value.
5
b)
Otherwise, return false. Hash#include?
6
15.2.13.3.15
7
include?( key ) Visibility: public Behavior: Same as the method has key? (see §15.2.13.3.13). 15.2.13.3.16 Hash#initialize
8
9
10
11
initialize( *args, &block ) Visibility: private Behavior: a) If the block is given, and the length of the args is not 0, raise a direct instance of the class ArgumentError. If the block is given and the length of the args is 0, set the default proc of the receiver to a direct instance of the class Proc which represents the block . If the block is not given: 1) 2) 3) If the length of the args is 0, let D be nil. If the length of the args is 1, let D be the only argument. If the length of the args is larger than 1, raise a direct instance of the class ArgumentError. Set the default value of the receiver to D.
12
13
14 15
16 17
b)
18
c)
19
20
21 22
23
4) d)
24
Return an implementation defined value. Hash#initialize copy
25
15.2.13.3.17
188
1
initialize copy( original ) Visibility: private Behavior: a) If the original is not an instance of the class Hash, the behavior is implementation dependent. Remove all the key/value pairs from the receiver. For each key/value pair P of the original , in an implementation defined order, store P in the receiver. Remove the default value and the default proc from the receiver. If the orignal has a default value, set the default value of the receiver to that value. If the orignal has a default proc, set the default proc of the receiver to that proc. Return an implementation defined value. Hash#key?
2
3
4 5
6
b) c)
7 8
9
d) e) f) g)
10
11
12
13
15.2.13.3.18
14
key?( key ) Visibility: public Behavior: Same as the method has key? (see §15.2.13.3.13). 15.2.13.3.19 Hash#keys
15
16
17
18
keys Visibility: public Behavior: The method returns a newly created instance of the class Array whose content is the keys of the receiver. The order of the keys stored in somewhere are implementation defined. 15.2.13.3.20 Hash#length
19
20 21 22
23
24
length Visibility: public Behavior: The method returns an instance of the class Integer whose value is the number of key/value pairs stored in the receiver. 189
25
26 27
1
15.2.13.3.21
Hash#member?
2
member?( key ) Visibility: public Behavior: Same as the method has key? (see §15.2.13.3.13). 15.2.13.3.22 Hash#merge
3
4
5
6
merge( other, &block ) Visibility: public Behavior: a) If the other is not an instance of the class Hash, the behavior is implementation dependent. Otherwise, create a direct instance of the class Hash H which has the same key/value pairs as the receiver. For each key/value pair P of the other , in an implementation defined order: 1) If the block is given: i) If H has the key/value pair Q where the key of P ≡ the key of Q, call the block with three arguments, the key of P, the value of Q, and the value of P. Store the key/value pair whose key is the key of P and whose value is the resulting value of this call. Otherwise, store P in H.
7
8
9 10
11 12
b)
13
c)
14
15 16 17 18
19
ii) 2) d)
20
If the block is not given, store P in H.
21
Return H. Hash#replace
22
15.2.13.3.23
23
replace( other ) Visibility: public Behavior: Same as the method initialize copy (see §15.2.13.3.17). 15.2.13.3.24 190 Hash#shift
24
25
26
1
shift Visibility: public Behavior: a) If the receiver has no key/value pairs: 1) If the receiver has the default proc, invoke the method call on the default proc with two arguments, the receiver and nil. Return the resulting value of this call. If the receiver has the default value, return the value. Otherwise, return nil.
2
3
4
5 6
7
2) 3) b)
8
9 10 11
Otherwise, choose a key/value pair P and remove P from the receiver. Return a newly created instance of the class Array which contains two elements, the key and the value of P. Which pair is chosen is implementation defined.
12
13
15.2.13.3.25
Hash#size
14
size Visibility: public Behavior: Same as the method length (see §15.2.13.3.20). 15.2.13.3.26 Hash#store
15
16
17
18
store( key, value ) Visibility: public Behavior: Same as the method []= (see §15.2.13.3.3). 15.2.13.3.27 Hash#value?
19
20
21
22
value?( value ) Visibility: public Behavior: Same as the method has value? (see §15.2.13.3.14).
23
24
191
1
15.2.13.3.28
Hash#values
2
values Visibility: public Behavior: The method returns a newly created instance of the class Array which contains all the values of the receiver. The order of the values stored are implementation defined. 15.2.14 Range
3
4 5
6
7
Instances of the class Range represent ranges between two values, the start and end point. An instance of the class Range has the following properties: start point: The value at the start of the range. end point: The value at the end of the range. exclusive flag: If this is true, the end point is excluded from the range. Otherwise, the end point is included in the range. When the method clone (see §15.3.1.2.8) or the method dup (see §15.3.1.2.9) of the class Kernel is invoked on an instance of the class Range, those properties shall be copied from the receiver to the resulting value. 15.2.14.1 Direct superclass
8
9
10
11 12
13 14 15
16
17
The class Object 15.2.14.2 Included modules
18
19
The following module is included in the class Range.
20
X
Enumerable Instance methods Range#==
21
15.2.14.3 15.2.14.3.1
22
23
= =( other ) Visibility: public Behavior: a) If all of the following conditions hold, return true:
24
25
26
27
the other is an instance of the class Range.
192
1 2
Let S be the start point of the other . Invocation of the method == on the start point of the receiver with S as the argument results in a true value. Let E be the end point of the other . Invocation of the method == on the end point of the receiver with E as the argument results in a true value. The exclusive flag of the receiver and the one of the other are the same boolean value.
3 4
5 6
7
b)
Otherwise, return false. Range#===
8
15.2.14.3.2
9
= = =( obj ) Visibility: public Behavior: a) If neither the start point of the receiver nor the end point of the receiver is an instance of the class Numeric, the behavior is implementation dependent. Invoke the method <=> on the start point of the receiver with the obj as the argument. Let S be the result of this invocation. 1) If S is not an instance of the class Integer, the behavior is implementation dependent. If the value of S is larger than 0, return false.
10
11
12 13
14 15
b)
16 17
18
2) c)
19 20
Invoke the method <=> on the obj with the end point of the receiver as the argument. Let E be the result of this invocation.
21 22
If E is not an instance of the class Integer, the behavior is implementation dependent. If the exclusive flag of the receiver is true, and the value of E is smaller than 0, return true. If the exclusive flag of the receiver is false, and the value of E is smaller than or equal to 0, return true. Otherwise, return false. Range#begin
23 24
25 26
27
28
15.2.14.3.3
29
begin Visibility: public 193
30
1
Behavior: The method returns the start point of the receiver. 15.2.14.3.4 Range#each
2
3
each( &block ) Visibility: public Behavior: a) b) If the block is not given, the behavior is implementation dependent. If an invocation of the method respond to? on the start point of the receiver with a direct instance of the class Symbol whose name is succ as the argument results in a false value, raise a direct instance of the class TypeError. Let V be the start point of the receiver. Invoke the method <=> on V with the end point of the receiver as the argument. Let C be the resulting value. 1) If C is not an instance of the class Integer, the behavior is implementation dependent. If the value of C is larger than 0, return the receiver. If the value of C is 0: i) ii) If the exclusive flag of the receiver is true, return the receiver. If the exclusive flag of the receiver is false, call the block with V as the argument, then, return the receiver.
4
5
6
7 8 9
10
c) d)
11 12
13 14
15
2) 3)
16
17
18 19
20
e) f)
Call the block with V as the argument. Invoke the method succ on V with no argument, and let new V be the resulting value. Continue processing from Step d.
21
22
23
15.2.14.3.5
Range#end
24
end Visibility: public Behavior: The method returns the end point of the receiver. 15.2.14.3.6 194 Range#exclude end?
25
26
27
1
exclude end? Visibility: public Behavior: The method returns the exclusive flag of the receiver. 15.2.14.3.7 Range#first
2
3
4
5
first Visibility: public Behavior: Same as the method begin (see §15.2.14.3.3). 15.2.14.3.8 Range#include?
6
7
8
9
include?( obj ) Visibility: public Behavior: Same as the method === (see §15.2.14.3.2). 15.2.14.3.9 Range#initialize
10
11
12
13
initialize( left, right, exclusive =false ) Visibility: public Behavior: a) Invoke the method <=> on the left with the right as the argument. If an exception is raised and not handled during this invocation, raise a direct instance of the class ArgumentError. If the result of this invocation is not an instance of the class Integer, the behavior is implementation dependent. If the exclusive is a true value, let F be true. Otherwise, let F be false. Set the start point, end point, and exclusive flag of the receiver to the left, the right, and F, respectively. Return an implementation defined value. Range#last
14
15
16 17 18 19
20
b) c)
21 22
23
d)
24
15.2.14.3.10
195
1
last Visibility: public Behavior: Same as the method end (see §15.2.14.3.5). 15.2.14.3.11 Range#member?
2
3
4
5
member?( obj ) Visibility: public Behavior: Same as the method === (see §15.2.14.3.2). 15.2.15 Regexp
6
7
8
9
Instances of the class Regexp represent regular expressions, and have the following properties. pattern: A pattern of the regular expression (see §15.2.15.3). The default value of this property is empty. If the value of this property is empty when a method is invoked on an instance of the class Regexp, except for the invocation of the method initialize, the behavior of the invoked method is implementation dependent. ignorecase: A boolean value which denotes whether a match is performed in the case insensitive manner. The default value of this property is false. multiline: A boolean value which denotes whether the pattern “.” terminator (see §15.2.15.3). The default value of this property is false. 15.2.15.1 Direct superclass matches a line-
10 11
12 13 14
15 16
17 18
19
20
The class Object 15.2.15.2 Constants
21
22
The following constants are defined in the class Regexp. IGNORECASE: An instance of the class Integer whose value is 2n , where the integer n is an implementation defined value. The value of this constant shall be different from that of MULTILINE described below. MULTILINE: An instance of the class Integer whose value is 2m , where the integer m is an implementation defined value. The above constants are used to set the ignorecase and multiline properties of an instance of the class Regexp (see §15.2.15.6.1). 196
23 24 25
26 27
28 29
1
15.2.15.3 Syntax
Patterns
2
3 4 5
pattern :: alternative 1 | pattern 1 | alternative 2 alternative :: [ empty ] | alternative 3 term term :: anchor | atom 1 | atom 2 quantifier anchor :: ˆ| � quantifier :: * | + | ? atom :: pattern-character | grouping | . | atom-escape-sequence pattern-character :: source-character but not regexp-meta-character regexp-meta-character :: | | . | * | + | ˆ| ? | ( | ) | future-reserved-meta-character future-reserved-meta-character :: [ | ] | { | } grouping :: ( pattern ) atom-escape-sequence :: decimal-escape-sequence | regexp-character-escape-sequence 197
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
1 2
decimal-escape-sequence :: \ decimal-digit-without-zero regexp-character-escape-sequence :: regexp-escape-sequence | regexp-non-escaped-sequence | hex-escape-sequence | regexp-octal-escape-sequence | regexp-control-escape-sequence regexp-escape-sequence :: \ regexp-escaped-character regexp-escaped-character :: n | t | r | f | v | a | e | b regexp-non-escaped-sequence :: \ regexp-non-escaped-character regexp-non-escaped-character :: source-character but not regexp-escaped-character regexp-octal-escape-sequence :: octal-escape-sequence but not decimal-escape-sequence regexp-control-escape-sequence :: \ ( C - | c ) regexp-control-escaped-character regexp-control-escaped-character :: regexp-character-escape-sequence | ? | source-character but not ( \ | ? )
3 4 5 6 7 8
9 10
11 12
13 14
15 16
17 18
19 20
21 22 23 24
25 26
future-reserved-meta-character s are reserved for the extension of the pattern of regular expressions. Semantics A pattern matches the following string: a) b) If the pattern is an alternative1 , it matches the string which the alternative1 matches. If the pattern is a pattern1 | alternative2 , it matches the string which either the pattern1 or the alternative 2 matches.
27
28
29
30 31
32
An alternative matches the following string: 198
1
a) b)
If the alternative is [empty], it matches an empty string. If the alternative is an alternative3 term, it matches the concatenation of two strings, the one which the alternative3 matches and the other which the term matches, in this order.
2 3
4
A term matches the following string: a) b) If the term is an atom1 , it matches the string which the atom1 matches. If the term is an atom2 quantifier, it matches a string as follows: 1) 2) 3) c) If the quantifier is *, it matches zero or more strings which the atom2 matches. If the quantifier is +, it matches one or more strings which the atom2 matches. If the quantifier is ?, it matches at most one string which the atom2 matches.
5
6
7
8
9
10 11
If the term is an anchor, it matches the position within the string S which the pattern is matched against, as follows: 1) If the anchor is ˆ, it matches the beginning of S or the position just after a lineterminator which is followed by at least one character. If the anchor is �, it matches the end of S or the position just before a line-terminator.
12 13
14
2)
15
An atom matches the following string: a) If the atom is a pattern-character, it matches a single character C represented by the pattern-character. If the atom occurs in the pattern of an instance of the class Regexp whose ignorecase property is true, it also matches a corresponding uppercase character of C, if C is a lowercase character, or a corresponding lowercase character of C, if C is an uppercase character. If the atom is a grouping, it matches the string which the grouping matches. If the atom is “.”, it matches any character except for a line-terminator. If the atom occurs in the pattern of an instance of the class Regexp whose multiline property is true, it also matches a line-terminator. If the atom is an atom-escape-sequence, it matches the string which the atom-escapesequence matches.
16 17 18 19 20
21
b) c)
22 23 24
25 26
d)
27
A grouping matches the string which the pattern matches. An atom-escape-sequence matches the following string: a) If the atom-escape-sequence is a decimal-escape-sequence, it matches the string which the decimal-escape-sequence matches. If the atom-escape-sequence is a regexp-character-escape-sequence, it matches a string of length one, the content of which is the character which the regexp-character-escape-sequence represents. 199
28
29 30
31 32 33
b)
1
A decimal-escape-sequence matches the following string: a) b) Let i be an integer represented by decimal-digit-without-zero. Let G be the i th grouping in the pattern, counted from 1, in the order of the occurrence of “(” of groupings from the left of the pattern. If the decimal-escape-sequence occurs before G within the pattern, it does not match any string. If G matches any string, the decimal-escape-sequence matches the same string. Otherwise, the decimal-escape-sequence does not match any string.
2
3 4
5 6
c)
7
d) e)
8
9
A regexp-character-escape-sequence represents a character as follows:
10
A regexp-escape-sequence represents a character as shown in Table 1 in §8.5.5.2.2. A regexp-non-escaped-sequence represents a regexp-non-escaped–character. A hex-escape-sequence represents a character as described in §8.5.5.2.2. A regexp-octal-escape-sequence is interpreted in the same way as an octal-escape-sequence (see §8.5.5.2.2). A regexp-control-escape-sequence represents a character, the code of which is computed by taking bitwise AND of 0x9f and the code of the character represented by the regexp-controlescaped-character, except when the regexp-control-escaped-character is ?, in which case, the regexp-control-escape-sequence represents a character whose code is 127. Matching process
11
12
13 14
15 16 17 18
19
15.2.15.4
20 21
A pattern P is considered to successfully match the given string S, if there exists a substring of S (including S itself) which P matches, satisfying the following conditions. a) If P is of the form pattern1 | alternative2 , and if both the pattern1 and the alternative2 matches some substrings, the matched substring is the one among the substrings matched by the pattern1 which meets conditions b and c as follows. If P matches more than one substrings, the substring which begins earliest in S is the matched substring. This condition takes precedence over the condition c. If there are more than one substrings which satisfy the condition b, the longest one is the matched substring.
22 23 24
25 26
b)
27 28
c)
29 30 31
These conditions are applied to any substring matched by any sub-pattern of P as much as possible in the way that the resulting substring matched by P still satisfies the conditions a, b and c. When a numerical offset is specified, P is matched against the part of S which begins at the offset. Note, however, that if the match succeeds, the string property of the resulting instance of the class MatchData is S, not the part of S which begins at the offset, as described below. 200
32 33 34
1 2
A matching process returns either an instance of the class MatchData (see §15.2.16) if the match succeeds or nil if the match failed. An instance of the class MatchData is created as follows: a) b) c) d) e) Let B be the substring of S which P matched. Create a direct instance of the class MatchData, and let M be the instance. Set the string of M to S. Create a new empty list L. Let O be an ordered pair whose first element is B and whose second element is the offset of the first character of B within S, counted from 0. Append O to L. For each grouping G in P, in the order of the occurrence of its “(” within P, take the following steps: 1) If G contributed to the match of P, let B be the substring which G matched. Let O be an ordered pair whose first element is B and whose second element is the offset of the first character of B within S, counted from 0. Append O to L. Otherwise, append to L an ordered pair whose elements are both nil.
3
4
5
6
7
8 9
10 11
f)
12 13 14
15
2) g) h)
16
Set the match result of M to L. M is the instance of the class MatchData returned by the matching process.
17
18 19
A matching process creates or updates a local variable binding with name “˜”, which is specifically used by the method Regexp.last match (see §15.2.15.5.3), as follows: a) b) Let M be the value which the matching process returns. If the binding for the name “˜” can be resolved by the process described in §9.1.1 as if “˜” were a local-variable-identifier, replace the value of the binding with M. Otherwise, create a local variable binding with name “˜” and value M in the uppermost nonblock element of [local-variable-bindings] where the non-block element means the element which does not correspond to a block.
20
21 22
23 24 25
c)
26 27
A conforming processor may name the binding other than “˜”; however, it shall not be of the form local-variable-identifier. 15.2.15.5 15.2.15.5.1 Singleton methods Regexp.compile
28
29
30
Regexp.compile( *args )
201
1
Visibility: public Behavior: Same as the method new (see §15.2.3.2.3). 15.2.15.5.2 Regexp.escape
2
3
4
Regexp.escape( string ) Visibility: public Behavior: a) If the string is not an instance of the class String, the behavior is implementation dependent. Let S be the content of the string. Return a new instance of the class String whose content is same as S, except that every occurrences of characters on the left of Table 3 are replaced with the corresponding sequences of characters on the right of the Table 3. Regexp.last match
5
6
7 8
9
b) c)
10 11 12
13
15.2.15.5.3
14
Regexp.last match( *index ) Visibility: public Behavior: a) Search for a binding of a local variable with name “˜” as described in §9.1.1 as if “˜” were a local-variable-identifier. If the binding is found and its value is an instance of the class MatchData, let M be the instance. Otherwise, return nil. If the length of the index is 0, return M. If the length of the index is larger than 1, raise a direct instance of the class ArgumentError. If the length of the index is 1, let A be the only argument. If A is not an instance of the class Integer, the behavior of the method is implementation dependent. Let R be the result returned as if by invoking the method [] on M with A as the only argument (see §15.2.16.2.1). Return R.
15
16
17 18
19 20
b)
21
c) d) e) f)
22
23
24 25
26 27
g)
28
h) 202
Table 3 – Regexp escaped characters Characters replaced Escaped sequence 0x0a 0x09 0x0d 0x0c 0x20 # \n \t \r \f \0x20 \# \� \( \) \* \+ \\. \? \[ \\ \] \ˆ \{ \| \}
� ( ) * + . ? [ \ ] ˆ { | } 15.2.15.5.4 Regexp.quote
1
2
Regexp.quote Visibility: public Behavior: Same as the method escape (see §15.2.15.5.2). 15.2.15.6 15.2.15.6.1 Instance methods Regexp#initialize
3
4
5
6
7
initialize( source, flag ) Visibility: private Behavior: 203
8
9
1 2 3
a)
If the source is an instance of the class Regexp, let S be the pattern of the source. If the source is an instance of the class String, let S be the content of the source. Otherwise, the behavior is implementation dependent. If S cannot be derived from the pattern (§15.2.15.3), raise a direct instance of the class RegexpError. Set the pattern of the receiver to S. If the flag is an instance of the class Integer, let n be its value. 1) If computing bitwise AND of the value of Regexp::IGNORECASE and n results in non-zero value, set the ignorecase property of the receiver to true. If computing bitwise AND of the value of Regexp::MULTILINE and n results in non-zero value, set the multiline property of the receiver to true.
4 5
b)
6
c) d)
7
8 9
10 11
2)
12 13
e)
If the flag is true value other than an instance of the class Integer, set the ignorecase property of the receiver to true. Return an implementation defined value. Regexp#initialize copy
14
f)
15
15.2.15.6.2
16
initialize copy( original ) Visibility: private Behavior: a) If the original is not an instance of the class of the receiver, raise a direct instance of the class TypeError. Set the pattern of the receiver to the patten of the original . Set the ignorecase property of the receiver to the ignorecase property of the orginal . Set the multiline property of the receiver to the multiline property of the orginal . Return an implementation defined value. Regexp#==
17
18
19 20
21
b) c) d) e)
22
23
24
25
15.2.15.6.3
26
= =( other ) Visibility: public Behavior: 204
27
28
1
a) b) c)
If the other is not an instance of the class Regexp, return false. If the corresponding properties of the receiver and the other are the same, return true. Otherwise, return false. Regexp#===
2
3
4
15.2.15.6.4
5
= = =( string ) Visibility: public Behavior: a) If the string is not an instance of the class String, the behavior is implementation dependent. Let S be the content of the string. Match the pattern of the receiver against S (see §15.2.15.3 and Step 15.2.15.4). Let M be the result of the matching process. If M is an instance of the class MatchData, return true. Otherwise, return false. Regexp#=˜
6
7
8 9
10
b) c)
11 12
13
d) e)
14
15
15.2.15.6.5
16
=~( string ) Visibility: public Behavior: a) If the string is not an instance of the class String, the behavior is implementation dependent. Let S be the content of the string. Match the pattern of the receiver against S (see §15.2.15.3 and Step 15.2.15.4). Let M be the result of the matching process. If M is nil return nil. If M is an instance of the class MatchData, let P be first element of the match result property of M, and let i be the second element of P. Return an instance of the class Integer whose value is i. 205
17
18
19 20
21
b) c)
22 23
24
d) e)
25 26
27
f)
1
15.2.15.6.6
Regexp#casefold?
2
casefold? Visibility: public Behavior: The method returns the value of the ignorecase property of the receiver. 15.2.15.6.7 Regexp#match
3
4
5
6
match( string ) Visibility: public Behavior: a) If the string is not an instance of the class String, the behavior is implementation dependent. Let S be the content of the string. Match the pattern of the receiver against S (see §15.2.15.3 and Step 15.2.15.4). Let M be the result of the matching process. Return M. Regexp#source
7
8
9 10
11
b) c)
12 13
14
d)
15
15.2.15.6.8
16
source Visibility: public Behavior: The method returns a direct instance of the class String whose content is the pattern of the receiver. 15.2.16 MatchData
17
18 19
20
21 22
Instances of the class MatchData represent results of successful matches of instances of the class Regexp against instances of the class String. An instance of the class MatchData has the properties called string and match result, which are initialized as described in §15.2.15.4. Elements of the match result are indexed by integers starting from 0. Given an instance of the class MatchData M, the matched substring, pre-match and postmatch of M are defined as follows:
23 24 25
26 27
206
1 2 3
Let S be the string of M. Let F be the first element of the match result of M. Let B and O be the first portion (the substring matched) and the second portion (offset of that substring) of F. Let i be the sum of O and the length of B. matched substring: The matched substring of M is B. pre-match: The pre-match of M is a part of S, from the first up to, but not including the Oth character of S. post-match: The post-match of M is a part of S, from the i th up to the last character of S. 15.2.16.1 Direct superclass
4
5 6
7 8
9
10
The class Object 15.2.16.2 15.2.16.2.1 Instance methods MatchData#[]
11
12
13
[]( *args ) Visibility: public Behavior: The method behaves as if the method to a were invoked on the receiver (see §15.2.16.2.12), and then, the method [] were invoked on the resulting instance of the class Array with the same arguments passed to an invocation of this method (see §15.2.12.4.4). 15.2.16.2.2 MatchData#begin
14
15 16 17
18
19
begin( index ) Visibility: public Behavior: a) If the index is not an instance of the class Integer, the behavior is implementation dependent. Let L be the match result of the receiver, and let i be the value of the index . If i is smaller than 0 or equal to, or larger than the number of elements of L, raise a direct instance of the class IndexError. Otherwise, return the second portion of the i th element of L. MatchData#captures
20
21
22 23
24
b) c)
25 26
27
d)
28
15.2.16.2.3
207
1
captures Visibility: public Behavior: a) b) c) Let L be the match result of the receiver. Create an empty direct instance of the class Array A. Except for the first element, for each element e of L, in the same order in the list, append to A a direct instance of the class String whose content is the first portion of e. Return A. MatchData#end
2
3
4
5
6 7 8
9
d)
10
15.2.16.2.4
11
end( index ) Visibility: public Behavior: a) If the index is not an instance of the class Integer, the behavior is implementation dependent. Let L be the match result of the receiver, and let i be the value of the index . If i is smaller than 0 or equal to, or larger than the number of elements of L, raise a direct instance of the class IndexError. Let F and S be the first and the second portions of the i th element of L. If F is nil, return nil. Otherwise, let f be the length of F. Return an instance of the class Integer whose value is the sum of S and f. MatchData#initialize copy
12
13
14 15
16
b) c)
17 18
19
d) e) f)
20
21 22
23
15.2.16.2.5
24
initialize copy( original ) Visibility: private Behavior: 208
25
26
1 2
a)
If the original is not an instance of the class of the receiver, raise a direct instance of the class TypeError. Set the string property of the receiver to the string property of the original . Set the match result property of the receiver to the match result property of the original . Return an implementation defined value. MatchData#length
3
b) c)
4 5
6
d)
7
15.2.16.2.6
8
length Visibility: public Behavior: The method returns the number of elements of the match result of the receiver. 15.2.16.2.7 MatchData#offset
9
10
11
12
offset( index ) Visibility: public Behavior: a) If the index is not an instance of the class Integer, the behavior is implementation dependent. Let L be the match result of the receiver, and let i be the value of the index . If i is smaller than 0 or equal to, or larger than the number of elements of L, raise a direct instance of the class IndexError. Let S and b be the first and the second portions of the i th element of L. Let e be the sum of b and the length of S. Return a new instance of the class Array which contains two instances of the class Integer, the one whose value is b and the other whose value is e, in this order. MatchData#post match
13
14
15 16
17
b) c)
18 19
20 21
d)
22 23
e)
24
15.2.16.2.8
25
post match Visibility: public Behavior: The method returns an instance of the class String the content of which is the post-match of the receiver. 209
26
27 28
1
15.2.16.2.9
MatchData#pre match
2
pre match Visibility: public Behavior: The method returns an instance of the class String the content of which is the pre-match of the receiver. 15.2.16.2.10 MatchData#size
3
4 5
6
7
size Visibility: public Behavior: Same as the method length (see §15.2.16.2.6). 15.2.16.2.11 MatchData#string
8
9
10
11
string Visibility: public Behavior: The method returns an instance of the class String the content of which is the string of the receiver. 15.2.16.2.12 MatchData#to a
12
13 14
15
16
to a Visibility: public Behavior: a) b) c) Let L be the match result of the receiver. Create an empty direct instance of the class Array A. For each element e of L, in the same order in the list, append to A an instance of the class String whose content is the first portion of e. Return A. MatchData#to s
17
18
19
20
21 22
23
d)
24
15.2.16.2.13
210
1
to s Visibility: public Behavior: The method returns an instance of the class String the content of which is the matched substring of the receiver. 15.2.17 Proc
2
3 4
5
6
Instances of the class Proc represent block s. An instance of the class Proc has the following property. block: The block represented by the instance. 15.2.17.1 Direct superclass
7
8
9
10
The class Object 15.2.17.2 15.2.17.2.1 Singleton methods Proc.new
11
12
13
Proc.new( &block ) Visibility: public Behavior: a) b) If the block is given, let B be the block . Otherwise: 1) 2) c) d) If the top of [block] is block-not-given, raise a direct instance of the class ArgumentError. Otherwise, let B be the top of [block] .
14
15
16
17
18
19
20
Create a new instance of the class Proc which has B as its block. Return the instance. Instance methods Proc#[]
21
22
15.2.17.3 15.2.17.3.1
23
24
[]( *args )
211
1
Visibility: public Behavior: Same as the method call (see §15.2.17.3.3). 15.2.17.3.2 Proc#arity
2
3
4
arity Visibility: public Behavior: Let B be the block represented by the receiver. a) If a block-formal-argument does not occur in B, return an instance of the class Integer whose value is implementation defined. If a block-formal-argument occurs in B : 1) If a block-formal-argument-list does not occur in the block-formal-argument, return an instance of the class Integer whose value is 0. If a block-formal-argument-list occurs in the block-formal-argument: i) If the block-formal-argument-list is of the form left-hand-side, return an instance of the class Integer whose value is 1. If the block-formal-argument-list is of the form multiple-left-hand-side: I) If the multiple-left-hand-side is of the form grouped-left-hand-side, return an instance of the class Integer whose value is implementation defined.
5
6
7 8
9
b)
10 11
12
2)
13 14
15
ii)
16 17
18 19
II) If the multiple-left-hand-side is of the form packing-left-hand-side, return -1. III) Otherwise, let n be the number of multiple-left-hand-side-items of the multiple-left-hand-side. IV) If the multiple-left-hand-side ends with a packing-left-hand-side, return an instance of the class Integer whose value is -(n+1). V) Otherwise, return an instance of the class Integer whose value is n. 15.2.17.3.3 Proc#call
20 21
22 23
24
25
26
call( *args ) Visibility: public Behavior: Let B be the block of the receiver. Let L be an empty list. 212
27
28
1
a) b) c)
Append each element of the args, in the indexing order, to L. Call B with L as the arguments (see §11.2.2). Let V be the result of the call. Return V. Proc#clone
2
3
4
15.2.17.3.4
5
clone Visibility: public Behavior: a) Create a direct instance of the class of the receiver which has no bindings of instance variables. Let O be the newly created instance. For each binding B of the instance variables of the receiver, create a variable binding with the same name and value as B in the set of bindings of instance variables of O. If the receiver is associated with an eigenclass, let Eo be the eigenclass, and take the following steps: 1) Create an eigenclass whose direct superclass is the direct superclass of Eo . Let En be the eigenclass. For each binding Bv1 of the constants of Eo , create a variable binding with the same name and value as Bv1 in the set of bindings of constants of En . For each binding Bv2 of the class variables of Eo , create a variable binding with the same name and value as Bv2 in the set of bindings of class variables of En . For each binding Bm of the instance methods of Eo , create a method binding with the same name and value as Bm in the set of bindings of instance methods of En . Associate O with En .
6
7
8 9
10 11
b)
12 13
c)
14 15
16 17
2)
18 19
3)
20 21
4)
22
5) d) e)
23
Set the block of O to the block of the receiver. Return O. Proc#dup
24
25
15.2.17.3.5
26
dup Visibility: public Behavior: 213
27
28
1 2
a)
Create a direct instance of the class of the receiver which has no bindings of instance variables. Let O be the newly created instance. Set the block of O to the block of the receiver. Return O. Struct
3
b) c) 15.2.18
4
5
6 7 8 9
The class Struct is a generator of a structure type which is a class defining a set of fields and methods for accessing these fields. Fields are indexed by integers starting from 0 (see §15.2.18.2.1). An instance of a generated class has values for the set of fields. Those values can be referenced and updated with accessor methods for their fields. 15.2.18.1 Direct superclass
10
11
The class Object 15.2.18.2 15.2.18.2.1 Singleton methods Struct.new
12
13
14
Struct.new( string, *symbol list ) Visibility: public Behavior: The method creates a class defining a set of fields and accessor methods for these fields. When the method is invoked, take the following steps: a) Create a direct instance of the class Class which has the class Struct as its direct superclass. Let C be that class. If the string is not an instance of the class String or the class Symbol, the behavior is implementation dependent. If the string is an instance of the class String, let N be the content of the instance. 1) If N is not of the form constant-identifier, raise a direct instance of the class ArgumentError error. Otherwise, i) If the binding with name N exists in the set of bindings of constants in the class Struct, replace the value of the binding with C. Otherwise, create a constant binding in the class Struct with name N and value C.
15
16 17
18
19 20
21 22
b)
23
c)
24 25
26
2)
27 28
29 30
ii)
31
d) 214
If the string is an instance of the class Symbol, prepend the instance to the symbol list.
1
e) f)
Let i be 0. For each element S of the symbol list, take the following steps: 1) 2) 3) Let N be the name designated by S. Define a field, which is named N and is indexed by i, in C. If N is of the form local-variable-identifier or constant-identifier : i) Define a method named N in C which takes no arguments, and when invoked, returns the value of the field named N. Define a method named N= (i.e. N postfixed by “=”) in C which takes one argument, and when invoked, sets the value of the field named N to the given argument and returns the argument.
2
3
4
5
6 7
8 9 10
ii)
11
4) g)
Increment i by 1.
12
Return C.
13
Singleton methods of classes created by the Struct.new Classes created by the method Struct.new are equipped with public singleton methods new, [], and members. The following describes those methods, assuming that the name of a class created by the method Struct.new is C.
14 15 16
17
C .new( *args ) Visibility: public Behavior: a) Create a direct instance of the class with the set of fields the receiver defines. Let I be the instance. Invoke the method initialize on I with the args as the list of arguments. Return I.
18
19
20 21
22
b) c)
23
24
C .[]( *args ) Visibility: public Behavior: Same as the method new described above.
25
26
215
1
C .members Visibility: public Behavior: a) Create a direct instance of the class Array which contains instances of the class String, each of which represents a field name of the receiver. Let A be the instance of the class Array. The elements in A are arranged in the indexing order of corresponding fields. b) Return A. Instance methods Struct#==
2
3
4 5 6
7
8
9
15.2.18.3 15.2.18.3.1
10
11
= =( other ) Visibility: public Behavior: a) b) c) If the other and the receiver is the same object, return true. If the class of the other and that of the receiver are different, return false. Otherwise, for each field named f of the receiver, take the following steps: 1) Let R and O be the values of the fields named f of the receiver and the other respectively. If R and O are not the same object, i) Invoke the method == on R with O as the only argument. Let V be the resulting value of the invocation. If V is a false value, return false.
12
13
14
15
16
17 18
19
2)
20 21
22
ii) d)
23
Return true. Struct#[]
24
15.2.18.3.2
25
[]( name ) Visibility: public 216
26
1
Behavior: a) If the name is an instance of the class Symbol or the class String: 1) 2) 3) Let N be the name designated by the name. If the receiver has the field named N, return the value of the field. Otherwise, let S be an instance of the class Symbol with name N and raise a direct instance of the class NameError which has S as its name property.
2
3
4
5 6
7 8
b)
If the name is an instance of the class Integer, let i be the value of the name. Let n be the number of the fields of the receiver. 1) 2) If i is negative, replace i with n + i. If i is still negative or i equal or larger than n, raise a direct instance of the class IndexError. Otherwise, return the value of the field whose index is i.
9
10 11
12
3) c)
13
Otherwise, the behavior of the method is implementation dependent. Struct#[]=
14
15.2.18.3.3
15
[] =( name, obj ) Visibility: public Behavior: a) If the name is an instance of the class Symbol or an instance of the class String: 1) 2) Let N be the name designated by the name. If the receiver has the field named N, i) ii) 3) Replace the value of the field with the obj , Return the obj .
16
17
18
19
20
21
22
23 24
Otherwise, let S be an instance of the class Symbol with name N and raise a direct instance of the class NameError which has S as its name property.
25 26
b)
If the string is an instance of the class Integer, let i be the value of the name. Let n be the number of the fields of the receiver. 1) If i is negative, replace i with n + i.
27
217
1 2
2)
If i is still negative or i equal or larger than n, raise a direct instance of the class IndexError. Otherwise, i) ii) Replace the value of the field whose index is i with the obj Return the obj .
3
3)
4
5
6
c)
Otherwise, the behavior of the method is implementation dependent. Struct#each
7
15.2.18.3.4
8
each( &block ) Visibility: public Behavior: a) b) If the block is not given, the behavior is implementation dependent. For each field of the receiver, in the indexing order, call the block with the value of the field as the only argument. Return the receiver. Struct#each pair
9
10
11
12 13
14
c)
15
15.2.18.3.5
16
each pair( &block ) Visibility: public Behavior: a) b) If the block is not given, the behavior is implementation dependent. For each field of the receiver, in the indexing order, take the following steps: 1) Let N and V be the name and the value of the field respectively. Let S be an instance of the class Symbol with name N. Call the block with the list of arguments which contains S and V in this order.
17
18
19
20
21 22
23
2) c)
24
Return the receiver. Struct#members
25
15.2.18.3.6
218
1
members Visibility: public Behavior: Same as the method members described in §15.2.18.2.1. 15.2.18.3.7 Struct#select
2
3
4
5
select( &block ) Visibility: public Behavior: a) b) c) If the block is not given, the behavior is implementation dependent. Create an empty instance the class Array. Let A be the instance. For each field of the receiver, in the indexing order, take the following steps: 1) 2) Let V be the value of the field. Call the block with V as the only argument. Let R be the resulting value of the call. If R is a true value, append V to A.
6
7
8
9
10
11
12 13
14
3) d)
15
Return A. Struct#initialize
16
15.2.18.3.8
17
initialize( *args ) Visibility: private Behavior: Let Na be the length of the args, and let Nf be the number of the fields of the receiver. a) b) If Na is larger than Nf , raise a direct instance of the class ArgumentError. Otherwise, for each field f of the receiver, let i be the index of f, and set the value of f to the i th element of the args, or to nil when i is equal to or larger than Na . Return an implementation defined value. Struct#initialize copy 219
18
19 20
21
22 23
24
c)
25
15.2.18.3.9
1
initialize copy( original ) Visibility: private Behavior: a) If the receiver and the original are the same object, return an implementation defined value. If the original is not an instance of the class of the receiver, raise a direct instance of the class TypeError. If the number of the fields of the receiver and the number of the fields of the original are different, raise a direct instance of the class TypeError. For each field f of the original , let i be the index of f, and set the value of the i th field of the receiver to the value of f. Return an implementation defined value. Time
2
3
4 5
6 7
b)
8 9
c)
10 11
d)
12
e) 15.2.19
13
14
Instances of the class Time represent dates and times. An instance of the class Time holds the following data. Microseconds: Microseconds since January 1, 1970 00:00 UTC. Microseconds is an integer whose range is implementation defined. If an out of range value is given as microseconds when creating an instance of the class Time, a direct instance of either of the class ArgumentError or the class RangeError shall be raised. Time zone: The time zone. 15.2.19.1 Direct superclass
15
16 17 18 19
20
21
22
The class Object 15.2.19.2 Time computation
23
24 25 26
Mathematical functions introduced in this subclause are used throughout the descriptions in §15.2.19. These functions are assumed to compute exact mathematical results using mathematical real numbers. 15.2.19.2.1 Day
27
The number of microseconds of a day is computed as follows: M icroSecP erDay = 24 × 60 × 60 × 106
220
The number of days since January 1, 1970 00:00 UTC which corresponds to microseconds t is computed as follows: ( ) t Day(t) = f loor M icroSecP erDay f loor(t) = The integer x such that x ≤ t < x + 1 The weekday which corresponds to microseconds t is computed as follows: W eekDay(t) = (Day(t) + 4) modulo 7
1
15.2.19.2.2
Year
2 3
A year has 365 days, except for leap years, which have 366 days. Leap years are those which are either:
4
divisible by 4 and not divisible by 100, or divisible by 400.
5
The number of days from January 1, 1970 00:00 UTC to the beginning of a year y is computed as follows: ) ( ) ( ) ( y − 1901 y − 1601 y − 1969 −f loor +f loor DayF romY ear(y) = 365×(y−1970)+f loor 4 100 400 Microseconds elapsed since January 1, 1970 00:00 UTC until the beginning of y is computed as follows: M icroSecF romY ear(y) = DayF romY ear(y) × M icroSecP erDay The year number y which corresponds to microseconds t measured from January 1, 1970 00:00 UTC is computed as follows. Y earF romT ime(t) = y such that, M icroSecF romY ear(y − 1) < t ≤ M icroSecF romY ear(y) The number of days from the beginning of the year for the given microseconds t is computed as follows. DayW ithinY ear(t) = Day(t) − DayF romY ear(Y earF romT ime(t)) 15.2.19.2.3 Month
6
7 8
Months have usual number of days. Leap years have the extra day in February. Each month is identified by the number in the range 1 to 12, in the order from January to December.
221
The month number which corresponds to microseconds t measured from January 1, 1970 00:00 UTC is computed as follows. 1 if 0 ≤ DayW ithinY ear(t) < 31 2 if 31 ≤ DayW ithinY ear(t) < 59 3 if 59 + LeapY ear(t) ≤ DayW ithinY ear(t) < 90 + LeapY ear(t) 4 if 90 + LeapY ear(t) ≤ DayW ithinY ear(t) < 120 + LeapY ear(t) 5 if 120 + LeapY ear(t) ≤ DayW ithinY ear(t) < 151 + LeapY ear(t) 6 if 151 + LeapY ear(t) ≤ DayW ithinY ear(t) < 180 + LeapY ear(t) M onthF romT ime(t) = 7 if 181 + LeapY ear(t) ≤ DayW ithinY ear(t) < 212 + LeapY ear(t) 8 if 212 + LeapY ear(t) ≤ DayW ithinY ear(t) < 243 + LeapY ear(t) 9 if 243 + LeapY ear(t) ≤ DayW ithinY ear(t) < 273 + LeapY ear(t) 10 if 273 + LeapY ear(t) ≤ DayW ithinY ear(t) < 304 + LeapY ear(t) 11 if 304 + LeapY ear(t) ≤ DayW ithinY ear(t) < 334 + LeapY ear(t) 12 if 334 + LeapY ear(t) ≤ DayW ithinY ear(t) < 365 + LeapY ear(t) { 1 LeapY ear(t) = 0 15.2.19.2.4
if Y earF romT ime(t) is a leap year otherwise
1
Days of month from January 1, 1970
The day of the month which corresponds to microseconds t measured 00:00 UTC is computed as follows. DayW ithinY ear(t) + 1 DayW ithinY ear(t) − 30 DayW ithinY ear(t) − 58 − LeapY ear(t) DayW ithinY ear(t) − 89 − LeapY ear(t) DayW ithinY ear(t) − 119 − LeapY ear(t) DayW ithinY ear(t) − 150 − LeapY ear(t) DayW ithinM onth(t) = DayW ithinY ear(t) − 180 − LeapY ear(t) DayW ithinY ear(t) − 211 − LeapY ear(t) DayW ithinY ear(t) − 242 − LeapY ear(t) DayW ithinY ear(t) − 272 − LeapY ear(t) DayW ithinY ear(t) − 303 − LeapY ear(t) DayW ithinY ear(t) − 333 − LeapY ear(t) 15.2.19.2.5 Hours, Minutes, and Seconds
if if if if if if if if if if if if
M onthF romT ime(t) = 1 M onthF romT ime(t) = 2 M onthF romT ime(t) = 3 M onthF romT ime(t) = 4 M onthF romT ime(t) = 5 M onthF romT ime(t) = 6 M onthF romT ime(t) = 7 M onthF romT ime(t) = 8 M onthF romT ime(t) = 9 M onthF romT ime(t) = 10 M onthF romT ime(t) = 11 M onthF romT ime(t) = 12
2
The number of microseconds in an hour, a minute, a second are as follows: M icroSecP erHour = 60 × 60 × 106 M icroSecP erM inute = 60 × 106 M icroSecP erSecond = 106
222
The hour, the minute, and the second which correspond to microseconds t measured from January 1, 1970 00:00 UTC are computed as follows. ( ) t HourF romT ime(t) = f loor modulo 24 M icroSecP erHour ) ( t modulo 60 M inuteF romT ime(t) = f loor M icroSecP erM inute ( ) t SecondF romT ime(t) = f loor modulo 60 M icroSecP erSecond 15.2.19.3 Time zone and Local time
1
2 3 4
The current time zone is determined from time zone information provided by the underlying system. If the system does not provide information on the current time zone, the time zone of an instance of the class Time is implementation defined. The local time for an instance of the class Time is computed from its microseconds t and time zone z as follows. LocalT ime = t + ZoneOf f set(z) ZoneOf f set(z) = UTC offset of z measured in microseconds
5
15.2.19.4
Daylight saving time
6 7 8 9
On system where it is possible to determine the daylight saving time for each time zone, a conforming processor should adjust the microseconds of an instance of the class Time if that microseconds falls within the daylight saving time of the time zone of the instance. An algorithm used for the adjustment is implementation defined. 15.2.19.5 15.2.19.5.1 Singleton methods Time.at
10
11
12
Time.at( *args ) Visibility: public Behavior: a) If the length of the args is 0 or larger than 2, raise a direct instance of the class ArgumentError. If the length of the args is 1, let A be the only argument. 1) If A is an instance of the class Time, return a new instance of the class Time which represents the same time and has the same time zone as A. If A is an instance of the class Integer or an instance of the class Float:
13
14
15 16
17
b)
18 19
20
2)
223
1 2
i)
If A is an instance of the class Integer, let NS be the value of A. Let NM be 0. If A is an instance of the class Float, let F be the value of A. Let NS be the largest integer such that NS ≤ F . Let NM be an integer which is the result of computing (F − NS ) × 106 , rounded off the first decimal place.
3 4 5
ii)
6 7 8
iii) Create a direct instance of the class Time which represents the time at NS × 106 + NM microseconds since January 1, 1970 00:00 UTC, with the current local time zone. iv) Return the resulting instance. 3) c) Otherwise, the behavior is implementation dependent.
9
10
11
If the length of the args is 2, let S and M be the first and second element of the args. 1) i) ii) If S is an instance of the class Integer, let NS be the value of A. If S is an instance of the class Float, let F be the value of A. If F is positive, let NM be the largest integer such that N ≤ F . Otherwise, let NM be the smallest integer such that N ≥ F .
12
13 14 15
16
iii) Otherwise, the behavior is implementation dependent. 2) Compute an integer which corresponds to M in the same way as S as described in Step c-1-i and c-1-ii. Let NM be the integer. Create a direct instance of the class Time which represents the time at NS × 106 + NM microseconds since January 1, 1970 00:00 UTC, with the current local time zone. Return the resulting instance. Time.gm
17 18
19 20 21
3)
22
4) 15.2.19.5.2
23
24
Time.gm( year, month =1, day =0, hour =0, min =0, sec =0, usec =0 ) Visibility: public Behavior: a) Compute an integer value for the year , day, hour , min, sec, and usec as described below. Let Y, D, H, Min, S, and U be integers thus converted. An integer I is determined from the given object O as follows: 1) 2) 224 If O is an instance of the class Integer, let I be the value of O. If O is an instance of the class Float, let I be the integral part of the value of O.
25
26
27 28
29
30
31
1
3)
If O is an instance of the class String: i) If the content of O is a sequence of decimal-digits, let I be the value of those sequence of digits computed using base 10. Otherwise, the behavior is implementation dependent.
2 3
4
ii) 4) b)
5
Otherwise, the behavior is implementation dependent.
6
Compute an integer value from the month as follows: 1) If the month is not an instance of the class String, the behavior is implementation dependent. If the content of the month is the same as one of the names of the months in the upper row on Table 4, ignoring the differences in case, let Mon be the integer which corresponds to the month in the lower row on the same table. If the first character of the month is decimal-digit, compute an integer value from the month as in Step a. Let Mon be the resulting integer. Otherwise, raise a direct instance of the class ArgumentError.
7 8
9 10 11
2)
12 13
3)
14
4) c) d) e)
15
If Y is an integer such that 0 ≤ Y ≤ 38, replace Y with 2000 + Y . If Y is an integer such that 69 ≤ Y ≤ 138, replace Y with 1900 + Y . If each integer computed above is outside the range as listed below, raise a direct instance of the class ArgumentError.
16
17 18
19
1 ≤ M on ≤ 12 1 ≤ D ≤ 31 0 ≤ H ≤ 23 0 ≤ M in ≤ 59 0 ≤ S ≤ 60
20
21
22
23
24
Whether any conditions are placed on Y is implementation defined. f) Let t be an integer which satisfies all of the following equations.
25
26
Y earF romT ime(t) = Y M onthF romT ime(t) = M on DayW ithinM onth(t) = 1
27
28
225
g)
Compute microseconds T as follows. T = t + D × M icroSecP erDay + H × M icroSecP erHour+ M in × M icroSecP erM inute + S × 106 + U
1 2
h)
Create a direct instance of the class Time which represents the time at T since January 1, 1970 00:00 UTC, with the UTC time zone. Return the resulting instance. Table 4 – The names of months and corresponding integer 1 2 3 4 5 6 7 8 9 10 11 12
3
i)
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec 4
15.2.19.5.3
Time.local
5
Time.local( year, month =1, day =0, hour =0, min =0, sec =0, usec =0 ) Visibility: public Behavior: Same as the method Time.gm (see §15.2.19.5.2), except that the method returns a direct instance of the class Time which has the current local time zone as its time zone. 15.2.19.5.4 Time.mktime
6
7 8
9
10
Time.mktime( year, month =1, day =0, hour =0, min =0, sec =0, usec =0 ) Visibility: public Behavior: Same as the method Time.local (see §15.2.19.5.3). 15.2.19.5.5 Time.now
11
12
13
14
Time.now Visibility: public Behavior: This method returns a direct instance of the class Time which represents the current time with the current time zone. The behavior of this method is the same as the method new (see §15.2.3.2.3). 15.2.19.5.6 Time.utc
15
16 17
18
19
226
1
Time.utc( year, month =1, day =0, hour =0, min =0, sec =0, usec =0 ) Visibility: public Behavior: Same as the method Time.gm (see §15.2.19.5.2). 15.2.19.6 15.2.19.6.1 Instance methods Time#+
2
3
4
5
6
+( offset ) Visibility: public Behavior: a) If the offset is not an instance of the class Integer or the class Float, the behavior is implementation dependent. Let V be the value of the offset. Let o be an integer which is the result of computing V × 106 , rounded of the first decimal place, if any. Let t and z be the microseconds and time zone of the receiver. Create a direct instance of the class Time which represents the time at (t + o) microseconds since January 1, 1970 00:00 UTC, with z as its time zone. Return the resulting instance. Time#−
7
8
9 10
11
b) c)
12 13
14
d) e)
15 16
17
f)
18
15.2.19.6.2
19
-( offset ) Visibility: public Behavior: a) If the offset is not an instance of the class Integer or the class Float, the behavior is implementation dependent. Let V be the value of the offset. Let o be an integer which is the result of computing V × 106 , rounded of the first decimal place, if any. 227
20
21
22 23
24
b) c)
25 26
1
d) e)
Let t and z be the microseconds and time zone of the receiver. Create a direct instance of the class Time which represents the time at t−o microseconds since January 1, 1970 00:00 UTC, with z as its time zone. Return the resulting instance. Time#<=>
2 3
4
f)
5
15.2.19.6.3
6
< =>( other ) Visibility: public Behavior: a) b) If the other is not an instance of the class Time, return nil. Otherwise, let Tr and To be microseconds of the receiver and the other , respectively. 1) 2) 3) 15.2.19.6.4 If Tr > To , return an instance of the class Integer whose value is 1. If Tr = To , return an instance of the class Integer whose value is 0. If Tr < To , return an instance of the class Integer whose value is -1. Time#asctime
7
8
9
10
11
12
13
14
15
asctime Visibility: public Behavior: a) b) Compute the local time from the receiver (see §15.2.19.3). Let t be the result. Let W be the name of the day of the week in the second row on Table 5 which corresponds to W eekDay(t) in the upper row on the same table. Let Mon be the name of the month in the second row on Table 4 which corresponds to M onthF romT ime(t) in the upper row on the same table. Let D, Min, S, and Y be as follows: D = DayW ithinM onth(t) H = HourF romT ime(t) M = M inuteF romT ime(t) S = SecondF romT ime(t) Y = Y earF romT ime(t) 228
16
17
18
19 20
21 22
c)
d)
e)
Create a direct instance of the class String, the content of which is the following sequence of characters: W M on D H:M :S Y <line-terminator >
1
f)
Return the resulting instance. Table 5 – The names of the days of the week corresponding integer 0 1 2 3 4 5 6
Sun Mon Tue Wed Thu Fly Sat 2
15.2.19.6.5
Time#ctime
3
ctime Visibility: public Behavior: Same as the method asctime (see §15.2.19.6.4). 15.2.19.6.6 Time#day
4
5
6
7
day Visibility: public Behavior: a) b) c) Compute the local time from the receiver (see §15.2.19.3). Let t be the result. Compute DayW ithinM onth(t). Return an instance of the class Integer whose value is the result of Step b. Time#dst?
8
9
10
11
12
13
15.2.19.6.7
14
dst? Visibility: public Behavior: Let T and Z be the microseconds and time zone of the receiver. a) b) If T falls within the daylight saving time of Z, return true. Otherwise, return false. 229
15
16
17
18
1
15.2.19.6.8
Time#getgm
2
getgm Visibility: public Behavior: Same as the method getutc (see §15.2.19.6.10). 15.2.19.6.9 Time#getlocal
3
4
5
6
getlocal Visibility: public Behavior: The method returns a new instance of the class Time which has the same microseconds as the receiver, but has current local time zone as its time zone. 15.2.19.6.10 Time#getutc
7
8 9
10
11
getutc Visibility: public Behavior: The method returns a new instance of the class Time which has the same microseconds as the receiver, but has UTC as its time zone. 15.2.19.6.11 Time#gmt?
12
13 14
15
16
gmt? Visibility: public Behavior: Same as the method utc? (see §15.2.19.6.26). 15.2.19.6.12 Time#gmt offset
17
18
19
20
gmt offset Visibility: public Behavior: Same as the method utc offset (see §15.2.19.6.27). 15.2.19.6.13 230 Time#gmtime
21
22
23
1
gmtime Visibility: public Behavior: Same as the method utc (see §15.2.19.6.25). 15.2.19.6.14 Time#gmtoff
2
3
4
5
gmtoff Visibility: public Behavior: Same as the method utc offset (see §15.2.19.6.27). 15.2.19.6.15 Time#hour
6
7
8
9
hour Visibility: public Behavior: a) b) c) Compute the local time from the receiver (see §15.2.19.3). Let t be the result. Compute HourF romT ime(t). Return an instance of the class Integer whose value is the result of Step b. Time#localtime
10
11
12
13
14
15
15.2.19.6.16
16
localtime Visibility: public Behavior: a) b) Change the time zone of the receiver to the current local time zone. Return the receiver. Time#mday
17
18
19
20
21
15.2.19.6.17
231
1
mday Visibility: public Behavior: a) b) c) Compute the local time from the receiver (see §15.2.19.3). Let t be the result. Compute DayW ithinM onth(t). Return an instance of the class Integer whose value is the result of Step b. Time#min
2
3
4
5
6
7
15.2.19.6.18
8
min Visibility: public Behavior: a) b) c) Compute the local time from the receiver (see §15.2.19.3). Let t be the result. Compute M inuteF romT ime(t). Return an instance of the class Integer whose value is the result of Step b. Time#mon
9
10
11
12
13
14
15.2.19.6.19
15
mon Visibility: public Behavior: a) b) c) Compute the local time from the receiver (see §15.2.19.3). Let t be the result. Compute M onthF romT ime(t). Return an instance of the class Integer whose value is the result of Step b. Time#month
16
17
18
19
20
21
15.2.19.6.20
22
month Visibility: public Behavior: Same as the method mon (see §15.2.19.6.19). 232
23
24
1
15.2.19.6.21
Time#sec
2
sec Visibility: public Behavior: a) b) c) Compute the local time from the receiver (see §15.2.19.3). Let t be the result. Compute SecondF romT ime(t). Return an instance of the class Integer whose value is the result of Step b. Time#to f
3
4
5
6
7
8
15.2.19.6.22
9
to f Visibility: public Behavior: Let t the microseconds of the receiver. a) b) c) Compute t/106 . Create a direct instance of the class Float whose value is the result of Step a. Return the resulting instance. Time#to i
10
11
12
13
14
15
15.2.19.6.23
16
to i Visibility: public Behavior: Let t the microseconds of the receiver. a) b) Compute f loor(t/106 ). Return an instance of the class Integer whose value is the result of Step a. Time#usec
17
18
19
20
21
15.2.19.6.24
22
usec Visibility: public 233
23
1
Behavior: a) b) c) Compute the local time from the receiver (see §15.2.19.3). Let t be the result. Compute t modulo 106 . Return the resulting instance. Time#utc
2
3
4
5
15.2.19.6.25
6
utc Visibility: public Behavior: a) b) Change the time zone of the receiver to UTC. Return the receiver. Time#utc?
7
8
9
10
11
15.2.19.6.26
12
utc? Visibility: public Behavior: Let Z be the time zone of the receiver. a) b) If Z is UTC, return true. Otherwise, return false. Time#utc offset
13
14
15
16
17
15.2.19.6.27
18
utc offset Visibility: public Behavior: Let Z be the time zone of the receiver. a) b) Compute f loor(ZoneOf f set(Z)/106 ). Return an instance of the class Integer whose value is the result of Step a. Time#wday
19
20
21
22
23
15.2.19.6.28
234
1
wday Visibility: public Behavior: a) b) c) Compute the local time from the receiver (see §15.2.19.3). Let t be the result. Compute W eekDay(t). Return an instance of the class Integer whose value is the result of Step b Time#yday
2
3
4
5
6
7
15.2.19.6.29
8
yday Visibility: public Behavior: a) b) c) Compute the local time from the receiver (see §15.2.19.3). Let t be the result. Compute DayW ithinY ear(t). Return an instance of the class Integer whose value is the result of Step b. Time#year
9
10
11
12
13
14
15.2.19.6.30
15
year Visibility: public Behavior: a) b) c) Compute the local time from the receiver (see §15.2.19.3). Let t be the result. Compute Y earF romT ime(t). Return an instance of the class Integer whose value is the result of Step b. Time#zone
16
17
18
19
20
21
15.2.19.6.31
22
zone Visibility: public 235
23
1
Behavior: Let Z be the time zone of the receiver. a) Create a direct instance of the class String, the content of which represents Z. The exact content of the instance is implementation dependent. Return the resulting instance. Time#initialize
2 3
4
b)
5
15.2.19.6.32
6
initialize Visibility: private Behavior: a) Set the microseconds of the receiver to microseconds elapsed since January 1, 1970 00:00 UTC. Set the time zone of the receiver to the current time zone. Return an implementation defined value. Time#initialize copy
7
8
9 10
11
b) c)
12
13
15.2.19.6.33
14
initialize copy( original ) Visibility: private Behavior: a) If the original is not an instance of the class Time, raise a direct instance of the class TypeError. Set the microseconds of the receiver to the microseconds of the original . Set the time zone of the receiver to the time zone of the original . Return an implementation defined value. IO
15
16
17 18
19
b) c) d) 15.2.20
20
21
22
23
An instance of the class IO represents a stream, which is a source and/or a sink of data. An instance of the class IO has the following properties: readability flag: A boolean value which denotes whether the stream can handle input operations. An instance of the class IO is said to be readable if and only if this flag is true. 236
24
25 26
27
1
Reading from a stream which is not readable raises a direct instance of the class IOError. writability flag: A boolean value which denotes whether the stream can handle output operations. An instance of the class IO is said to be writable if and only if this flag is true. Writing to a stream which is not writable raises a direct instance of the class IOError. openness flag: A boolean value which denotes whether the stream is open. An instance of the class IO is said to be open if and only if this flag is true. An instance of the class IO is said to be closed if and only if this flag is false. A closed stream is neither readable nor writable. Reading from or writing to a stream which is not open raises an instance of the class IOError. buffering flag: A boolean value which denotes whether the data to be written to the stream is buffered. When this flag is true, a conforming processor may delay the output to the receiver until the instance methods flush or close is invoked. A conforming processor may raise an instance of the class SystemCallError during the execution of instance methods of the class IO. In the following description of the methods of the class IO, a byte means an integer from 0 to 255. 15.2.20.1 Direct superclass
2 3
4
5
6
7 8
9 10
11 12
13 14
15 16
17 18
19
20
The class Object 15.2.20.2 Included modules
21
22
The following module is included in the class IO.
23
X
Enumerable Singleton methods IO.open
24
15.2.20.3 15.2.20.3.1
25
26
IO.open( *args, &block ) Visibility: public Behavior: a) Invoke the method new on the receiver with all the elements of the args as the arguments. Let I be the resulting value. 237
27
28
29 30
1
b) c) d)
If the block is not given, return I. Otherwise, call the block with I as the argument. Let V be the resulting value. Invoke the method close on I with no arguments, even when an exception is raised and not handled in Step c. Return V. Instance methods IO#close
2
3 4
5
e)
6
15.2.20.4 15.2.20.4.1
7
8
close Visibility: public Behavior: a) b) If the receiver is closed, raise a direct instance of the class IOError. If the buffering flag of the receiver is true, and the receiver is buffering any output, immediately write all the buffered data to the stream which the receiver represents. Set the openness flag of the receiver to false. Return nil. IO#closed?
9
10
11
12 13
14
c) d)
15
16
15.2.20.4.2
17
closed? Visibility: public Behavior: a) b) If the receiver is closed, return true. Otherwise, return false. IO#each
18
19
20
21
22
15.2.20.4.3
23
each( &block ) Visibility: public Behavior: 238
24
25
1
a) b) c) d)
If the block is not given, the behavior is implementation dependent. If the receiver is not readable, raise a direct instance of the class IOError. If the receiver has reached its end, return the receiver. Otherwise, read characters from the receiver until 0x0a is read or the receiver reaches its end. Call the block with an argument, a direct instance of the class String whose content is the sequence of characters read in Step d. Continue processing from Step c. IO#each byte
2
3
4 5
6 7
e)
8
f)
9
15.2.20.4.4
10
each byte( &block ) Visibility: public Behavior: a) b) c) d) If the block is not given, the behavior is implementation dependent. If the receiver is not readable, raise a direct instance of the class IOError. If the receiver has reached its end, return the receiver. Otherwise, read a single byte from the receiver. Call the block with an argument, an instance of the class Integer whose value is the byte. Continue processing from Step c. 15.2.20.4.5 IO#each line
11
12
13
14
15
16 17
18
19
20
each line( &block ) Visibility: public Behavior: Same as the method each (see §15.2.20.4.3). 15.2.20.4.6 IO#eof ?
21
22
23
24
eof? Visibility: public Behavior: 239
25
26
1
a) b)
If the receiver is not readable, raise a direct instance of the class IOError. If the receiver has reached its end, return true. Otherwise, return false. IO#flush
2
3
15.2.20.4.7
4
flush Visibility: public Behavior: a) b) If the receiver is not writable, raise a direct instance of the class IOError. If the buffering flag of the receiver is true, and the receiver is buffering any output, immediately write all the buffered data to the stream which the receiver represents. Return the receiver. IO#getc
5
6
7
8 9
10
c)
11
15.2.20.4.8
12
getc Visibility: public Behavior: a) b) c) If the receiver is not readable, raise a direct instance of the class IOError. If the receiver has reached its end, return nil. Otherwise, read a character from the receiver. Return an instance of the class Object which represents the character. IO#gets
13
14
15
16
17 18
19
15.2.20.4.9
20
gets Visibility: public Behavior: a) b) c) If the receiver is not readable, raise a direct instance of the class IOError. If the receiver has reached its end, return nil. Otherwise, read characters from the receiver until 0x0a is read or the receiver reaches its end.
21
22
23
24
25 26
240
1 2
d)
Return a direct instance of the class String whose content is the sequence of characters read in Step c. IO#initialize copy
3
15.2.20.4.10
4
initialize copy( original ) Visibility: private Behavior: The behavior of the method is implementation dependent. 15.2.20.4.11 IO#print
5
6
7
8
print( *args ) Visibility: public Behavior: a) For each element of the args in the indexing order: 1) If the element is nil, let V be an instance of the class String whose content is “nil”. A conforming processor may let V be an empty instance of the class String. 2) 3) b) If the element is not nil, let V be the element. Invoke the method write on the receiver with V as the argument.
9
10
11
12 13
14
15
16
17
Return nil. IO#putc
18
15.2.20.4.12
19
putc( obj ) Visibility: public Behavior: a) If the obj is not an instance of the class Integer or an instance of the class String, the behavior is implementation dependent. If the obj is an instance of the class Integer whose value is smaller than 0 or larger than 255, the behavior is implementation dependent. If the obj is an instance of the class Integer, create a direct instance of the class String S whose content is a single character, whose character code is the integer represented by obj . 241
20
21
22 23 24 25
26 27 28
b)
1 2
c)
If the obj is an instance of the class String, create a direct instance of the class String S whose content is the first character of the obj . Invoke the method write on the receiver with S as the argument. Return the obj . IO#puts
3
d) e)
4
5
15.2.20.4.13
6
puts( *args ) Visibility: public Behavior: a) If the length of the args is 0, Invoke the method write on the receiver with an argument, which is a direct instance of the class String whose content is a single character 0x0a. Otherwise, for each element E of the args in the indexing order: 1) If E is nil: i) let S be an instance of the class String whose content is “nil”. A conforming processor may let S be an empty instance of the class String. ii) Invoke the method write on the receiver with S as the argument.
7
8
9 10
11
b)
12
13
14
15
16 17
iii) Invoke the method write on the receiver with an argument, which is an instance of the class String whose content is a single character 0x0a. 2) If E is an instance of the class Array, for each element X of E in the indexing order: i) If X is the same object as E, i.e. if E contains itself, invoke the method write on the receiver with an instance of the class String, whose content is implementation defined. Otherwise, invoke the method write on the receiver with X as the argument.
18 19
20 21 22
23
ii) 3)
24
If E is an instance of the class String: i) ii) Invoke the method write on the receiver with E as the argument. If the last character of E is not 0x0a, invoke the method write on the receiver with an argument, which is an instance of the class String whose content is a single character 0x0a.
25
26 27 28
29
c) 242
Return nil.
1
15.2.20.4.14
IO#read
2
read( length =nil ) Visibility: public Behavior: a) b) If the receiver is not readable, raise a direct instance of the class IOError. If the receiver has reached its end: 1) If the length is nil, create an empty instance of the class String and return that instance. If the length is not nil, return nil.
3
4
5
6
7 8
9
2) c)
10
Otherwise: 1) If the length is nil, read characters from the receiver until the receiver reaches its end. If the length is an instance of the class Integer, let N be the value of the length. Otherwise, the behavior is implementation dependent. If N is smaller than 0, raise a direct instance of the class ArgumentError. Read bytes from the receiver until N bytes are read or the receiver reaches its end.
11 12
13 14
2)
15
3) 4) d)
16
17 18
Return a direct instance of the class String whose content is the sequence of characters read in Step c. IO#readchar
19
15.2.20.4.15
20
readchar Visibility: public Behavior: a) b) c) If the receiver is not readable, raise a direct instance of the class IOError. If the receiver has reached its end, raise a direct instance of the class EOFError. Otherwise, read a character from the receiver. Return an instance of the class Object which represents the character. IO#readline 243
21
22
23
24
25 26
27
15.2.20.4.16
1
readline Visibility: public Behavior: a) b) c) If the receiver is not readable, raise a direct instance of the class IOError. If the receiver has reached its end, raise a direct instance of the class EOFError. Otherwise, read characters from the receiver until 0x0a is read or the receiver reaches its end. Return a direct instance of the class String whose content is the sequence of characters read in Step c. IO#readlines
2
3
4
5
6 7
8 9
d)
10
15.2.20.4.17
11
readlines Visibility: public Behavior: a) b) c) d) If the receiver is not readable, raise a direct instance of the class IOError. Create an empty direct instance of the class Array A. If the receiver has reached to its end, return A. Otherwise, read characters from the receiver until 0x0a is read or the receiver reaches its end. Append a direct instance of the class String whose content is the sequence of characters read in Step d to A. Continue processing from Step c. IO#sync
12
13
14
15
16
17 18
19 20
e)
21
f)
22
15.2.20.4.18
23
sync Visibility: public Behavior: a) b) 244 If the receiver is closed, raise a direct instance of the class IOError. If the buffering flag of the receiver is true, return false. Otherwise, return true.
24
25
26
27
1
15.2.20.4.19
IO#sync=
2
sync =( bool ) Visibility: public Behavior: a) b) If the receiver is closed, raise a direct instance of the class IOError. If the bool is a true value, set the buffering flag of the receiver to false. If the bool is a false value, set the buffering flag of the receiver to true. Return the bool . IO#write
3
4
5
6 7
8
c)
9
15.2.20.4.20
10
write( str ) Visibility: public Behavior: a) If the str is not an instance of the class String, the behavior is implementation dependent. If the str is empty, return an instance of the class Integer whose value is 0. If the receiver is not writable, raise a direct instance of the class IOError. Write all the characters in the str to the stream which the receiver represents, preserving their order. Return an instance of the class Integer, whose value is implementation defined. File
11
12
13 14
15
b) c) d)
16
17 18
19
e) 15.2.21
20
21
Instances of the class File represent opened files. A conforming processor may raise an instance of the class SystemCallError during the execution of the methods of the class File if the underlying system reports an error. A path of a file is a sequence of characters which represents the location of the file. The correct syntax of paths is implementation defined. An instance of the class File has the following property: path: The path of the file. 245
22 23
24 25
26
27
1
15.2.21.1 The class IO 15.2.21.2 15.2.21.2.1
Direct superclass
2
3
Singleton methods File.exist?
4
5
File.exist?( path ) Visibility: public Behavior: a) b) If the file specified by the path exists, return true. Otherwise, return false. Instance methods File#initialize
6
7
8
9
10
15.2.21.3 15.2.21.3.1
11
12
initialize( path, mode ="r" ) Visibility: private Behavior: a) If the path is not an instance of the class String, the behavior is implementation dependent. If the mode is not an instance of the class String whose content is a single character “r” or “w”, the behavior is implementation dependent. Open the file specified by the path in an implementation defined way, and associate it with the receiver. Set the path of the receiver to the content of the path. Set the openness flag and the buffering flag of the receiver to true. Set the readability flag and the writability flag of the receiver as follows: 1) If the mode is an instance of the class String whose content is a single character “r”, set the readability flag of the receiver to true and set the writability flag of the receiver to false. If the mode is an instance of the class String whose content is a single character “w”, set the readability flag of the receiver to false and set the writability flag of the receiver to true.
13
14
15 16
17 18
b)
19 20
c)
21
d) e) f)
22
23
24 25 26
27 28 29
2)
246
1
g)
Return an implementation defined value. File#path
2
15.2.21.3.2
3
path Visibility: public Behavior: The method returns a direct instance of the class String whose content is the path of the receiver. 15.2.22 Exception
4
5 6
7
8 9
Instances of the class Exception represent exceptions. The class Exception is a superclass of all the other exception classes. Instances of the class Exception have the following property. message: An object returned by the method to s (see §15.2.22.4.3). When the method clone (see §15.3.1.2.8) or the method dup (see §15.3.1.2.9) of the class Kernel is invoked on an instance of the class Exception, the message property shall be copied from the receiver to the resulting value. 15.2.22.1 Direct superclass
10
11
12 13 14
15
16
The class Object 15.2.22.2 Built-in exception classes
17
18 19 20 21
This document defines several built-in subclasses of the class Exception. Figure 1 shows the list of these subclasses and their class hierarchy. A conforming processor shall raise instances of these built-in subclasses in various erroneous conditions as described in this document. The class hierarchy shown in Figure 1 is used to handle an exception (see §14). 15.2.22.3 15.2.22.3.1 Singleton methods Exception.exception
22
23
24
Exception.exception( *args, &block ) Visibility: public Behavior: Same as the method new (see §15.2.3.2.3). 15.2.22.4 15.2.22.4.1 Instance methods Exception#exception 247
25
26
27
28
Figure 1 – The exception class hierarchy Exception StandardError ArgumentError LocalJumpError RangeError RegexpError RuntimeError TypeError ZeroDivisionError NameError NoMethodError IndexError StopIteration IOError EOFError SystemCallError ScriptError SyntaxError LoadError
1
exception( *string ) Visibility: public Behavior: a) b) If the length of the string is 0, return the receiver. If the length of the string is 1: 1) 2) If the only argument is the same object as the receiver, return the receiver. Otherwise let M be the argument. i) ii) Create a direct instance of the class of the receiver. Let E be the instance. Set the message property of E to M.
2
3
4
5
6
7
8
9
10
iii) Return E. c) 248 If the length of the string is larger than 1, raise a direct instance of the class ArgumentError.
11
1
15.2.22.4.2
Exception#message
2
message Visibility: public Behavior: a) b) Invoke the method to s on the receiver with no arguments. Return the resulting value of the invocation. Exception#to s
3
4
5
6
7
15.2.22.4.3
8
to s Visibility: public Behavior: a) b) c) d) Let M be the message property of the receiver. If M is nil, return an implementation defined value. If M is not an instance of the class String, the behavior is implementation dependent. Otherwise, return M. Exception#initialize
9
10
11
12
13
14
15
15.2.22.4.4
16
initialize( message = nil ) Visibility: private Behavior: a) b) 15.2.23 Set the message property of the receiver to the message. Return an implementation defined value. StandardError
17
18
19
20
21
22 23
Instances of the class StandardError represent standard errors, which can be handled in a rescue-clause without a exception-class-list (see §11.4.1.4.1). 15.2.23.1 Direct superclass
24
25
The class Exception 249
1
15.2.24
ArgumentError
2
Instances of the class ArgumentError represent argument errors. 15.2.24.1 Direct superclass
3
4
The class StandardError 15.2.25 LocalJumpError
5
6 7
Instances of the class LocalJumpError represent errors which occur while evaluating block s and jump-expressions. 15.2.25.1 Direct superclass
8
9
The class StandardError 15.2.25.2 15.2.25.2.1 Instance methods LocalJumpError#exit value
10
11
12
exit value Visibility: public Behavior: The method returns the value of the instance variable @exit value of the receiver. 15.2.25.2.2 LocalJumpError#reason
13
14 15
16
17
reason Visibility: public Behavior: The method returns the value of the instance variable @reason of the receiver. 15.2.26 RangeError
18
19
20
21
Instances of the class RangeError represent range errors. 15.2.26.1 Direct superclass
22
23
The class StandardError 15.2.27 RegexpError
24
25
Instances of the class RegexpError represent regular expression errors. 15.2.27.1 Direct superclass
26
27
The class StandardError 250
1
15.2.28
RuntimeError
2 3
Instances of the class RuntimeError represent runtime errors, which are raised by the method raise of the class Kernel by default (see §15.3.1.1.13). 15.2.28.1 Direct superclass
4
5
The class StandardError 15.2.29 TypeError
6
7
Instances of the class TypeError represent type errors. 15.2.29.1 Direct superclass
8
9
The class StandardError 15.2.30 ZeroDivisionError
10
11
Instances of the class ZeroDivisionError represent zero division errors. 15.2.30.1 Direct superclass
12
13
The class StandardError 15.2.31 NameError
14
15
Instances of the class NameError represent errors which occur while resolving names to values. Instances of the class NameError have the following property. name: The name a reference to which causes this exception to be raised. When the method clone (see §15.3.1.2.8) or the method dup (see §15.3.1.2.9) of the class Kernel is invoked on an instance of the class NameError, the name property shall be copied from the receiver to the resulting value. 15.2.31.1 Direct superclass
16
17
18 19 20
21
22
The class StandardError 15.2.31.2 15.2.31.2.1 Instance methods NameError#name
23
24
25
name Visibility: public Behavior: The method returns the name property of the receiver. 251
26
27
1
15.2.31.2.2
NameError#initialize
2
initialize( message =nil, name =nil ) Visibility: public Behavior: a) b) Set the name property of the receiver to the name. Invoke the method initialize defined in the class Exception, which is a superclass of the class NameError, as if super-with-argument were evaluated with the message as the argument of the super-with-argument. Return an implementation defined value. NoMethodError
3
4
5
6 7 8
9
c) 15.2.32
10
11 12
Instances of the class NoMethodError represent errors which occur while invoking methods which do not exists or which cannot be invoked. Instances of the class NoMethodError have properties called name (see §15.2.31) and arguments. The values of these properties are set in the method initialize (see §15.2.32.2.2). When the method clone (see §15.3.1.2.8) or the method dup (see §15.3.1.2.9) of the class Kernel is invoked on an instance of the class NoMethodError, those properties shall be copied from the receiver to the resulting value. 15.2.32.1 Direct superclass
13 14
15 16 17
18
19
The class NameError 15.2.32.2 15.2.32.2.1 Instance methods NoMethodError#args
20
21
22
args Visibility: public Behavior: The method returns the value of the arguments property of the receiver. 15.2.32.2.2 NoMethodError#initialize
23
24
25
26
initialize( message =nil, name =nil, args =nil ) Visibility: public 252
27
1
Behavior: a) b) c) 15.2.33 Set the arguments property of the receiver to the args. Perform all the steps of the method initialize described in §15.2.31.2.2. Return an implementation defined value. IndexError
2
3
4
5
6
Instances of the class IndexError represent index errors. 15.2.33.1 Direct superclass
7
8
The class StandardError 15.2.34 StopIteration
9
10 11
Instances of the class StopIteration represent exceptions, which are raised to terminate the method loop of the class Kernel (see §15.3.1.1.8). 15.2.34.1 Direct superclass
12
13
The class IndexError 15.2.35 IOError
14
15
Instances of the class IOError represent input/output errors. 15.2.35.1 Direct superclass
16
17
The class StandardError 15.2.36 EOFError
18
19
Instances of the class EOFError represent errors which occur when a stream has reached its end. 15.2.36.1 Direct superclass
20
21
The class IOError 15.2.37 SystemCallError
22
23 24
Instances of the class SystemCallError represent errors which occur while invoking the instance methods of the class IO. 15.2.37.1 Direct superclass
25
26
The class StandardError 15.2.38 ScriptError
27
28
Instances of the class ScriptError represent programming errors. 253
1
15.2.38.1
Direct superclass
2
The class Exception 15.2.39 SyntaxError
3
4
Instances of the class SyntaxError represent syntax errors. 15.2.39.1 Direct superclass
5
6
The class ScriptError 15.2.40 LoadError
7
8 9
Instances of the class LoadError represent errors which occur while loading external programs (see §15.3.1.1.14). 15.2.40.1 Direct superclass
10
11
The class ScriptError
12
15.3 Built-in modules
15.3.1 Kernel
13
14 15
The module Kernel is included in the class Object. Unless overridden, instance methods defined in the module Kernel can be invoked on any instance of the class Object. 15.3.1.1 15.3.1.1.1 Singleton methods Kernel.‘
16
17
18
Kernel.‘( string ) The method ‘ is invoked in the form described in §8.5.5.2.6. Visibility: public Behavior: The method ‘ executes an external command corresponding to the string. The external command executed by the method is implementation defined. When the method is invoked, take the following steps: a) If the string is not an instance of the class String, the behavior is implementation dependent. Execute the command which corresponds to the content of the string. Let R be the output of the command. Create a direct instance of the class String whose content is R, and return the instance.
19
20
21 22
23
24 25
26 27
b)
28
c) 254
1
15.3.1.1.2
Kernel.block given?
2
Kernel.block given? Visibility: public Behavior: a) b) If the top of [block] is block-not-given, return false. Otherwise, return true. Kernel.eval
3
4
5
6
7
15.3.1.1.3
8
Kernel.eval( string ) Visibility: public Behavior: a) If the string is not an instance of the class String, the behavior is implementation dependent. Parse the content of the string as a program (see §10.1). If it fails, raise a direct instance of the class SyntaxError. Evaluate the program. Let V be the resulting value of the evaluation. Return V.
9
10
11 12
13 14
b)
15
c) d)
16
17 18 19
In Step c, the string is evaluated under the new local variable scope in which references to local-variable-identifier s are resolved in the same way as in scopes created by block s (see §9.1.1). 15.3.1.1.4 Kernel.global variables
20
21
Kernel.global variables Visibility: public Behavior: The method returns a new direct instance of the class Array which consists of names of all the global variables. These names are represented by instances of either the class String or the class Symbol. Which of those classes is chosen is implementation defined. 15.3.1.1.5 Kernel.iterator? 255
22
23 24 25 26
27
1
Kernel.iterator? Visibility: public Behavior: Same as the method Kernel.block given? (see §15.3.1.1.2). 15.3.1.1.6 Kernel.lambda
2
3
4
5
Kernel.lambda( &block ) Visibility: public Behavior: The method creates an instance of the class Proc as Proc.new does (see §15.2.17.2.1). However, the way in which the block is evaluated differs from the one described in §11.2.2 except when the block is called by a yield-expression. The differences are as follows. a) Before Step d of §11.2.2, the number of arguments is checked as follows: 1) 2) Let A be the list of arguments passed to the block. Let Na be the length of A. If the block-formal-argument-list is of the form left-hand-side, and if Na is not 1, the behavior is implementation dependent. If the block-formal-argument-list is of the form multiple-left-hand-side: i) If the multiple-left-hand-side is not of the form grouped-left-hand-side or packingleft-hand-side: I) Let Np be the number of multiple-left-hand-side-items of the multipleleft-hand-side.
6
7 8 9
10
11
12
13 14
15
3)
16 17
18 19
20
II) If Na < Np , raise a direct instance of the class ArgumentError. III) If a packing-left-hand-side does not occur, and if Na > Np , raise a direct instance of the class ArgumentError. ii) If the multiple-left-hand-side is of the form grouped-left-hand-side, and if Na is not 1, the behavior is implementation dependent.
21 22
23 24
25 26 27 28
b)
In Step e of §11.2.2, when the evaluation of the block associated with a lambda invocation is terminated by a return-expression or break-expression, the execution context is restored to Eo (i.e. the saved execution context), and the evaluation of the lambda invocation is terminated. The value of the lambda invocation is determined as follows: 1) If the jump-argument of the return-expression or the break-expression occurs, the value of the lambda invocation is the value of the jump-argument.
29
30 31
256
1
2) 15.3.1.1.7
Otherwise, the value of the lambda invocation is nil. Kernel.local variables
2
3
Kernel.local variables Visibility: public Behavior: The method returns a new direct instance of the class Array which contains all the names of local variable bindings which meet the following conditions.
4
5 6
7
The name of the binding is of the form local-variable-identifier. The binding can be resolved in the scope of local variables which includes the point of invocations of this method by the process described in §9.1.1.
8 9
10 11 12
In the instance of the class Array returned by the method, names of the local variables are represented by instances of either the class String or the class Symbol. Which of those classes is chosen is implementation defined. 15.3.1.1.8 Kernel.loop
13
14
Kernel.loop( &block ) Visibility: public Behavior: a) b) c) If the block is not given, the behavior is implementation dependent. Otherwise, repeat calling the block . If a direct instance of the class StopIteration is raised and not handled in Step b, handle the exception and return nil. Kernel.method missing
15
16
17
18
19 20
21
15.3.1.1.9
22
Kernel.method missing( symbol, *args ) Visibility: public Behavior: a) If the symbol is not an instance of the class Symbol, raise a direct instance of the class ArgumentError.
23
24
25 26
257
1 2 3 4 5
b)
Otherwise, raise a direct instance of the class NoMethodError which has the symbol as its name property and the args as its arguments property. A conforming processor may raise a direct instance of the class NameError which has the symbol as its name property instead of NoMethodError if the method is invoked in Step e of §13.3.3 during an evaluation of a local-variable-identifier as a method invocation. Kernel.p
6
15.3.1.1.10
7
Kernel.p( *args ) Visibility: public Behavior: a) For each element E of the args, in the indexing order, take the following steps: 1) Invoke the method inspect on E with no arguments and let X be the resulting value of this invocation. If X is not an instance of the class String, the behavior is implementation dependent. Invoke the method write on Object::STDOUT with X as the argument. Invoke the method write on Object::STDOUT with an argument, which is a direct instance of the class String whose content is a single character 0x0a.
8
9
10
11 12
13 14
2)
15
3) 4)
16 17
18
b)
Return nil. A conforming processor may return the args instead of nil. Kernel.print
19
15.3.1.1.11
20
Kernel.print( *args ) Visibility: public Behavior: The method behaves as if the method print of the class IO (see §15.2.20.4.11) were invoked on Object::STDOUT with the same arguments. 15.3.1.1.12 Kernel.puts
21
22 23
24
25
Kernel.puts( *args ) Visibility: public Behavior: The method behaves as if the method puts of the class IO (see §15.2.20.4.13) were invoked on Object::STDOUT with the same arguments. 258
26
27 28
1
15.3.1.1.13
Kernel.raise
2
Kernel.raise( *args ) Visibility: public Behavior: a) b) If the length of the args is larger than 2, the behavior is implementation dependent. If the length of the args is 0: 1) If the location of the method invocation is within an operator-expression 2 of an assignment-with-rescue-modifier, a fallback-statement-of-rescue-modifier-statement, or a rescue-clause, let E be the current exception (see §14.2). Otherwise, invoke the method new on the class RuntimeError with no argument. Let E be the resulting value.
3
4
5
6
7 8 9
10 11
2)
12
c)
If the length of the args is 1, let A be the only argument. 1) If A is an instance of the class String, invoke the method new on the class RuntimeError with A as the only argument. Let E be the resulting instance. Otherwise, invoke the method exception on A. Let E be the resulting value. If E is not an instance of the class Exception, raise a direct instance of the class TypeError.
13 14
15
2) 3)
16 17
18 19
d)
If the length of the args is 2, let F and S be the first and the second argument, respectively. 1) Invoke exception on F with S as the only argument. Let E be the resulting value. If E is not an instance of the class Exception, raise a direct instance of the class TypeError.
20 21
22 23
2)
24
e)
Raise E. Kernel.require
25
15.3.1.1.14
26
Kernel.require( string ) Visibility: public Behavior: The method require evaluates the external program P corresponding to the string. The way in which P is determined from the string is implementation defined. When the method is invoked, take the following steps: 259
27
28 29
30
1 2
a)
If the string is not an instance of the class String, the behavior is implementation dependent. Search for the external program P corresponding to the string. If the program does not exist, raise a direct instance of the class LoadError. If P cannot be derived from the program (§10.1), raise a direct instance of the class SyntaxError. Change the state of the execution context temporarily for the evaluation of P as follows: 1) [self ]
3
b) c) d)
4
5 6
7
e)
8 9
contains only one object which is the object at the bottom of [self ] in the current execution context. contains only one list whose only element is the class Object. contains only one visibility, which is the private visibility.
10
2) 3) 4) f) g)
[class-module-list] [default-visibility]
11
12
All the other attributes of the execution context are empty logical stacks.
13
Evaluate P under the execution context set up in Step e. Restore the state of the execution context as it is just before Step e, even when an exception is raised and not handled during the evaluation of P. Note that the evaluation of P affects the restored execution context if it changes the attributes of objects in the original execution context.
14 15
16 17
18
h) 15.3.1.2
Unless an exception is raised and not handled in Step f, return true. Instance methods Kernel#==
19
20
15.3.1.2.1
21
= =( other ) Visibility: public Behavior: a) b) If the receiver and the other is the same object, return true. Otherwise, return false. Kernel#===
22
23
24
25
26
15.3.1.2.2
27
= = =( other )
260
1
Visibility: public Behavior: a) b) If the receiver and the other is the same object, return true. Otherwise, invoke the method == on the receiver with the other as the only argument. Let V be the resulting value. If V is a true value, return true. Otherwise, return false. Kernel# id
2
3
4 5
6
c)
7
15.3.1.2.3
8
id Visibility: public Behavior: Same as the method object id (see §15.3.1.2.33). 15.3.1.2.4 Kernel# send
9
10
11
12
send ( symbol, *args, &block ) Visibility: public Behavior: Same as the method send (see §15.3.1.2.44). 15.3.1.2.5 Kernel#‘
13
14
15
16
‘( string ) The method ‘ is invoked in the form described in §8.5.5.2.6. Visibility: private Behavior: Same as the method Kernel.‘ (see §15.3.1.1.1). 15.3.1.2.6 Kernel#block given?
17
18
19
20
21
block given? Visibility: private Behavior: Same as the method Kernel.block given? (see §15.3.1.1.2).
22
23
261
1
15.3.1.2.7
Kernel#class
2
class Visibility: public Behavior: The method returns the class of the receiver. 15.3.1.2.8 Kernel#clone
3
4
5
6
clone Visibility: public Behavior: a) If the receiver is an instance of one of the following classes: NilClass, TrueClass, FalseClass, Integer, Float, or Symbol, then raise a direct instance of the class TypeError. Create a direct instance of the class of the receiver which has no bindings of instance variables. Let O be the newly created instance. For each binding B of the instance variables of the receiver, create a variable binding with the same name and value as B in the set of bindings of instance variables of O. If the receiver is associated with an eigenclass, let Eo be the eigenclass, and take the following steps: 1) Create an eigenclass whose direct superclass is the direct superclass of Eo . Let En be the eigenclass. For each binding Bv1 of the constants of Eo , create a variable binding with the same name and value as Bv1 in the set of bindings of constants of En . For each binding Bv2 of the class variables of Eo , create a variable binding with the same name and value as Bv2 in the set of bindings of class variables of En . For each binding Bm of the instance methods of Eo , create a method binding with the same name and value as Bm in the set of bindings of instance methods of En . Associate O with En .
7
8
9 10 11
12 13
b)
14 15
c)
16 17
d)
18 19
20 21
2)
22 23
3)
24 25
4)
26
5) e) f)
27
Invoke the method initialize copy on O with the receiver as the argument. Return O. Kernel#dup
28
29
15.3.1.2.9 262
1
dup Visibility: public Behavior: a) If the receiver is an instance of one of the following classes: NilClass, TrueClass, FalseClass, Integer, Float, or Symbol, then raise a direct instance of the class TypeError. Create a direct instance of the class of the receiver which has no bindings of instance variables. Let O be the newly created instance. For each binding B of the instance variables of the receiver, create a variable binding with the same name and value as B in the set of bindings of instance variables of O. Invoke the method initialize copy on O with the receiver as the argument. Return O. Kernel#eql?
2
3
4 5 6
7 8
b)
9 10
c)
11
d) e)
12
13
15.3.1.2.10
14
eql?( other ) Visibility: public Behavior: Same as the method == (see §15.3.1.2.1). 15.3.1.2.11 Kernel#equal?
15
16
17
18
equal?( other ) Visibility: public Behavior: Same as the method == (see §15.3.1.2.1). 15.3.1.2.12 Kernel#eval
19
20
21
22
eval( string ) Visibility: private Behavior: Same as the method Kernel.eval (see §15.3.1.1.3). 15.3.1.2.13 Kernel#extend 263
23
24
25
1
extend( *module list ) Visibility: public Behavior: Let R be the receiver of the method. a) b) If the length of the module list is 0, raise a direct instance of the class ArgumentError. For each element A of the module list, take the following steps: 1) If A is not an instance of the class Module, raise a direct instance of the class TypeError. If A is an instance of the class Class, raise a direct instance of the class TypeError. Invoke the method extend object on A with R as the only argument. Invoke the method extended on A with R as the only argument.
2
3
4
5
6 7
8
2) 3) 4) c)
9
10
11
Return R. Kernel#global variables
12
15.3.1.2.14
13
global variables Visibility: private Behavior: Same as the method Kernel.global variables (see §15.3.1.1.4). 15.3.1.2.15 Kernel#hash
14
15
16
17
hash Visibility: public Behavior: The method returns an instance of the class Integer. When invoked on the same object, the method shall always return an instance of the class Integer whose values is same. When a conforming processor overrides the method eql? (see §15.3.1.2.10), it shall override the method hash in the same class or module in which the method eql? is overridden in such a way that, if an invocation of the method eql? on an object with an argument returns a true value, invocations of the method hash on the object and the argument return the instances of the class Integer with the same value. 15.3.1.2.16 264 Kernel#initialize copy
18
19 20 21
22 23 24 25 26
27
1
initialize copy( original ) Visibility: private Behavior: The method initialize copy is invoked when an object is created by the method clone (see §15.3.1.2.8) or the method dup (see §15.3.1.2.9). When the method is invoked, take the following steps: a) If the classes of the receiver and the original are not the same class, raise a direct instance of the class TypeError. Return an implementation defined value. Kernel#inspect
2
3 4
5
6 7
8
b)
9
15.3.1.2.17
10
inspect Visibility: public Behavior: The method returns an instance of the class String, the content of which represents the state of the receiver. The content of the resulting instance of the class String is implementation defined. 15.3.1.2.18 Kernel#instance eval
11
12 13 14
15
16
instance eval( string = nil, &block ) Visibility: public Behavior: a) If the receiver is an instance of one of the implementation defined set of classes as described in Step a of §13.4.2, or if the receiver is one of nil, true, or false, then the behavior is implementation dependent. If the receiver is not associated with an eigenclass, create a new eigenclass. Let M be the newly created eigenclass. If the receiver is associated with an eigenclass, let M be that eigenclass. Take Step b through the last step of the method class eval of the class Module (see §15.2.2.3.15). Kernel#instance of ?
17
18
19 20 21
22 23
b)
24
c) d)
25 26
27
15.3.1.2.19
265
1
instance of?( module ) Visibility: public Behavior: Let C be the class of the receiver. a) If the module is not an instance of the class Class or the class Module, raise a direct instance of the class TypeError. If the module and C are the same object, return true. Otherwise, return false. Kernel#instance variable defined?
2
3
4 5
6
b) c)
7
8
15.3.1.2.20
9
instance variable defined?( symbol ) Visibility: public Behavior: a) b) Let N be the name designated by the symbol . If N is not of the form instance-variable-identifier, raise a direct instance of the class NameError which has the symbol as its name property. If a binding of an instance variable with name N exists in the set of bindings of instance variables of the receiver, return true. Otherwise, return false. Kernel#instance variable get
10
11
12
13 14
15 16
c)
17
d)
18
15.3.1.2.21
19
instance variable get( symbol ) Visibility: public Behavior: a) b) Let N be the name designated by the symbol . If N is not of the form instance-variable-identifier, raise a direct instance of the class NameError which has the symbol as its name property. If a binding of an instance variable with name N exists in the set of bindings of instance variables of the receiver, return the value of the binding. Otherwise, return nil.
20
21
22
23 24
25 26
c)
27
d) 266
1
15.3.1.2.22
Kernel#instance variable set
2
instance variable set( symbol, obj ) Visibility: public Behavior: a) b) Let N be the name designated by the symbol . If N is not of the form instance-variable-identifier, raise a direct instance of the class NameError which has the symbol as its name property. If a binding of an instance variable with name N exists in the set of bindings of instance variables of the receiver, replace the value of the binding with the obj . Otherwise, create a variable binding with name N and value obj in the set of bindings of instance variables of the receiver. Return the obj . Kernel#instance variables
3
4
5
6 7
8 9
c)
10 11
d)
12
e)
13
15.3.1.2.23
14
instance variables Visibility: public Behavior: The method returns a direct instance of the class Array which consists of names of all the instance variables of the receiver. These names are represented by instances of either the class String or the class Symbol. Which of those classes is chosen is implementation defined. 15.3.1.2.24 Kernel#is a?
15
16 17 18 19
20
21
is a?( module ) Visibility: public Behavior: a) If the module is not an instance of the class Class or the class Module, raise a direct instance of the class TypeError. Let C be the class of the receiver. If the module is an instance of the class Class and one of the following conditions holds, return true. 267
22
23
24 25
26
b) c)
27 28
1
The module and C are the same object. The module is a superclass of C. The module and the eigenclass of the receiver are the same object.
2
3
4 5
d)
If the module is an instance of the class Module and is included in C or one of the superclasses of C, return true. Otherwise, return false. Kernel#iterator?
6
e)
7
15.3.1.2.25
8
iterator? Visibility: private Behavior: Same as the method Kernel.iterator? (see §15.3.1.1.5). 15.3.1.2.26 Kernel#kind of ?
9
10
11
12
kind of?( module ) Visibility: public Behavior: Same as the method is a? (see §15.3.1.2.24). 15.3.1.2.27 Kernel#lambda
13
14
15
16
lambda( &block ) Visibility: private Behavior: Same as the method Kernel.lambda (see §15.3.1.1.6). 15.3.1.2.28 Kernel#local variables
17
18
19
20
local variables Visibility: private Behavior: Same as the method Kernel.local variables (see §15.3.1.1.7). 15.3.1.2.29 Kernel#loop
21
22
23
268
1
loop( &block ) Visibility: private Behavior: Same as the method Kernel.loop (see §15.3.1.1.8). 15.3.1.2.30 Kernel#method missing
2
3
4
5
method missing( symbol, *args ) Visibility: private Behavior: Same as the method Kernel.method missing (see §15.3.1.1.9). 15.3.1.2.31 Kernel#methods
6
7
8
9
methods( all =true ) Visibility: public Behavior: Let C be the class of the receiver. a) If the all is a true value, the method behaves as if the method instance methods were invoked on C with no arguments (see §15.2.2.3.33). If the all is a false value, the method behaves as if the method singleton methods were invoked on the receiver with false as the only argument (see §15.3.1.2.45). Kernel#nil?
10
11
12 13
14 15
b)
16
15.3.1.2.32
17
nil? Visibility: public Behavior: a) b) If the receiver is nil, return true. Otherwise, return false. Kernel#object id
18
19
20
21
22
15.3.1.2.33
269
1
object id Visibility: public Behavior: The method returns an instance of the class Integer with the same value whenever it is invoked on the same object. When invoked on two distinct objects, the method returns an instance of the class Integer with different value for each invocation. 15.3.1.2.34 Kernel#p
2
3 4 5
6
7
p( *args ) Visibility: private Behavior: Same as the method Kernel.p (see §15.3.1.1.10). 15.3.1.2.35 Kernel#print
8
9
10
11
print( *args ) Visibility: private Behavior: Same as the method Kernel.print (see §15.3.1.1.11). 15.3.1.2.36 Kernel#private methods
12
13
14
15
private methods( all =true ) Visibility: public Behavior: a) b) c) Create an empty direct instance of the class Array A. If the receiver is associated with an eigenclass, let C be the eigenclass. Let I be the set of bindings of instance methods of C. For each binding B of I, let N and V be the name and the value of B respectively, and take the following steps: 1) 2) If V is undef, or the visibility of V is not private, skip the next two steps. Let S be either a direct instance of the class String whose content is N or a direct instance of the class Symbol whose name is N. Which of the these classes of instance is chosen as the value of S is implementation defined.
16
17
18
19
20
21 22
23
24 25 26
270
1 2 3
3)
Unless A contains the element of the same name (if S is an instance of the class Symbol) or the same content (if S is an instance of the class String) as S, append S to A.
4 5
d)
For each module M in included module list of C, take Step c, assuming that C in that step to be M. Replace C with the class of the receiver, and take Step c. If the all is a true value: 1) 2) 3) Take Step d. Replace C with the direct superclass of current C. If C is not nil, take Step c, and then, repeat from Step f-1.
6
e) f)
7
8
9
10
11
g)
Return A. Kernel#protected methods
12
15.3.1.2.37
13
protected methods( all =true ) Visibility: public Behavior: Same as the method private methods (see §15.3.1.2.36), except that the method returns a direct instance of the class Array which contains names of protected methods. 15.3.1.2.38 Kernel#public methods
14
15 16
17
18
public methods Visibility: public Behavior: Same as the method private methods (see §15.3.1.2.36), except that the method returns a direct instance of the class Array which contains names of public methods. 15.3.1.2.39 Kernel#puts
19
20 21
22
23
puts( *args ) Visibility: private Behavior: Same as the method Kernel.puts (see §15.3.1.1.12). 15.3.1.2.40 Kernel#raise 271
24
25
26
1
raise( *args ) Visibility: private Behavior: Same as the method Kernel.raise (see §15.3.1.1.13). 15.3.1.2.41 Kernel#remove instance variable
2
3
4
5
remove instance variable( symbol ) Visibility: private Behavior: a) b) Let N be the name designated by the symbol . If N is not of the form instance-variable-identifier, raise a direct instance of the class NameError which has the symbol as its name property. If a binding of an instance variable with name N exists in the set of bindings of instance variables of the receiver, let V be the value of the binding. 1) 2) d) Remove the binding from the set of bindings of instance variables of the receiver. Return V.
6
7
8
9 10
11 12
c)
13
14
15 16
Otherwise, raise a direct instance of the class NameError which has the symbol as its name property. Kernel#require
17
15.3.1.2.42
18
require( *args ) Visibility: private Behavior: Same as the method Kernel.require (see §15.3.1.1.14). 15.3.1.2.43 Kernel#respond to?
19
20
21
22
respond to?( symbol, include private =false ) Visibility: public Behavior: 272
23
24
1
a) b)
Let N be the name designated by the symbol . Search for a binding of an instance method named N starting from the receiver of the method as described in §13.3.4. If a binding is found, let V be the value of the binding. 1) 2) If V is undef, return false. If the visibility of V is private: i) ii) 3) If the include private is a true value, return true. Otherwise, return false.
2 3
4
c)
5
6
7
8
9
Otherwise, return true.
10
d)
Otherwise, return false. Kernel#send
11
15.3.1.2.44
12
send( symbol, *args, &block ) Visibility: public Behavior: a) b) Let N be the name designated by the symbol . Invoke the method named N on the receiver with the args as arguments and the block as the block, if any. Return the resulting value of the invocation. Kernel#singleton methods
13
14
15
16 17
18
c)
19
15.3.1.2.45
20
singleton methods( all =true ) Visibility: public Behavior: Let E be the eigenclass of the receiver. a) b) Create an empty direct instance of the class Array A. Let I be the set of bindings of instance methods of E. For each binding B of I, let N and V be the name and the value of B respectively, and take the following steps: 273
21
22
23
24
25 26
1
1) 2)
If V is undef, or the visibility of V is private, skip the next two steps. Let S be either a direct instance of the class String whose content is N or a direct instance of the class Symbol whose name is N. Which of the these classes of instance is chosen as the value of S is implementation defined. Unless A contains the element of the same name (if S is an instance of the class Symbol) or the same content (if S is an instance of the class String), append S to A.
2 3 4
5 6 7
3)
8 9
c)
If the all is a true value, for each module M in included module list of E, take Step b, assuming that E in that step to be M. Return A. Kernel#to s
10
d)
11
15.3.1.2.46
12
to s Visibility: public Behavior: The method returns an instance of the class String, the content of which is the string representation of the receiver. The content of the resulting instance of the class String is implementation defined. 15.3.2 Enumerable
13
14 15 16
17
18 19
The module Enumerable provides methods which iterates over the elements of the object using the method each. In the following description of the methods of the module Enumerable, an element of the receiver means one of the values which is yielded by the method each. 15.3.2.1 15.3.2.1.1 Instance methods Enumerable#all?
20 21
22
23
24
all?( &block ) Visibility: public Behavior: a) b) Invoke the method each on the receiver. For each element X which the method each yields: 1) If the block is given, call the block with X as the argument. If this call results in a false value, return false. 274
25
26
27
28
29
30
1
2) c)
If the block is not given, and X is a false value, return false.
2
Return true. Enumerable#any?
3
15.3.2.1.2
4
any?( &block ) Visibility: public Behavior: a) b) Invoke the method each on the receiver. For each element X which each yields: 1) If the block is given, call the block with X as the argument. If this call results in a true value, return true. 2) c) If the block is not given, and X is a true value, return true.
5
6
7
8
9
10
11
12
Return false. Enumerable#collect
13
15.3.2.1.3
14
collect( &block ) Visibility: public Behavior: a) b) c) d) If the block is not given, the behavior is implementation dependent. Create an empty direct instance of the class Array A. Invoke the method each on the receiver. For each element X which each yields, call the block with X as the argument and append the resulting value to A. Return A. Enumerable#detect
15
16
17
18
19
20 21
22
e)
23
15.3.2.1.4
275
1
detect( ifnone =nil, &block ) Visibility: public Behavior: a) b) c) If the block is not given, the behavior is implementation dependent. Invoke the method each on the receiver. For each element X which each yields, call the block with X as the argument. If this call results in a true value, return X. Return the ifnone. Enumerable#each with index
2
3
4
5
6 7
8
d)
9
15.3.2.1.5
10
each with index( &block ) Visibility: public Behavior: a) b) c) d) If the block is not given, the behavior is implementation dependent. Let i be 0. Invoke the method each on the receiver. For each element X which each yields: 1) 2) e) Call the block with X and i as the arguments. Increase i by 1.
11
12
13
14
15
16
17
18
19
Return the receiver. Enumerable#entries
20
15.3.2.1.6
21
entries Visibility: public Behavior: a) 276 Create an empty direct instance of the class Array A.
22
23
24
1
b) c) d)
Invoke the method each on the receiver. For each element X which each yields, append X to A. Return A. Enumerable#find
2
3
4
15.3.2.1.7
5
find( ifnone =nil, &block ) Visibility: public Behavior: Same as the method detect (see §15.3.2.1.4). 15.3.2.1.8 Enumerable#find all
6
7
8
9
find all( &block ) Visibility: public Behavior: a) b) c) d) If the block is not given, the behavior is implementation dependent. Create an empty direct instance of the class Array A. Invoke the method each on the receiver. For each element X which each yields, call the block with X as the argument. If this call results in a true value, append the element to A. Return A. Enumerable#grep
10
11
12
13
14
15 16
17
e)
18
15.3.2.1.9
19
grep( pattern, &block ) Visibility: public Behavior: a) b) c) Create an empty direct instance of the class Array A. Invoke the method each on the receiver. For each element X which each yields, invoke the method === on the pattern with X as the argument. If this invocation results in a true value: 277
20
21
22
23
24 25
26
1 2
1)
If the block is given, call the block with X as the argument and append the resulting value to A. Otherwise, append X to A.
3
2) d)
4
Return A. Enumerable#include?
5
15.3.2.1.10
6
include?( obj ) Visibility: public Behavior: a) b) Invoke the method each on the receiver. For each element X which each yields, invoke the method == on X with the obj as the argument. If this invocation results in a true value, return true. Return false. Enumerable#inject
7
8
9
10 11
12
c)
13
15.3.2.1.11
14
inject( *args, &block ) Visibility: public Behavior: a) b) If the block is not given, the behavior is implementation dependent. If the length of the args is 2, the behavior is implementation dependent. If the length of the args is smaller than 0 or larger than 2, raise a direct instance of the class ArgumentError. Invoke the method each on the receiver. If the method each does not yield any element, return nil. For each element X which each yields: 1) 2) If X is the first element, and the length of the args is 0, let V be X. If X is the first element, and the length of the args is 1, call the block with two arguments, which are the only element of the args and X. Let V be the resulting value of this call. If X is not the first element, call the block with V and X as the arguments. Let new V be the resulting value of this call.
15
16
17
18 19 20
21 22
c)
23
d)
24
25 26 27
28 29
3)
278
1
e)
Return V. Enumerable#map
2
15.3.2.1.12
3
map( &block ) Visibility: public Behavior: Same as the method collect (see §15.3.2.1.3). 15.3.2.1.13 Enumerable#max
4
5
6
7
max( &block ) Visibility: public Behavior: a) b) c) Invoke the method each on the receiver. If the method each does not yield any elements, return nil. For each element X which the method each yields: 1) 2) If X is the first element, let V be X. Otherwise, if the block is given: i) Call the block with X and V as the arguments. Let D be the result of this call. If D is not an instance of the class Integer, the behavior is implementation dependent.
8
9
10
11
12
13
14
15 16
17 18
ii)
19
iii) If the value of D is larger than 0, let new V be X. If the block is not given: i) Invoke the method <=> on X with V as the argument. Let D be the result of this invocation. If D is not an instance of the class Integer, the behavior is implementation dependent.
20
21 22
23 24
ii)
25
iii) If the value of D is larger than 0, let new V be X. d) Return V.
26
279
1
15.3.2.1.14
Enumerable#min
2
min( &block ) Visibility: public Behavior: a) b) c) Invoke the method each on the receiver. If the method each does not yield any elements, return nil. For each element X which the method each yields: 1) 2) If X is the first element, let V be X. Otherwise, if the block is given: i) Call the block with X and V as the arguments. Let D be the result of this call. If D is not an instance of the class Integer, the behavior is implementation dependent.
3
4
5
6
7
8
9
10 11
12 13
ii)
14
iii) If the value of D is smaller than 0, let new V be X. If the block is not given: i) Invoke the method <=> on X with V as the argument. Let D be the result of this invocation. If D is not an instance of the class Integer, the behavior is implementation dependent.
15
16 17
18 19
ii)
20
iii) If the value of D is smaller than 0, let new V be X. d) Return V. Enumerable#member?
21
22
15.3.2.1.15
23
member?( obj ) Visibility: public Behavior: Same as the method include? (see §15.3.2.1.10). 15.3.2.1.16 Enumerable#partition
24
25
26
280
1
partition( &block ) Visibility: public Behavior: a) b) c) d) If the block is not given, the behavior is implementation dependent. Create two empty instances of the class Array T and F. Invoke the method each on the receiver. For each element X which each yields, call the block with X as the argument. If this call results in a true value, append X to T. If this call results in a false value, append X to F. e) Return a newly created instance the class Array, which contains only T and F in this order. Enumerable#reject
2
3
4
5
6
7
8 9
10 11
12
15.3.2.1.17
13
reject( &block ) Visibility: public Behavior: a) b) c) d) If the block is not given, the behavior is implementation dependent. Create an empty direct instance of the class Array A. Invoke the method each on the receiver. For each element X which each yields, call the block with X as the argument. If this call results in a false value, append the element to A. Return A. Enumerable#select
14
15
16
17
18
19 20
21
e)
22
15.3.2.1.18
23
select( &block ) Visibility: public Behavior: Same as the method find all (see §15.3.2.1.8). 281
24
25
1
15.3.2.1.19
Enumerable#sort
2
sort( &block ) Visibility: public Behavior: a) b) c) Create an empty direct instance of the class Array A. Invoke the method each on the receiver. Insert all the elements which the method each yields into A. For any two elements Ei and Ej of A, all of the following conditions shall hold: 1) 2) Let i and j be the index of Ei and Ej , respectively. If the block is given: i) ii) Suppose the block is called with Ei and Ej as the arguments. If this invocation does not result in an instance of the class Integer, the behavior is implementation dependent.
3
4
5
6
7 8
9
10
11
12 13
14 15
iii) If this invocation results in an instance of the class Integer whose value is larger than 0, j shall be larger than i. iv) If this invocation results in an instance of the class Integer whose value is smaller than 0, i shall be larger than j. 3) If the block is not given: i) ii) Suppose the method <=> is invoked on Ei with Ej as the argument. If this invocation does not result in an instance of the class Integer, the behavior is implementation dependent.
16 17
18
19
20 21
22 23
iii) If this invocation results in an instance of the class Integer whose value is larger than 0, j shall be larger than i. iv) If this invocation results in an instance of the class Integer whose value is smaller than 0, i shall be larger than j. d) Return A. Enumerable#to a
24 25
26
27
15.3.2.1.20
28
to a
282
1
Visibility: public Behavior: Same as the method entries (see §15.3.2.1.6). 15.3.3 Comparable
2
3
4 5
The module Comparable provides methods which compare the receiver and an argument using the method <=>. 15.3.3.1 15.3.3.1.1 Instance methods Comparable#<
6
7
8
<( other ) Visibility: public Behavior: a) Invoke the method <=> on the receiver with the other as the argument. Let I be the resulting value of this invocation. If I is not an instance of the class Integer, the behavior is implementation dependent. If the value of I is smaller than 0, return true. Otherwise, return false. Comparable#<=
9
10
11 12
13
b) c)
14
15
15.3.3.1.2
16
< =( other ) Visibility: public Behavior: a) Invoke the method <=> on the receiver with the other as the argument. Let I be the resulting value of this invocation. If I is not an instance of the class Integer, the behavior is implementation dependent. If the value of I is smaller than or equal to 0, return true. Otherwise, return false. Comparable#==
17
18
19 20
21
b) c)
22
23
15.3.3.1.3
24
= =( other ) Visibility: public 283
25
1
Behavior: a) Invoke the method <=> on the receiver with the other as the argument. Let I be the resulting value of this invocation. If I is not an instance of the class Integer, the behavior is implementation dependent. If the value of I is 0, return true. Otherwise, return false. Comparable#>
2 3
4
b) c)
5
6
15.3.3.1.4
7
>( other ) Visibility: public Behavior: a) Invoke the method <=> on the receiver with the other as the argument. Let N be the resulting value of this invocation. If I is not an instance of the class Integer, the behavior is implementation dependent. If the value of I is larger than 0, return true. Otherwise, return false. Comparable#>=
8
9
10 11
12
b) c)
13
14
15.3.3.1.5
15
> =( other ) Visibility: public Behavior: a) Invoke the method <=> on the receiver with the other as the argument. Let N be the resulting value of this invocation. If I is not an instance of the class Integer, the behavior is implementation dependent. If the value of I is larger than or equal to 0, return true. Otherwise, return false. Comparable#between?
16
17
18 19
20
b) c)
21
22
15.3.3.1.6
23
between?( left, right ) Visibility: public Behavior: 284
24
25
1 2
a)
Invoke the method <=> on the receiver with the left as the argument. Let I1 be the resulting value of this invocation. 1) If I1 is not an instance of the class Integer, the behavior is implementation dependent. If the value of I1 is smaller than 0, return false.
3 4
5
2) b)
6 7
Invoke the method <=> on the receiver with the right as the argument. Let I2 be the resulting value of this invocation. 1) If I2 is not an instance of the class Integer, the behavior is implementation dependent. If the value of I2 is larger than 0, return false. Otherwise, return true.
8 9
10
2)
285
1 2
Annex A (informative) Grammar Summary
3
4
A.1 A.1.1
Lexical structure Source text
5
6
see §8.1 source-character :: [ any character in ISO/IEC 646 ]
7 8
9
A.1.2 see §8.2
Line terminators
10
11 12
line-terminator :: 0x0d? 0x0a separator :: ; | [ line-terminator here ] single-variable-assignment-expression :: variable [ no line-terminator here ] = operator-expression
13 14 15
16 17
18
A.1.3 see §8.3
Whitespace
19
20 21 22 23
whitespace :: 0x09 | 0x0b | 0x0c | 0x0d | 0x20 | \ 0x0d? 0x0a
24
A.1.4 see §8.4
Comments
25
286
1 2 3
comment :: single-line-comment | multi-line-comment single-line-comment :: # comment-content ? comment-content :: line-content line-content :: source-character + multi-line-comment :: multi-line-comment-begin-line multi-line-comment-line ? multi-line-comment-end-line multi-line-comment-begin-line :: [ beginning of a line ] =begin rest-of-begin-end-line ? line-terminator multi-line-comment-end-line :: [ beginning of a line ] =end rest-of-begin-end-line ? ( line-terminator | [ end of a program ] ) rest-of-begin-end-line :: whitespace + comment-content line :: comment-content line-terminator multi-line-comment-line :: line but not multi-line-comment-end-line
4 5
6 7
8 9
10 11 12
13 14
15 16 17
18 19
20 21
22 23
24
A.1.5 see §8.5
Tokens
25
26 27 28 29 30 31
token :: reserved-word | identifier | punctuator | operator | literal 287
1
A.1.5.1 see §8.5.1
Reserved words
2
3 4 5 6 7 8
reserved-word :: __LINE__ | __ENCODING__ | __FILE__ | BEGIN | END | alias | and | begin | break | case | class | def | defined? | do | else | elsif | end | ensure | for | false | if | in | module | next | nil | not | or | redo | rescue | retry | return | self | super | then | true | undef | unless | until | when | while | yield
9
A.1.5.2 see §8.5.2
Identifiers
10
11 12 13 14 15 16 17
identifier :: local-variable-identifier | global-variable-identifier | class-variable-identifier | instance-variable-identifier | constant-identifier | method-identifier local-variable-identifier :: ( lowercase-character | _ ) identifier-character * global-variable-identifier :: � identifier-start-character identifier-character * class-variable-identifier :: @@ identifier-start-character identifier-character * instance-variable-identifier :: @ identifier-start-character identifier-character * constant-identifier :: uppercase-character identifier-character * method-identifier :: method-only-identifier | assignment-like-method-identifier | constant-identifier | local-variable-identifier 288
18 19
20 21
22 23
24 25
26 27
28 29 30 31 32
1 2
method-only-identifier :: ( constant-identifier | local-variable-identifier ) ( ! | ? ) assignment-like-method-identifier :: ( constant-identifier | local-variable-identifier ) = identifier-character :: lowercase-character | uppercase-character | decimal-digit |_ identifier-start-character :: lowercase-character | uppercase-character |_ uppercase-character :: A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z lowercase-character :: a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z decimal-digit :: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
3 4
5 6 7 8 9
10 11 12 13
14 15 16
17 18 19
20 21
22
A.1.5.3 see §8.5.3
Punctuators
23
24 25
punctuator :: [ | ] | ( | ) | { | } | :: | , | ; | .. | ... | ? | : | =>
26
A.1.5.4 see §8.5.4
Operators
27
28 29 30
operator :: operator-method-name | assignment-operator
289
1 2 3
operator-method-name :: ^ | & | | | <=> | == | === | !~ | =~ | > | >= | < | <= | << | >> | + | - | * | / | % | ** | ~ | +@ | -@ | [] | []= | ‘ assignment-operator :: assignment-operator-name = assignment-operator-name :: + | - | * | ** | / | ^ | % | << | >> | & | && | || | |
4 5
6 7
8
A.1.5.5 see §8.5.5
Literals
9
10 11 12 13 14 15
literal :: numeric-literal | string-literal | array-literal | regular-expression-literal | symbol
16
A.1.5.5.1 see §8.5.5.1
Numeric literals
17
18 19 20
numeric-literal :: signed-number | unsigned-number unsigned-number :: integer-literal | float-literal integer-literal :: decimal-integer-literal | binary-integer-literal | octal-integer-literal | hexadecimal-integer-literal decimal-integer-literal :: digit-decimal-integer-literal | prefixed-decimal-integer-literal
21 22 23
24 25 26 27 28
29 30 31
290
1 2 3
digit-decimal-integer-literal :: 0 | decimal-digit-without-zero ( _? decimal-digit )* prefixed-decimal-integer-literal :: 0 ( d | D ) digit-decimal-part digit-decimal-part :: decimal-digit ( _? decimal-digit )* binary-integer-literal :: 0 ( b | B ) binary-digit ( _? binary-digit )* octal-integer-literal :: 0 ( _ | o | O )? octal-digit ( _? octal-digit )* hexadecimal-integer-literal :: 0 ( x | X ) hexadecimal-digit ( _? hexadecimal-digit )* float-literal :: decimal-float-literal | exponent-float-literal decimal-float-literal :: digit-decimal-integer-literal . digit-decimal-part exponent-float-literal :: base-part exponent-part base-part :: decimal-float-literal | digit-decimal-integer-literal exponent-part :: ( e | E ) ( + | - )? digit-decimal-part signed-number :: ( + | - ) unsigned-number decimal-digit-without-zero :: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 octal-digit :: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7
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
291
1 2
binary-digit :: 0 | 1 hexadecimal-digit :: decimal-digit | a | b | c | d | e | f | A | B | C | D | E | F
3 4
5
A.1.5.5.2 see §8.5.5.2
String literals
6
7 8 9 10 11 12 13
string-literal :: single-quoted-string | double-quoted-string | quoted-non-expanded-literal-string | quoted-expanded-literal-string | here-document | external-command-execution
14
A.1.5.5.2.1 see §8.5.5.2.1
Single quoted strings
15
16 17
single-quoted-string :: ’ single-quoted-string-character * ’ single-quoted-string-character :: non-escaped-single-quoted-string-character | single-quoted-escape-sequence single-quoted-escape-sequence :: single-escape-character-sequence | non-escaped-single-quoted-string-character-sequence single-escape-character-sequence :: \ single-escaped-character non-escaped-single-quoted-string-character-sequence :: \ non-escaped-single-quoted-string-character single-escaped-character :: ’ |\ non-escaped-single-quoted-string-character :: source-character but not single-escaped-character 292
18 19 20
21 22 23
24 25
26 27
28 29
30 31
1
A.1.5.5.2.2 see §8.5.5.2.2
Double quoted strings
2
3 4
double-quoted-string :: " double-quoted-string-character * " double-quoted-string-character :: source-character but not ( " | \ ) | double-escape-sequence | interpolated-character-sequence double-escape-sequence :: simple-escape-sequence | non-escaped-sequence | line-terminator-escape-sequence | octal-escape-sequence | hex-escape-sequence | control-escape-sequence simple-escape-sequence :: \ double-escaped-character non-escaped-sequence :: \ non-escaped-double-quoted-string-character line-terminator-escape-sequence :: \ line-terminator non-escaped-double-quoted-string-character :: source-character but not ( double-escaped-character | line-terminator ) double-escaped-character :: \ | n | t | r | f | v | a | e | b | s octal-escape-sequence :: \ octal-digit ( octal-digit octal-digit ? )? hex-escape-sequence :: \ x hexadecimal-digit hexadecimal-digit ? control-escape-sequence :: \ ( C - | c ) control-escaped-character 293
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
1 2 3 4
control-escaped-character :: double-escape-sequence | ? | source-character but not ( \ | ? ) interpolated-character-sequence :: # global-variable-identifier | # class-variable-identifier | # instance-variable-identifier | # { compound-statement }
5 6 7 8 9
10
A.1.5.5.2.3 see §8.5.5.2.3
Quoted non-expanded literal strings
11
12 13
quoted-non-expanded-literal-string :: %q literal-beginning-delimiter non-expanded-literal-string * literal-ending-delimiter non-expanded-literal-string :: non-expanded-literal-character | non-expanded-delimited-string non-expanded-delimited-string :: literal-beginning-delimiter non-expanded-literal-string * literal-ending-delimiter non-expanded-literal-character :: non-escaped-literal-character | non-expanded-literal-escape-sequence non-escaped-literal-character :: source-character but not quoted-literal-escape-character non-expanded-literal-escape-sequence :: non-expanded-literal-escape-character-sequence | non-escaped-non-expanded-literal-character-sequence non-expanded-literal-escape-character-sequence :: \ non-expanded-literal-escaped-character non-expanded-literal-escaped-character :: literal-beginning-delimiter | literal-ending-delimiter | \
14 15 16
17 18
19 20 21
22 23
24 25 26
27 28
29 30 31 32
294
1 2
quoted-literal-escape-character :: non-expanded-literal-escaped-character non-escaped-non-expanded-literal-character-sequence :: \ non-escaped-non-expanded-literal-character non-escaped-non-expanded-literal-character :: source-character but not non-expanded-literal-escaped-character
3 4
5 6
7
A.1.5.5.2.4 see §8.5.5.2.4
Quoted expanded literal strings
8
9 10
quoted-expanded-literal-string :: % Q? literal-beginning-delimiter expanded-literal-string * literal-ending-delimiter expanded-literal-string :: expanded-literal-character | expanded-delimited-string expanded-literal-character :: non-escaped-literal-character | double-escape-sequence | interpolated-character-sequence expanded-delimited-string :: literal-beginning-delimiter expanded-literal-string * literal-ending-delimiter literal-beginning-delimiter :: source-character but not alpha-numeric-character-or-separator alpha-numeric-character-or-separator :: whitespace | line-terminator | uppercase-character | lowercase-character | decimal-digit literal-ending-delimiter :: [ depending on the literal-beginning-delimiter ] matching-literal-beginning-delimiter :: ( | { | < | [
11 12 13
14 15 16 17
18 19
20 21
22 23 24 25 26 27
28 29
30 31
295
1
A.1.5.5.2.5 see §8.5.5.2.5
Here documents
2
3 4
here-document :: heredoc-start-line heredoc-body heredoc-end-line heredoc-start-line :: heredoc-signifier rest-of-line heredoc-signifier :: << heredoc-delimiter-specifier rest-of-line :: line-content ? line-terminator heredoc-body :: heredoc-body-line * heredoc-body-line :: line but not heredoc-end-line heredoc-delimiter-specifier :: -? heredoc-delimiter heredoc-delimiter :: non-quoted-delimiter | single-quoted-delimiter | double-quoted-delimiter | command-quoted-delimiter non-quoted-delimiter :: non-quoted-delimiter-identifier non-quoted-delimiter-identifier :: identifier-character * single-quoted-delimiter :: ’ single-quoted-delimiter-identifier * ’ single-quoted-delimiter-identifier :: source-character but not ’ double-quoted-delimiter :: " double-quoted-delimiter-identifier * " 296
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
1 2
double-quoted-delimiter-identifier :: source-character but not " command-quoted-delimiter :: ‘ command-quoted-delimiter-identifier * ‘ command-quoted-delimiter-identifier :: source-character but not ‘ heredoc-end-line :: indented-heredoc-end-line | non-indented-heredoc-end-line indented-heredoc-end-line :: [ beginning of a line ] whitespace * heredoc-delimiter-identifier line-terminator non-indented-heredoc-end-line :: [ beginning of a line ] heredoc-delimiter-identifier line-terminator heredoc-delimiter-identifier :: non-quoted-delimiter-identifier | single-quoted-delimiter-identifier | double-quoted-delimiter-identifier | command-quoted-delimiter-identifier
3 4
5 6
7 8 9
10 11
12 13
14 15 16 17 18
19
A.1.5.5.2.6 see §8.5.5.2.6
External command execution
20
21 22 23
external-command-execution :: backquoted-external-command-execution | quoted-external-command-execution backquoted-external-command-execution :: ‘ double-quoted-string-character * ‘ quoted-external-command-execution :: %x literal-beginning-delimiter expanded-literal-string * literal-ending-delimiter
24 25
26 27
28
A.1.5.5.3 see §8.5.5.3
Array literals
29
297
1 2 3
array-literal :: quoted-non-expanded-array-constructor | quoted-expanded-array-constructor quoted-non-expanded-array-constructor :: %w literal-beginning-delimiter non-expanded-array-content literal-ending-delimiter non-expanded-array-content :: quoted-array-item-separator-list ? non-expanded-array-item-list ? quoted-array-item-separator-list ? non-expanded-array-item-list :: non-expanded-array-item ( quoted-array-item-separator-list non-expanded-array-item )* quoted-array-item-separator-list :: quoted-array-item-separator + quoted-array-item-separator :: whitespace | line-terminator non-expanded-array-item :: non-expanded-array-item-character + non-expanded-array-item-character :: non-escaped-array-item-character | non-expanded-array-escape-sequence non-escaped-array-item-character :: non-escaped-array-character | matching-literal-delimiter non-escaped-array-character :: non-escaped-literal-character but not quoted-array-item-separator matching-literal-delimiter :: ( | { | < | [ | ) | } | > | ] non-expanded-array-escape-sequence :: non-expanded-literal-escape-sequence but not escaped-quoted-array-item-separator | escaped-quoted-array-item-separator escaped-quoted-array-item-separator :: \ quoted-array-item-separator 298
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
1 2
quoted-expanded-array-constructor :: %W literal-beginning-delimiter expanded-array-content literal-ending-delimiter expanded-array-content :: quoted-array-item-separator-list ? expanded-array-item-list ? quoted-array-item-separator-list ? expanded-array-item-list :: expanded-array-item ( quoted-array-item-separator-list expanded-array-item )* expanded-array-item :: expanded-array-item-character + expanded-array-item-character :: non-escaped-array-item-character | expanded-array-escape-sequence | interpolated-character-sequence expanded-array-escape-sequence :: double-escape-sequence but not escaped-quoted-array-item-separator | escaped-quoted-array-item-separator
3 4 5
6 7
8 9
10 11 12 13
14 15 16
17
A.1.5.5.4 see §8.5.5.4
Regular expression literals
18
19 20 21 22
regular-expression-literal :: / regular-expression-body / regular-expression-option * | %r literal-beginning-delimiter expanded-literal-string * literal-ending-delimiter regular-expression-option * regular-expression-body :: regular-expression-character * regular-expression-character :: source-character but not ( / | \ ) | \\ | line-terminator-escape-sequence | interpolated-character-sequence regular-expression-option :: i | m
23 24
25 26 27 28 29
30 31
299
1
A.2 A.2.1
Program structure Program
2
3
see §10.1 program :: compound-statement
4 5
6
A.3 A.3.1
Expressions Logical expressions
7
8
see §11.1 keyword-logical-expression :: keyword-NOT-expression | keyword-AND-expression | keyword-OR-expression keyword-NOT-expression :: method-invocation-without-parentheses | operator-expression | logical-NOT-with-method-invocation-without-parentheses | not keyword-NOT-expression logical-NOT-expression ::= logical-NOT-with-method-invocation-without-parentheses | logical-NOT-with-unary-expression logical-NOT-with-method-invocation-without-parentheses :: ! method-invocation-without-parentheses logical-NOT-with-unary-expression :: ! unary-expression keyword-AND-expression :: expression and keyword-NOT-expression keyword-OR-expression :: expression or keyword-NOT-expression logical-OR-expression :: logical-AND-expression | logical-OR-expression || logical-AND-expression 300
9 10 11 12
13 14 15 16 17
18 19 20
21 22
23 24
25 26
27 28
29 30 31
1 2 3
logical-AND-expression :: equality-expression | logical-AND-expression && equality-expression
4
A.3.2
Method invocation expressions
5
see §11.2 primary-method-invocation :: super-with-optional-argument | indexing-method-invocation | method-only-identifier | method-identifier ( [ no whitespace here ] argument-with-parentheses )? block ? | primary-expression [ no line-terminator here ] . method-name ( [ no whitespace here ] argument-with-parentheses )? block ? | primary-expression [ no line-terminator here ] :: method-name [ no whitespace here ] argument-with-parentheses block ? | primary-expression [ no line-terminator here ] :: method-name-without-constant block ? indexing-method-invocation :: primary-expression [ no line-terminator here ] optional-whitespace ? [ indexing-argument-list ? ] optional-whitespace :: [ whitespace here ] method-name-without-constant :: method-name but not constant-identifier method-invocation-without-parentheses :: command | chained-command-with-do-block | chained-command-with-do-block ( . | :: ) method-name argument | return-with-argument | break-with-argument | next-with-argument command :: super-with-argument | yield-with-argument | method-identifier argument | primary-expression [ no line-terminator here ] ( . | :: ) method-name argument chained-command-with-do-block :: command-with-do-block chained-method-invocation * 301
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
1 2 3 4
chained-method-invocation :: ( . | :: ) method-name | ( . | :: ) method-name [ no whitespace here ] [ lookahead ∈ { { } ] argument-with-parentheses / command-with-do-block :: super-with-argument-and-do-block | method-identifier argument do-block | primary-expression [ no line-terminator here ] ( . | :: ) method-name argument do-block
5 6 7 8 9
10
A.3.2.1 see §11.2.1
Method arguments
11
12 13 14 15 16 17
indexing-argument-list :: command | operator-expression-list ,? | operator-expression-list , splatting-argument | association-list ,? | splatting-argument splatting-argument :: * operator-expression operator-expression-list :: operator-expression ( , operator-expression )* argument-with-parentheses :: () | ( argument-in-parentheses ) | ( operator-expression-list , chained-command-with-do-block ) | ( chained-command-with-do-block ) argument :: [ no line-terminator here ] [ lookahead ∈ { { } ] optional-whitespace ? / argument-in-parentheses argument-in-parentheses :: command | ( operator-expression-list | association-list ) ( , splatting-argument )? ( , block-argument )? | operator-expression-list , association-list ( , splatting-argument )? ( , block-argument )? | splatting-argument ( , block-argument )? | block-argument 302
18 19
20 21
22 23 24 25 26
27 28 29
30 31 32 33 34 35 36 37
1 2
block-argument :: & operator-expression
3
A.3.2.2 see §11.2.2
Blocks
4
5 6 7
block :: brace-block | do-block brace-block :: { block-formal-argument ? block-body } do-block :: do block-formal-argument ? block-body end block-formal-argument :: || | || | | block-formal-argument-list | block-formal-argument-list :: left-hand-side | multiple-left-hand-side block-body :: compound-statement
8 9
10 11
12 13 14 15
16 17 18
19 20
21
A.3.2.3 see §11.2.3
The super expression
22
23 24 25 26
super-expression ::= super-with-optional-argument | super-with-argument | super-with-argument-and-do-block super-with-optional-argument :: super ( [ no whitespace here ] argument-with-parentheses )? block ? super-with-argument :: super argument 303
27 28
29 30
1 2
super-with-argument-and-do-block :: super argument do-block
3
A.3.2.4 see §11.2.4
The yield expression
4
5 6 7
yield-expression ::= yield-with-optional-argument | yield-with-argument yield-with-optional-argument :: yield-with-parentheses-and-argument | yield-with-parentheses-without-argument | yield yield-with-parentheses-and-argument :: yield [ no whitespace here ] ( argument-in-parentheses ) yield-with-parentheses-without-argument :: yield [ no whitespace here ] ( ) yield-with-argument :: yield argument
8 9 10 11
12 13
14 15
16 17
18
A.3.3
Operator expressions
19
see §11.3 operator-expression :: assignment-expression | defined?-without-parentheses | conditional-operator-expression
20 21 22 23
24
A.3.3.1 see §11.3.1
Assignments
25
26 27 28
assignment ::= assignment-expression | assignment-statement
304
1 2 3 4
assignment-expression :: single-assignment-expression | abbreviated-assignment-expression | assignment-with-rescue-modifier assignment-statement :: single-assignment-statement | abbreviated-assignment-statement | multiple-assignment-statement
5 6 7 8
9
A.3.3.1.1 see §11.3.1.1
Single assignments
10
11 12 13
single-assignment ::= single-assignment-expression | single-assignment-statement single-assignment-expression :: single-variable-assignment-expression | scoped-constant-assignment-expression | single-indexing-assignment-expression | single-method-assignment-expression single-assignment-statement :: single-variable-assignment-statement | scoped-constant-assignment-statement | single-indexing-assignment-statement | single-method-assignment-statement
14 15 16 17 18
19 20 21 22 23
24
A.3.3.1.1.1 see §11.3.1.1.1
Single variable assignments
25
26 27 28
single-variable-assignment ::= single-variable-assignment-expression | single-variable-assignment-statement single-variable-assignment-expression :: variable [ no line-terminator here ] = operator-expression single-variable-assignment-statement :: variable [ no line-terminator here ] = method-invocation-without-parentheses
29 30
31 32
305
1 2 3
scoped-constant-assignment ::= scoped-constant-assignment-expression | scoped-constant-assignment-statement scoped-constant-assignment-expression :: primary-expression [ no whitespace here ] :: constant-identifier [ no line-terminator here ] = operator-expression | :: constant-identifier [ no line-terminator here ] = operator-expression scoped-constant-assignment-statement :: primary-expression [ no whitespace here ] :: constant-identifier [ no line-terminator here ] = method-invocation-without-parentheses | :: constant-identifier [ no line-terminator here ] = method-invocation-without-parentheses
4 5 6 7
8 9 10 11
12
A.3.3.1.1.2 see §11.3.1.1.2
Single indexing assignments
13
14 15 16
single-indexing-assignment ::= single-indexing-assignment-expression | single-indexing-assignment-statement single-indexing-assignment-expression :: primary-expression [ no line-terminator here ] [ indexing-argument-list ? ] [ no line-terminator here ] = operator-expression single-indexing-assignment-statement :: primary-expression [ no line-terminator here ] [ indexing-argument-list ? ] [ no line-terminator here ] = method-invocation-without-parentheses
17 18 19
20 21 22
23
A.3.3.1.1.3 see §11.3.1.1.3
Single method assignments
24
25 26 27
single-method-assignment ::= single-method-assignment-expression | single-method-assignment-statement single-method-assignment-expression :: primary-expression [ no line-terminator here ] ( . | :: ) local-variable-identifier [ no line-terminator here ] = operator-expression | primary-expression [ no line-terminator here ] . constant-identifier [ no line-terminator here ] = operator-expression
28 29 30 31 32
306
1 2 3 4 5
single-method-assignment-statement :: primary-expression [ no line-terminator here ] ( . | :: ) local-variable-identifier [ no line-terminator here ] = method-invocation-without-parentheses | primary-expression [ no line-terminator here ] . constant-identifier [ no line-terminator here ] = method-invocation-without-parentheses
6
A.3.3.1.2 see §11.3.1.2
Abbreviated assignments
7
8 9 10
abbreviated-assignment ::= abbreviated-assignment-expression | abbreviated-assignment-statement abbreviated-assignment-expression :: abbreviated-variable-assignment-expression | abbreviated-indexing-assignment-expression | abbreviated-method-assignment-expression abbreviated-assignment-statement :: abbreviated-variable-assignment-statement | abbreviated-indexing-assignment-statement | abbreviated-method-assignment-statement
11 12 13 14
15 16 17 18
19
A.3.3.1.2.1 see §11.3.1.2.1
Abbreviated variable assignments
20
21 22 23
abbreviated-variable-assignment ::= abbreviated-variable-assignment-expression | abbreviated-variable-assignment-statement abbreviated-variable-assignment-expression :: variable [ no line-terminator here ] assignment-operator operator-expression abbreviated-variable-assignment-statement :: variable [ no line-terminator here ] assignment-operator method-invocation-without-parentheses
24 25
26 27 28
29
A.3.3.1.2.2 see §11.3.1.2.2
Abbreviated indexing assignments
30
307
1 2 3
abbreviated-indexing-assignment ::= abbreviated-indexing-assignment-expression | abbreviated-indexing-assignment-statement abbreviated-indexing-assignment-expression :: primary-expression [ no line-terminator here ] [ indexing-argument-list ? ] [ no line-terminator here ] assignment-operator operator-expression abbreviated-indexing-assignment-statement :: primary-expression [ no line-terminator here ] [ indexing-argument-list ? ] [ no line-terminator here ] assignment-operator method-invocation-without-parentheses
4 5 6
7 8 9
10
A.3.3.1.2.3 see §11.3.1.2.3
Abbreviated method assignments
11
12 13 14
abbreviated-method-assignment ::= abbreviated-method-assignment-expression | abbreviated-method-assignment-statement abbreviated-method-assignment-expression :: primary-expression [ no line-terminator here ] ( . | :: ) local-variable-identifier [ no line-terminator here ] assignment-operator operator-expression | primary-expression [ no line-terminator here ] . constant-identifier [ no line-terminator here ] assignment-operator operator-expression abbreviated-method-assignment-statement :: primary-expression [ no line-terminator here ] ( . | :: ) local-variable-identifier [ no line-terminator here ] assignment-operator method-invocation-without-parentheses | primary-expression [ no line-terminator here ] . constant-identifier [ no line-terminator here ] assignment-operator method-invocation-without-parentheses
15 16 17 18 19
20 21 22 23 24
25
A.3.3.1.3 see §11.3.1.3
Multiple assignments
26
27 28 29 30
multiple-assignment-statement :: many-to-one-assignment-statement | one-to-packing-assignment-statement | many-to-many-assignment-statement many-to-one-assignment-statement :: left-hand-side [ no line-terminator here ] = multiple-right-hand-side 308
31 32
1 2 3
one-to-packing-assignment-statement :: packing-left-hand-side [ no line-terminator here ] = ( method-invocation-without-parentheses | operator-expression ) many-to-many-assignment-statement :: multiple-left-hand-side [ no line-terminator here ] = multiple-right-hand-side | ( multiple-left-hand-side but not packing-left-hand-side ) [ no line-terminator here ] = ( method-invocation-without-parentheses | operator-expression ) left-hand-side :: variable | primary-expression [ no line-terminator here ] [ indexing-argument-list ? ] | primary-expression [ no line-terminator here ] ( . | :: ) ( local-variable-identifier | constant-identifier ) | :: constant-identifier multiple-left-hand-side :: ( multiple-left-hand-side-item , )+ multiple-left-hand-side-item ? | ( multiple-left-hand-side-item , )+ packing-left-hand-side ? | packing-left-hand-side | grouped-left-hand-side packing-left-hand-side :: * left-hand-side ? grouped-left-hand-side :: ( multiple-left-hand-side ) multiple-left-hand-side-item :: left-hand-side | grouped-left-hand-side multiple-right-hand-side :: operator-expression-list ( , splatting-right-hand-side )? | splatting-right-hand-side splatting-right-hand-side :: splatting-argument
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
A.3.3.1.4 see §11.3.1.4
Assignments with rescue modifiers
33
309
1 2 3
assignment-with-rescue-modifier :: left-hand-side [ no line-terminator here ] = operator-expression 1 rescue operator-expression 2
4
A.3.3.2 see §11.3.2
Unary operators
5
6 7 8
unary-minus-expression :: power-expression 1 | - power-expression 2 unary-expression :: primary-expression | logical-NOT-with-unary-expression | ~ unary-expression 1 | + unary-expression 2
9 10 11 12 13
14
A.3.3.2.1 see §11.3.2.1
The defined? expression
15
16 17 18
defined?-expression ::= defined?-with-parentheses | defined?-without-parentheses defined?-with-parentheses :: defined? ( expression ) defined?-without-parentheses :: defined? operator-expression
19 20
21 22
23
A.3.3.3 see §11.3.3
Binary operators
24
25 26 27 28 29 30 31 32
equality-expression :: relational-expression | relational-expression | relational-expression | relational-expression | relational-expression | relational-expression | relational-expression 310
<=> relational-expression == relational-expression === relational-expression != relational-expression =~ relational-expression !~ relational-expression
1 2 3 4 5 6
relational-expression :: bitwise-OR-expression | relational-expression > bitwise-OR-expression | relational-expression >= bitwise-OR-expression | relational-expression < bitwise-OR-expression | relational-expression <= bitwise-OR-expression bitwise-OR-expression :: bitwise-AND-expression | bitwise-OR-expression | bitwise-AND-expression | bitwise-OR-expression ^ bitwise-AND-expression bitwise-AND-expression :: bitwise-shift-expression | bitwise-AND-expression whitespace-before-operator ? & bitwise-shift-expression bitwise-shift-expression :: additive-expression | bitwise-shift-expression whitespace-before-operator ? << additive-expression | bitwise-shift-expression >> additive-expression additive-expression :: multiplicative-expression | additive-expression whitespace-before-operator ? + multiplicative-expression | additive-expression whitespace-before-operator ? - multiplicative-expression multiplicative-expression :: unary-minus-expression | multiplicative-expression whitespace-before-operator ? * unary-minus-expression | multiplicative-expression whitespace-before-operator ? / unary-minus-expression | multiplicative-expression whitespace-before-operator ? % unary-minus-expression power-expression :: unary-expression | - ( numeric-literal ) ** power-expression | unary-expression ** power-expression binary-operator ::= <=> | == | === | =~ | > | >= | < | <= | | | ^ | & | << | >> | + | - | * | / | % | **
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
A.3.4
Primary expressions
35
see §11.4
311
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
primary-expression :: class-definition | eigenclass-definition | module-definition | method-definition | singleton-method-definition | yield-with-optional-argument | if-expression | unless-expression | case-expression | while-expression | until-expression | for-expression | return-without-argument | break-without-argument | next-without-argument | redo-expression | retry-expression | rescue-expression | grouping-expression | variable-reference | scoped-constant-reference | array-constructor | hash-constructor | literal | defined?-with-parentheses | primary-method-invocation
28
A.3.4.0.0.1 see §11.4.1.1.1
The if expression
29
30 31
if-expression :: if expression then-clause elsif-clause * else-clause ? end then-clause :: separator compound-statement | separator ? then compound-statement else-clause :: else compound-statement elsif-clause :: elsif expression then-clause
32 33 34
35 36
37 38
312
1
A.3.4.0.0.2 see §11.4.1.1.2
The unless expression
2
3 4
unless-expression :: unless expression then-clause else-clause ? end
5
A.3.4.0.0.3 see §11.4.1.1.3
The case expression
6
7 8 9
case-expression :: case-expression-with-expression | case-expression-without-expression case-expression-with-expression :: case expression separator-list ? when-clause + else-clause ? end case-expression-without-expression :: case separator-list ? when-clause + else-clause ? end when-clause :: when when-argument then-clause when-argument :: operator-expression-list ( , splatting-argument )? | splatting-argument
10 11
12 13
14 15
16 17 18
19
A.3.4.0.0.4 see §11.4.1.1.4
Conditional operator
20
21 22 23
conditional-operator-expression :: range-constructor | range-constructor ? operator-expression 1 : operator-expression 2
24
A.3.4.0.1 see §11.4.1.2
Iteration expressions
25
26 27
iteration-expression ::= while-expression 313
1 2 3 4
| | | |
until-expression for-expression while-modifier-statement until-modifier-statement
5
A.3.4.0.1.1 see §11.4.1.2.1
The while expression
6
7 8
while-expression :: while expression do-clause end do-clause :: separator compound-statement | do compound-statement
9 10 11
12
A.3.4.0.1.2 see §11.4.1.2.2
The until expression
13
14 15
until-expression :: until expression do-clause end
16
A.3.4.0.1.3 see §11.4.1.2.3
The for expression
17
18 19
for-expression :: for for-variable in expression do-clause end for-variable :: left-hand-side | multiple-left-hand-side
20 21 22
23
A.3.4.0.2 see §11.4.1.3
Jump expressions
24
25 26 27 28
jump-expression ::= return-expression | break-expression | next-expression 314
1 2
| redo-expression | retry-expression
3
A.3.4.0.2.1 see §11.4.1.3.1
The return expression
4
5 6 7
return-expression ::= return-without-argument | return-with-argument return-without-argument :: return return-with-argument :: return jump-argument jump-argument :: argument
8 9
10 11
12 13
14
A.3.4.0.2.2 see §11.4.1.3.2
The break expression
15
16 17 18
break-expression ::= break-without-argument | break-with-argument break-without-argument :: break break-with-argument :: break jump-argument
19 20
21 22
23
A.3.4.0.2.3 see §11.4.1.3.3
The next expression
24
25 26 27
next-expression ::= next-without-argument | next-with-argument
315
1 2
next-without-argument :: next next-with-argument :: next jump-argument
3 4
5
A.3.4.0.2.4 see §11.4.1.3.4
The redo expression
6
7 8
redo-expression :: redo
9
A.3.4.0.2.5 see §11.4.1.3.5
The retry expression
10
11 12
retry-expression :: retry
13
A.3.4.0.2.6 see §11.4.1.4.1
The rescue expression
14
15 16
rescue-expression :: begin body-statement end body-statement :: compound-statement rescue-clause * else-clause ? ensure-clause ? rescue-clause :: rescue [ no line-terminator here ] exception-class-list ? exception-variable-assignment ? then-clause exception-class-list :: operator-expression | multiple-right-hand-side exception-variable-assignment :: => left-hand-side
17 18
19 20 21
22 23 24
25 26
316
1 2
ensure-clause :: ensure compound-statement
3
A.3.4.1 see §11.4.2
Grouping expression
4
5 6 7
grouping-expression :: ( expression ) | ( compound-statement )
8
A.3.4.2 see §11.4.3
Variable references
9
10 11 12
variable-reference :: variable | pseudo-variable variable :: constant-identifier | global-variable-identifier | class-variable-identifier | instance-variable-identifier | local-variable-identifier scoped-constant-reference :: primary-expression [ no whitespace here ] :: constant-identifier | :: constant-identifier
13 14 15 16 17 18
19 20 21
22
A.3.4.2.1 see §11.4.3.7
Pseudo variables
23
24 25 26 27 28
pseudo-variable :: nil | true | false | self
29
A.3.4.2.1.1 see §11.4.3.7.1
nil
30
317
1 2
nil :: nil
3
A.3.4.2.1.2 see §11.4.3.7.2 true :: true false :: false
true and false
4
5 6
7 8
9
A.3.4.2.1.3 see §11.4.3.7.3 self :: self
self
10
11 12
13
A.3.4.2.2 see §11.4.4.1
Array constructor
14
15 16
array-constructor :: [ indexing-argument-list ? ]
17
A.3.4.2.3 see §11.4.4.2
Hash constructor
18
19 20
hash-constructor :: { ( association-list ,? )? } association-list :: association ( , association )* association :: association-key => association-value 318
21 22
23 24
1 2
association-key :: operator-expression association-value :: operator-expression
3 4
5
A.3.4.2.4 see §11.4.4.3
Range constructor
6
7 8 9
range-constructor :: logical-OR-expression | logical-OR-expression 1 range-operator logical-OR-expression 2 range-operator :: .. | ...
10 11 12
13
A.4 A.4.1
Statements The expression statement
14
15
see §12.1 expression-statement :: expression
16 17
18
A.4.2
The if modifier statement
19
see §12.2 if-modifier-statement :: statement [ no line-terminator here ] if expression
20 21
22
A.4.3
The unless modifier statement
23
see §12.3 unless-modifier-statement :: statement [ no line-terminator here ] unless expression
24 25
319
1
A.4.4
The while modifier statement
2
see §12.4 while-modifier-statement :: statement [ no line-terminator here ] while expression
3 4
5
A.4.5
The until modifier statement
6
see §12.5 until-modifier-statement :: statement [ no line-terminator here ] until expression
7 8
9
A.5
Classes and modules Module definition
10
A.5.0.1 see §13.1.2
11
12 13
module-definition :: module module-path module-body end module-path :: top-module-path | module-name | nested-module-path module-name :: constant-identifier top-module-path :: :: module-name nested-module-path :: primary-expression [ no line-terminator here ] :: module-name module-body :: body-statement
14 15 16 17
18 19
20 21
22 23
24 25
26
A.5.0.2 see §13.2.2 320
Class definition
27
1 2
class-definition :: class class-path [ no line-terminator here ] superclass class-body end class-path :: top-class-path | class-name | nested-class-path class-name :: constant-identifier top-class-path :: :: class-name nested-class-path :: primary-expression [ no line-terminator here ] :: class-name superclass :: separator | < expression separator class-body :: body-statement
3 4 5 6
7 8
9 10
11 12
13 14 15
16 17
18
A.5.0.3 see §13.3.1
Method definition
19
20 21 22
method-definition :: def method-name [ no line-terminator here ] method-parameter-part method-body end method-name :: method-identifier | operator-method-name | reserved-word method-body :: body-statement
23 24 25 26
27 28
321
1
A.5.0.4 see §13.3.2
Method parameters
2
3 4 5
method-parameter-part :: ( parameter-list ? ) | parameter-list ? separator parameter-list :: mandatory-parameter-list , optional-parameter-list ? , array-parameter ? , block-parameter ? | optional-parameter-list , array-parameter ? , block-parameter ? | array-parameter , block-parameter ? | block-parameter mandatory-parameter-list :: mandatory-parameter | mandatory-parameter-list , mandatory-parameter mandatory-parameter :: local-variable-identifier optional-parameter-list :: optional-parameter | optional-parameter-list , optional-parameter optional-parameter :: optional-parameter-name = default-parameter-expression optional-parameter-name :: local-variable-identifier default-parameter-expression :: operator-expression array-parameter :: * array-parameter-name |* array-parameter-name :: local-variable-identifier block-parameter :: & block-parameter-name
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
322
1 2
block-parameter-name :: local-variable-identifier
3
A.5.0.5 see §13.3.6
The alias statement
4
5 6
alias-statement :: alias new-name aliased-name new-name :: method-name | symbol aliased-name :: method-name | symbol
7 8 9
10 11 12
13
A.5.0.6 see §13.3.7
The undef statement
14
15 16
undef-statement :: undef undef-list undef-list :: method-name-or-symbol ( , method-name-or-symbol )* method-name-or-symbol :: method-name | symbol
17 18
19 20 21
22
A.5.0.7 see §13.4.2
Eigenclass definition
23
24 25
eigenclass-definition :: class << expression separator eigenclass-body end eigenclass-body :: body-statement
26 27
323
1
A.5.0.7.1
Patterns
2
see §15.2.15.3 pattern :: alternative 1 | pattern 1 | alternative 2 alternative :: [ empty ] | alternative 3 term term :: anchor | atom 1 | atom 2 quantifier anchor :: ˆ| � quantifier :: * | + | ? atom :: pattern-character | grouping | . | atom-escape-sequence pattern-character :: source-character but not regexp-meta-character regexp-meta-character :: | | . | * | + | ˆ| ? | ( | ) | future-reserved-meta-character future-reserved-meta-character :: [ | ] | { | } grouping :: ( pattern ) atom-escape-sequence :: decimal-escape-sequence | regexp-character-escape-sequence
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
324
1 2
decimal-escape-sequence :: \ decimal-digit-without-zero regexp-character-escape-sequence :: regexp-escape-sequence | regexp-non-escaped-sequence | hex-escape-sequence | regexp-octal-escape-sequence | regexp-control-escape-sequence regexp-escape-sequence :: \ regexp-escaped-character regexp-escaped-character :: n | t | r | f | v | a | e | b regexp-non-escaped-sequence :: \ regexp-non-escaped-character regexp-non-escaped-character :: source-character but not regexp-escaped-character regexp-octal-escape-sequence :: octal-escape-sequence but not decimal-escape-sequence regexp-control-escape-sequence :: \ ( C - | c ) regexp-control-escaped-character regexp-control-escaped-character :: regexp-character-escape-sequence | ? | source-character but not ( \ | ? )
3 4 5 6 7 8
9 10
11 12
13 14
15 16
17 18
19 20
21 22 23 24
325