Keyword arguments

$B1sF#$G$9!#(B

redmine $B$N(B 2.0 tracker $B$K%A%1%C%H$rEPO?$7$?$N$G$9$,!"%a!<%k$,(B

$BN.$l$J$$$h$&$G$9!#%Q%C%A$O%A%1%C%H$r8+$F$/[email protected]$5$$!#(B

http://redmine.ruby-lang.org/issues/5454

$B%-!<%o!<%I0z?t$r<BAu$7$F$_$^$7$?!#(B

$ cat t.rb
def foo(str: “foo”, num: 424242)
p [str, num]
end
foo(str: “bar”)

$ ./ruby t.rb
[“bar”, 424242]

$B:Y$+$$$3$H$O$^[email protected]$$K$7$F$$$^$;$s$,!"%-!<%o!<%I0z?t$,(B 2.0 $B$KF~$k(B
$B$3$H$O$+$J$j3NDjE*$J$N$G!"$H$j$"$($:(B trunk $B$KF~$l$F9M$($k$H$$$&(B
$B$N$O$I$&$G$7$g$&$+!#(B

$B0l1~;d$N4D6-$G$O(B make check $B$ODL$j$^$9(B
($B%-!<%o!<%I0z?t$N%F%9%H(B
$B$O=q$$$F$J$$$G$9$,(B) $B!#%Q!<%5$K(B conflict $B$O$J$$$G$9!#(B
$B4{B8$N%Y%s%A%^!<%/$r8+$k$H!“0lIt5$$K$J$k9`L$b$”$j$^$9$,(B (vm1_
rescue $B$H$+(B) $B!"A4BNE*$K$OFC$KB.EYNt2=$7$J$$$h$&$G$9!#(B

$B0J2<$OL$<BAu!&7|0F$G$9!#(B

  • $B;D$j$N%-!<%o!<%I0z?t$r<u$1<h$kJ}K!$,I,MW$+(B

    def foo(str: “foo”, num: 424242, **h)
    p h
    end
    foo(str: “bar”, check: true) #=> { :check => true }

  • nil $B$,EO$5$l$?$i$d$O$j(B nil $B$K$J$k$Y$-!)(B

    def foo(str: “foo”)
    p str
    end
    foo(str: nil) #=> nil ? ($B:#$O<jH4$-$G(B “foo”)

  • rest $B$J$7$GCN$i$J$$%-!<%o!<%I$,Mh$?$iNc30!&7Y9p$rH/$7$F(B
    $BM_$7$$!)(B

    def foo(str: “foo”, num: 424242)
    end
    foo(check: true) #=> ArgumentError?

  • Hash $B$N$h$&$K?6$kIq$&%*%V%8%’%/%H$b%-!<%o!<%I0z?t$H$7$F(B
    $B2r<a$9$Y$-!)(B ($B8=:_$OB.EY=E;k$G(B T_HASH [email protected]$18+$F$$$k(B)

  • $B0z?t$N=gHV$O$h$$$+(B

    def foo(
    a, b, c, # mandatory arguments
    opt = 1, # optional arguments
    *rest, # rest argument
    x, y, z, # post mandatory arguments
    k1: 1, k2:2, # keyword arguments
    **kh, # keyword rest argument
    &blk # block argument
    )

  • $BB>$$$m$$$m(B

$B$^$D$b$H(B $B$f$-$R$m$G$9(B

In message “Re: [ruby-dev:44602] keyword arguments”
on Tue, 18 Oct 2011 01:36:58 +0900, Yusuke E. [email protected]
writes:

|$B%-!<%o!<%I0z?t$r<BAu$7$F$_$^$7$?!#(B

$B$"$j$,$H$&$4$6$$$^$9!#(B

| $ cat t.rb
| def foo(str: “foo”, num: 424242)
| p [str, num]
| end
| foo(str: “bar”)
|
| $ ./ruby t.rb
| [“bar”, 424242]
|
|$B:Y$+$$$3$H$O$^[email protected]$$K$7$F$$$^$;$s$,!"%-!<%o!<%I0z?t$,(B 2.0 $B$KF~$k(B
|$B$3$H$O$+$J$j3NDjE*$J$N$G!"$H$j$"$($:(B trunk $B$KF~$l$F9M$($k$H$$$&(B
|$B$N$O$I$&$G$7$g$&$+!#(B

$B;[email protected]$G$9!#(Btrunk$B$KF~$l$A$c$C$F$$$$$N$+$J!#$=$&$$$&5!G=$NF~$C(B
$B$F$$$J$$(B1.9$B%V%i%s%A$,M_$7$$?M$O5^[email protected]$C$?$[$&$,$$$$$H;W$$(B
$B$^$9$h!#(B

|$B0J2<$OL$<BAu!&7|0F$G$9!#(B
|
| - $B;D$j$N%-!<%o!<%I0z?t$r<u$1<h$kJ}K!$,I,MW$+(B
|
| def foo(str: “foo”, num: 424242, **h)
| p h
| end
| foo(str: “bar”, check: true) #=> { :check => true }

$B%a%=%C%I$N0>y$N$?$a$KI,[email protected]$H;W$$$^$9!#(B

| - nil $B$,EO$5$l$?$i$d$O$j(B nil $B$K$J$k$Y$-!)(B
|
| def foo(str: “foo”)
| p str
| end
| foo(str: nil) #=> nil ? ($B:#$O<jH4$-$G(B “foo”)

nil$B$K$J$k$Y$-$G$7$g$&!#(B

| - rest $B$J$7$GCN$i$J$$%-!<%o!<%I$,Mh$?$iNc30!&7Y9p$rH/$7$F(B
| $BM_$7$$!)(B
|
| def foo(str: “foo”, num: 424242)
| end
| foo(check: true) #=> ArgumentError?

[email protected]$NM>CO$,$"$k$H$3$m$G$9$M!#8=;[email protected]$G$O!"(B**$B$G<u$1<h$i(B
[email protected]$H9M$($F$$$^$9$,!"<B:]$K$$$m$$$m$J6I(B
$BLL$G;H$C$F$_$k$H0U8+$,JQ$o$k$+$b$7$l$^$;$s!#(B

| - Hash $B$N$h$&$K?6$kIq$&%*%V%8%’%/%H$b%-!<%o!<%I0z?t$H$7$F(B
| $B2r<a$9$Y$-!)(B ($B8=:_$OB.EY=E;k$G(B T_HASH [email protected]$18+$F$$$k(B)

$B$H$j$"$($:(Bto_hash$B$r;H$C$F$/[email protected]$5$$!#[email protected]_$N(Bto_kw$B$N$h$&$J%a%=%C(B
$B%I$r;H$&$Y$-$J$N$+$b$7$l$^$;$s$,!#(B

| - $B0z?t$N=gHV$O$h$$$+(B
|
| def foo(
| a, b, c, # mandatory arguments
| opt = 1, # optional arguments
| *rest, # rest argument
| x, y, z, # post mandatory arguments
| k1: 1, k2:2, # keyword arguments
| **kh, # keyword rest argument
| &blk # block argument
| )

$B$3$l$GNI$$$H;W$$$^$9!#(B

                            $B$^$D$b$H(B $B$f$-$R$m(B /:|)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs