Project

General

Profile

Actions

Misc #8905

closed

Add documentation to semantics of method default arguments

Misc #8905: Add documentation to semantics of method default arguments

Added by prijutme4ty (Ilya Vorontsov) about 12 years ago. Updated over 6 years ago.

Status:
Closed
Assignee:
[ruby-core:57175]

Description

In http://www.ruby-doc.org/core-2.0.0/doc/syntax/methods_rdoc.html one can see simple uses of default parameters. But I couldn't find any rules on default parameters, which are calculated in a runtime such as context and order of assignments. And whether this order is always well defined? Usually it's left to right, are there any exceptions for keyword arguments or splats?

I've made a bit of tests to make am intuition of using default arguments but missed lots of corner cases. Which probably should be documented, but simple cases should be documented too.

class A
def f
'A#f'
end
def self.f
'A.f'
end
def g(arg = f)
puts arg;
end
def self.g(arg = f)
puts arg
end
def h(arg1 = f, arg2 = '(' +arg1 + ')')
puts "#{arg1}, #{arg2}"
end
def j(arg1 = 2*arg2, arg2 = f)
puts "#{arg1}, #{arg2}"
end
end

A.g # => 'A.f'
instance = A.new
instance.g # => 'A#f'
instance.h # => 'A#f, (A#f)'
instance.j # => Undefined local variable or method arg2 for #<A:...>

Updated by nobu (Nobuyoshi Nakada) about 12 years ago Actions #1 [ruby-core:57401]

It's defined as left-to-right order, always.

Updated by jeremyevans (Jeremy Evans) over 6 years ago Actions #2

  • Status changed from Open to Closed

Applied in changeset git|bf2f84b2ff298583d3efbecb88da271e99fa7930.


Document evaluation order of arguments [ci skip]

Fixes [Misc #8905]

Actions

Also available in: PDF Atom