Class: Melbourne::AST::FormalArguments
- Defined in:
- lib/melbourne/ast/definitions.rb
Overview
The formal arguments of a method definition as in:
def method(x) # x is a formal argument of the method
end
Instance Attribute Summary collapse
-
#block_arg ⇒ Object
The block argument if there is one.
-
#defaults ⇒ Object
The arguments of the method that have default values.
-
#names ⇒ Object
The names of the arguments.
-
#optional ⇒ Object
The names of the optional arguments.
-
#required ⇒ Object
The names of the required arguments.
-
#splat ⇒ Object
The splat (+*some+) arguments of the method.
Attributes inherited from Node
Instance Method Summary collapse
-
#arity ⇒ Object
Gets the arity of the method.
-
#initialize(line, args, defaults, splat) ⇒ FormalArguments
constructor
A new instance of FormalArguments.
-
#required_args ⇒ Object
Gets the number of required arguments the method defines.
-
#splat_index ⇒ Object
TODO: document!.
-
#total_args ⇒ Object
Gets the total number of all arguments the method defines.
Methods inherited from Node
Constructor Details
#initialize(line, args, defaults, splat) ⇒ FormalArguments
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/melbourne/ast/definitions.rb', line 178 def initialize(line, args, defaults, splat) @line = line @defaults = nil @block_arg = nil if defaults defaults = DefaultArguments.new line, defaults @defaults = defaults @optional = defaults.names stop = defaults.names.first last = args.each_with_index { |a, i| break i if a == stop } @required = args[0, last] else @required = args.dup @optional = [] end if splat.kind_of? Symbol args << splat elsif splat splat = :@unnamed_splat args << splat end @names = args @splat = splat end |
Instance Attribute Details
#block_arg ⇒ Object
The block argument if there is one
176 177 178 |
# File 'lib/melbourne/ast/definitions.rb', line 176 def block_arg @block_arg end |
#defaults ⇒ Object
The arguments of the method that have default values
168 169 170 |
# File 'lib/melbourne/ast/definitions.rb', line 168 def defaults @defaults end |
#names ⇒ Object
The names of the arguments
156 157 158 |
# File 'lib/melbourne/ast/definitions.rb', line 156 def names @names end |
#optional ⇒ Object
The names of the optional arguments
164 165 166 |
# File 'lib/melbourne/ast/definitions.rb', line 164 def optional @optional end |
#required ⇒ Object
The names of the required arguments
160 161 162 |
# File 'lib/melbourne/ast/definitions.rb', line 160 def required @required end |
#splat ⇒ Object
The splat (+*some+) arguments of the method
172 173 174 |
# File 'lib/melbourne/ast/definitions.rb', line 172 def splat @splat end |
Instance Method Details
#arity ⇒ Object
Gets the arity of the method. The arity is the number of required arguments the method defines.
Example
def method(a, b = 1) # arity is 1 as the second argument is optional
end
218 219 220 |
# File 'lib/melbourne/ast/definitions.rb', line 218 def arity @required.size end |
#required_args ⇒ Object
Gets the number of required arguments the method defines.
224 225 226 |
# File 'lib/melbourne/ast/definitions.rb', line 224 def required_args @required.size end |
#splat_index ⇒ Object
TODO: document!
235 236 237 238 239 240 241 242 |
# File 'lib/melbourne/ast/definitions.rb', line 235 def splat_index if @splat index = @names.size index -= 1 if @block_arg index -= 1 if @splat.kind_of? Symbol index end end |
#total_args ⇒ Object
Gets the total number of all arguments the method defines.
230 231 232 |
# File 'lib/melbourne/ast/definitions.rb', line 230 def total_args @required.size + @optional.size end |