mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
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:
parent
2a3d58d692
commit
2b206be6ac
2 changed files with 46 additions and 11 deletions
27
README.EXT
27
README.EXT
|
@ -678,15 +678,36 @@ argument is the C array of the method arguments, and the third
|
||||||
argument is the receiver of the method.
|
argument is the receiver of the method.
|
||||||
|
|
||||||
You can use the function rb_scan_args() to check and retrieve the
|
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
|
arguments. The third argument is a string that specifies how to
|
||||||
argument, and at most receives two arguments.
|
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
|
Methods with an arbitrary number of arguments can receive arguments
|
||||||
by Ruby's array, like this:
|
by Ruby's array, like this:
|
||||||
|
|
||||||
--
|
--
|
||||||
static VALUE
|
static VALUE
|
||||||
fdbm_indexes(VALUE obj, VALUE args)
|
thread_initialize(VALUE thread, VALUE args)
|
||||||
{
|
{
|
||||||
:
|
:
|
||||||
}
|
}
|
||||||
|
|
|
@ -767,20 +767,34 @@ fdbm_s_open(int argc, VALUE *argv, VALUE klass)
|
||||||
|
|
||||||
この配列で与えられた引数を解析するための関数がopen()でも使わ
|
この配列で与えられた引数を解析するための関数がopen()でも使わ
|
||||||
れているrb_scan_args()です.第3引数に指定したフォーマットに
|
れているrb_scan_args()です.第3引数に指定したフォーマットに
|
||||||
従い,第4変数以降に指定した変数に値を代入してくれます.この
|
従い,第4変数以降に指定した変数(の参照)に値を代入してくれます.
|
||||||
フォーマットは,第1文字目が省略できない引数の数,第2文字目が
|
このフォーマットは,ABNFで記述すると以下の通りです.
|
||||||
省略できる引数の数,第3文字目が対応する相手が無いあまりの引
|
|
||||||
数があるかどうかを示す"*"です.2文字目と3文字目は省略できま
|
--
|
||||||
す.dbm.cの例では,フォーマットは"11"ですから,引数は最低1つ
|
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つまで許されるという意味になります.省略されている時の
|
で,2つまで許されるという意味になります.省略されている時の
|
||||||
変数の値はnil(C言語のレベルではQnil)になります.
|
変数の値はnil(C言語のレベルではQnil)になります.
|
||||||
|
|
||||||
Rubyの配列で引数を受け取るものはindexesがあります.実装はこ
|
最後に、引数をRubyの配列として受け取るものにはThread#initializeがあります.
|
||||||
うです.
|
実装はこうです.
|
||||||
|
|
||||||
--
|
--
|
||||||
static VALUE
|
static VALUE
|
||||||
fdbm_indexes(VALUE obj, VALUE args)
|
thread_initialize(VALUE thread, VALUE args)
|
||||||
{
|
{
|
||||||
:
|
:
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue