Improve documentation of rb_scan_args().

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22335 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
knu 2009-02-16 05:54:17 +00:00
parent 2a3d58d692
commit 2b206be6ac
2 changed files with 46 additions and 11 deletions

View File

@ -678,15 +678,36 @@ argument is the C array of the method arguments, and the third
argument is the receiver of the method.
You can use the function rb_scan_args() to check and retrieve the
arguments. For example, "11" means that the method requires at least one
argument, and at most receives two arguments.
arguments. The third argument is a string that specifies how to
capture method arguments and assign them to the following variable
references. The format can be described in ABNF as follows:
--
scan-arg-spec := param-arg-spec [block-arg-spec]
param-arg-spec := pre-arg-spec [post-arg-spec] / post-arg-spec
pre-arg-spec := num-of-leading-mandatory-args [num-of-optional-args]
post-arg-spec := sym-for-variable-length-args
block-arg-spec := sym-for-block-arg
num-of-leading-mandatory-args := DIGIT ; -- the number of the leading mandatory arguments
num-of-optional-args := DIGIT ; -- the number of the following optional arguments
sym-for-variable-length-args := "*" ; -- indicates that the following variable length
; arguments are captured as a Ruby array
sym-for-block-arg := "&" ; -- indicates that the iterator block should be
; captured if given
--
For example, "11" means that the method requires at least one
argument, and at most receives two arguments. For omitted arguments,
variables are set to Qnil.
Methods with an arbitrary number of arguments can receive arguments
by Ruby's array, like this:
--
static VALUE
fdbm_indexes(VALUE obj, VALUE args)
thread_initialize(VALUE thread, VALUE args)
{
:
}

View File

@ -767,20 +767,34 @@ fdbm_s_open(int argc, VALUE *argv, VALUE klass)
この配列で与えられた引数を解析するための関数がopen()でも使わ
れているrb_scan_args()です第3引数に指定したフォーマットに
従い第4変数以降に指定した変数に値を代入してくれますこの
フォーマットは第1文字目が省略できない引数の数第2文字目が
省略できる引数の数第3文字目が対応する相手が無いあまりの引
数があるかどうかを示す"*"です2文字目と3文字目は省略できま
dbm.cの例ではフォーマットは"11"ですから引数は最低1つ
従い第4変数以降に指定した変数(の参照)に値を代入してくれます.
このフォーマットはABNFで記述すると以下の通りです
--
scan-arg-spec := param-arg-spec [block-arg-spec]
param-arg-spec := pre-arg-spec [post-arg-spec] / post-arg-spec
pre-arg-spec := num-of-leading-mandatory-args [num-of-optional-args]
post-arg-spec := sym-for-variable-length-args
block-arg-spec := sym-for-block-arg
num-of-leading-mandatory-args := DIGIT ; -- 先頭に置かれる省略不可能な引数の数
num-of-optional-args := DIGIT ; -- 続いて置かれる省略可能な引数の数
sym-for-variable-length-args := "*" ; -- 続いて置かれる可変長引数をRubyの配列で
; 取得するための指定
sym-for-block-arg := "&" ; -- イテレータブロックを取得するための指定
--
dbm.cの例ではフォーマットは"11"ですから引数は最低1つ
2つまで許されるという意味になります省略されている時の
変数の値はnil(C言語のレベルではQnil)になります.
Rubyの配列で引数を受け取るものはindexesがあります実装はこ
うです.
最後に、引数をRubyの配列として受け取るものにはThread#initializeがあります
実装はこうです.
--
static VALUE
fdbm_indexes(VALUE obj, VALUE args)
thread_initialize(VALUE thread, VALUE args)
{
:
}