Forum: Ruby-core [ruby-trunk - misc #8905][Open] Add documentation to semantics of method default arguments

Fe1c0797f345249625590d25a51efd0a?d=identicon&s=25 prijutme4ty (Ilya Vorontsov) (Guest)
on 2013-09-13 09:06
(Received via mailing list)
Issue #8905 has been reported by prijutme4ty (Ilya Vorontsov).

----------------------------------------
misc #8905: Add documentation to semantics of method default arguments
https://bugs.ruby-lang.org/issues/8905

Author: prijutme4ty (Ilya Vorontsov)
Status: Open
Priority: Normal
Assignee: zzak (Zachary Scott)
Category: doc
Target version:


In http://www.ruby-doc.org/core-2.0.0/doc/syntax/meth... 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:...>
F1d6cc2b735bfd82c8773172da2aeab9?d=identicon&s=25 Nobuyoshi Nakada (nobu)
on 2013-09-26 17:40
(Received via mailing list)
Issue #8905 has been updated by nobu (Nobuyoshi Nakada).


It's defined as left-to-right order, always.
----------------------------------------
misc #8905: Add documentation to semantics of method default arguments
https://bugs.ruby-lang.org/issues/8905#change-42002

Author: prijutme4ty (Ilya Vorontsov)
Status: Open
Priority: Normal
Assignee: zzak (Zachary Scott)
Category: doc
Target version:


In http://www.ruby-doc.org/core-2.0.0/doc/syntax/meth... 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:...>
This topic is locked and can not be replied to.