Module#mix

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

Module#mix$B$,F3F~$5$l$^$7$?!#$^$"!"$=$N$3$H$=$N$b$N$K$O;[email protected]$J(B
$B$N$G$9$,!"$5$i$K;EMM$r5M$a$J$$$H$$$1$J$$$h$&$J5$$,$7$F$$$kIt(B
$BJ,$,$"$j$^$9!#;[email protected]$7$?;d$N%V%m%0$G$b$9$G$K=R$Y$i(B
$B$l$F$$$^$9!#(B

($B0zMQ(B)
mix$B$5$l$k%b%8%e!<%k$,JL$N%b%8%e!<%k$r(Binclude$B$7$F$$$?>l9g$K(B
$B$O$I$&$J$k$+!#(B
$B$*$=$i$/$ONc30$K$J$k!#(Bmix$B$H(Binclude$B$O:.$<$k$Y(B
$B$-$G$O$J$$!#(B

$B8=:$N<BAu$G;n$7$F$$^$7$g$&!#(B

module Foo
def m
end
end
module Bar
include Foo
end
class Baz
mix Bar
end
p Baz.new.m

m$B$,(BNoMethodError$B$K$J$j$^$9!#$D$^$j!"8=>u(BBar$B$O(BFoo$B$r%$%s%/%k!<(B
$B%I$7$F$$$k$K$b4X$o$i$:!"L5;k$5$l$F$$$k$o$1$G$9$M!#$3$l$O%(%i!<(B
$B$K$J$k$Y$-$G$7$g$&!#(B

($B0zMQ(B)
mix$B$GJLL>$rIU$1$FLdBj2r7h!"$HFI$a$k$h$&[email protected]$$J}$r$7$F$$$k$,!"(B
$B<B:]$K$O%b%8%e!<%kFbIt$GL>A0$r=q$-49$($kA0$N%a%=%C%I$rFI$s(B
[email protected]$,$"$k!#(B $B$=$l$r$I$&$9$k$+!#$J$K$b$7$J$$(B($B=EJ#$9(B
$B$kJ}$,0-$$(B)$B$H$9$k9M$($b$"$k$,!"(B [email protected]$H$;$C$+$/6lO+$7$F(B
Traits$B$rF3F~$7$h$&$H$7$F$$$k$N$,(B $B$^$C$?$/L5BL$K$J$k$N$G!“L>(B
$BA0$r=q$-49$($?%a%=%C%I8F$S=P$7$r(B $B%b%8%e!<%k$N%a%=%C%IDj5A<B(B
$BBN$+$iC5$7=P$7$F%a%=%C%I$r%3%T!<$9$k(B $B$H$$$&(B(Bertrand Mayer
$B$N(BOOSC$B$K5-=R$5$l$F$$$?%”%$%G%#%"(B)$B$rF3F~$9$k$3$H$r9M$($k(B

$B$3$l$b;n$7$^$7$g$&!#(B

module Foo
def m
:Foo
end
def x
self.m
end
end

module Bar
def m
:Bar
end
def y
self.m
end
end

class Baz
mix Foo, :m => :m_f
mix Bar, :m => :m_b
end

obj = Baz.new
p obj.x

$B$9$k$H(BFoo#x$B$NCf$G8F$P$l$?(Bm$B$O$9$G$K%j%M!<%$5$l$FB8:_$7$J$$$N(B $B$G%(%i!<$K$J$j$^$9!#$7$+$7!"$3$l$O%$%s%/%k!<%I;~$K%a%=%C%I(Bx$B$N(B $BK\BN$r(B($B%3%T!<$7$?>e$G!K=q$-49$($F!"(Bm$B$NBe$o$j$K%j%M!<%$5$l$?(B
m_f$B$r8F$V$Y$-$+$b$7$l$^$;$s!#$"$k$$$O!“8=>uDL$j!”$=$s$J$3$H$O(B
$B$7$J$$$H$$$&$N$b$R$H$D$N9M$($G$9!#(B

$B6a1J$H?=$7$^$9!#(B

$B$^$:(B r31873 $B$GF3F~$5$l$?(B Module#mix
$B$ODj?t$N%3%T!<$N;XDj$,8z$$$F$$$J$$$N$H!"(B
$BDj?t$N;XDj$,(B rename $B$"$j$N;XDj$7$+<u$1IU$1$F$$$^$;$s$G$7$?!#(B
[email protected]$7$?%Q%C%A$r0J2<$K>e$2$F$*$-$^$9!#(B

https://github.com/nagachika/ruby/commit/fb7314e3682da41fa32a0ef0a4d8822f1b3d2ef0

$B$3$A$i$,K\Bj$G$9$,(B Module#mix
$B$N;EMM$K$D$$$F!“[email protected][email protected]$,$”$j$^$7$?$N$G(B
[email protected]@$K2C$($FD:$-$?$$$H;W$$$^$9!#(B

mix
$B$O%G%U%)%k%H$G$ODj?t$r%3%T!<$7$^$;$s$,!"$=$N$?$aFbIt%/%i%9$dDj?t$K0MB8$7$?(B
$B%a%=%C%I$,MxMQ$G$-$J$/$J$C$F$7$^$$$^$9!#$?$H$($P0J2<$N$h$&$J(B
FileUtils $B$NNc$,$"$j$^$9!#(B

require “fileutils”
class C
mix FileUtils
def meth1
touch(“file”)
end
end

C.new.meth1 #=> uninitialized constant C::OPT_TABLE (NameError)

$B$b$A$m$sI,MW$JDj?t$r%3%T!<$9$l$PF0$-$^$9$,!"(Bfileutils.rb $B$NCf$G(B
OPT_TABLE $B$O(B
OPT_TABLE = {} #:nodoc: internal use only
$B$N$h$&$K(B nodoc $B$H;XDj$5$l$F$$$F!“FbItE*$JDj?t$G$”$k$h$&$G$9!#(B
$B%i%$%V%i%j$N%f!<%6$,$3$N$h$&$JDj?t$NB8:_$r0U<1$7$J$$$H$$$1$J$$$N$O(B
$BFbIt9=B$$K0MB8$9$k$3$H$K$J$C$F$h$/$J$$$H;W$$$^$9!#(B

$B;XDj$J$7$GDj?t$,%3%T!<$5$l$k$J$i$P0U<1$;$:$KMxMQ$G$-$^$9$,!">WFM$,H/@8$7$?;~$K(B
rename
$B$9$k$3$H$G2sHr$7$?>l9g!"$3$l$b%a%=%C%I$N;~$HF1MM$K%3%T!<$5$l$k%a%=%C%IFb$G(B
$B$=$NDj?t$r;2>H$7$F$$$kItJ,$b(B rename
$B$9$Y$-$+$I$&$+$H$$$&[email protected]@$K$J$k$+$H;W$$$^$9!#(B

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

Module#mix$B$,F3F~$5$l$^$7$?!#$^$"!"$=$N$3$H$=$N$b$N$K$O;[email protected]$J(B
$B$N$G$9$,!"$5$i$K;EMM$r5M$a$J$$$H$$$1$J$$$h$&$J5$$,$7$F$$$kIt(B
$BJ,$,$"$j$^$9!#;[email protected]$7$?;d$N%V%m%0$G$b$9$G$K=R$Y$i(B
$B$l$F$$$^$9!#(B

kosaki$B$G$9!#(B

$B5!G=<+BN$K$O0[[email protected]$O$J$$$N$G$9$,[email protected]>$J$H$3$m%3%a%s%H$G$-$k$[$I(BRuby$B$rJ,$+$C$F$$$J$$$G$9!K!"(Bfeature
freeze
$B$r2a$.$?$H$3$m$+$i;[email protected]$,%9%?!<%H$9$k$H$$$&$N$O$"$s$^$j$G$O$J$$$G$7$g$&$+!#(B
$B$o$?$7$K$O$3$N5!G=$,(B1.9.4 (or 2.0)
$B$K%9%j%C%W$7$?$iC/$,:$$k$N$+$b$&0l$D8+$($F$$$^$;$s!#$b$N$9$4$/@5D>[email protected]$&$H!#$"$H!"(Bruby-core$B$G$O0lEY$b>R2p$5$l$F$$$J$$5$$,[email protected]$H;W$C$F$$$^$9!#(B

$B%9%1%8%e!<%k$K$D$$$F$b$^$H$b$H$5$s$HCfED$5$s$N0U8+$r$*J9$+$;4j$($l$P$H;W$$$^$9(B

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

In message “Re: [ruby-dev:43625] Re: Module#mix”
on Sun, 5 Jun 2011 03:21:06 +0900, KOSAKI Motohiro
[email protected] writes:

|$B5!G=<+BN$K$O0[[email protected]$O$J$$$N$G$9$,[email protected]>$J$H$3$m%3%a%s%H$G$-$k$[$I(BRuby$B$rJ,$+$C$F$$$J$$$G$9!K!"(Bfeature
freeze
|$B$r2a$.$?$H$3$m$+$i;[email protected]$,%9%?!<%H$9$k$H$$$&$N$O$"$s$^$j$G$O$J$$$G$7$g$&$+!#(B
|$B$o$?$7$K$O$3$N5!G=$,(B1.9.4 (or 2.0)
|$B$K%9%j%C%W$7$?$iC/$,:$$k$N$+$b$&0l$D8+$($F$$$^$;$s!#$b$N$9$4$/@5D>[email protected]$&$H!#$"$H!"(Bruby-core$B$G$O0lEY$b>R2p$5$l$F$$$J$$5$$,[email protected]$H;W$C$F$$$^$9!#(B
|
|$B%9%1%8%e!<%k$K$D$$$F$b$^$H$b$H$5$s$HCfED$5$s$N0U8+$r$*J9$+$;4j$($l$P$H;W$$$^$9(B

$B;d$O(B1.9.3$B$KF~$k$Y$-5!G=$G$O$J$$$H;W$C$F$^$9!#@h$K%V%i%s%[email protected](B
$B$k$Y$-$H$$$&0U8+$G$"$l$P;[email protected]$G$9!#[email protected]$5$s$O$I$&;[email protected](B
$B$m$&!)(B

[email protected]$G$9!#(B

At Sun, 5 Jun 2011 09:24:29 +0900,
Yukihiro M. wrote in [ruby-dev:43627]:

$B$k$Y$-$H$$$&0U8+$G$"$l$P;[email protected]$G$9!#[email protected]$5$s$O$I$&;[email protected](B
$B$m$&!)(B

$B;EMM$O5nG/$9$G$K7h$^$C$F$$$k$b$N$H;W$C$F$$$?$N$G!"<BAu$r0BDj$5$;(B
$B$k$K$O$b$&F~$l$F$*$+$J$$$H4V$K9g$o$J$$$H;W$C$?$N$G$9$,!“Aa7W$K2a(B
$B$.$?$h$&$G$9!#2~$a$F(BRubyKaigi2010$B$N%9%i%$%I$r8+D>$9$H!”(B
Module#prepend$BAjEy$d$i(Brefinement$B$,I,MW$=$&$J5!G=$,4^$^$l$F$$$?$j(B
$B$G(B1.9.3$B$K$OL5M}$,$"$j$=$&$G$9!#(B1.9.4$BAw$j$K;[email protected]$7$^$9!#(B

$B5nG/$9$0$KF~$l$F$*$1$P$h$+$C$?!#(B

[email protected]$G$9!#(B

At Sat, 4 Jun 2011 03:45:32 +0900,
nagachika wrote in [ruby-dev:43623]:

$B$^$:(B r31873 $B$GF3F~$5$l$?(B Module#mix
$B$ODj?t$N%3%T!<$N;XDj$,8z$$$F$$$J$$$N$H!"(B
$BDj?t$N;XDj$,(B rename $B$"$j$N;XDj$7$+<u$1IU$1$F$$$^$;$s$G$7$?!#(B
[email protected]$7$?%Q%C%A$r0J2<$K>e$2$F$*$-$^$9!#(B

https://github.com/nagachika/ruby/commit/fb7314e3682da41fa32a0ef0a4d8822f1b3d2ef0

$B$?$7$+$KDj?t$^$o$j$OL$<BAuItJ,$,;D$C$F$$$^$7$?!#(B

touch("file")

end
end

C.new.meth1 #=> uninitialized constant C::OPT_TABLE (NameError)

RubyKaigi2010$B$G$N%9%i%$%I(Bp56([1]24:34)$B$G$O!"(Bmix$B$5$l$?%a%=%C%I$O(B
$B85$N%9%3!<%W$+$iDj?t$rC5:w$9$k$H$$$&Iw$K8+$($^$9!#$D$^$j!"%a%=%C(B
$B%I%(%s%H%j!<$rC1=c$K%3%T!<$7$F$$$k:#$N<BAu$,!";EMM$H0c$&$H$$$&$3(B
$B$H$G$O$J$$$+$H;W$$$^$9!#(B

[1] http://www.nicovideo.jp/watch/sm11914317

$B6a1J$G$9!#(B

2011$BG/(B6$B7n(B6$BF|(B0:42 Nobuyoshi N. [email protected]:

RubyKaigi2010$B$G$N%9%i%$%I(Bp56([1]24:34)$B$G$O!"(Bmix$B$5$l$?%a%=%C%I$O(B
$B85$N%9%3!<%W$+$iDj?t$rC5:w$9$k$H$$$&Iw$K8+$($^$9!#$D$^$j!"%a%=%C(B
$B%I%(%s%H%j!<$rC1=c$K%3%T!<$7$F$$$k:#$N<BAu$,!";EMM$H0c$&$H$$$&$3(B
$B$H$G$O$J$$$+$H;W$$$^$9!#(B

[1] http://www.nicovideo.jp/watch/sm11914317

$B8+Mn$7$F$^$7$?!#3N$+$KDj?tC5:w$O%3%T!<85$N0LCV$+$i9T$J$o$J$$$H(B
$B$$$1$J$$$h$&$G$9$M!#(B
$B3’$GF02h$r$_$F3NG’$9$k$N$bBgJQ$J$N$G%9%i%$%I(Bp56
$B$+$i$3$A$i$K0zMQ$5$;$FD:$-$^$9!#(B

constant resolution based on

module Foo
C = 42
def foo() p C: end
end
class Bar
C = 55
mix Foo
end
Bar.new.foo # => 42

$B$3$l$J$iDj?t$O%3%T!<$7$J$/$F$b$9$`$N$G(B [ruby-dev:43623]
$B$GDs<($7$?LdBj$O<h$j2<$2$^$9!#(B
$B<:Ni$7$^$7$?!#(B

$B5nG/$9$0$KF~$l$F$*$1$P$h$+$C$?!#(B

$B!!$3$N7o$C$F$b$&La$7$?$s$G$7$g$&$+!%(B

$B$^[email protected][email protected]$H;W$$$^$9!#%V%i%s%[email protected]$C$F$+$i(B193$B%V%i%s%[email protected]$1La$7$?J}$,(B
$B<j4V$,>/$J$/$"$j$^$;$s!)(B

[email protected]$G$9!%(B

(2011/06/06 0:47), Nobuyoshi N. wrote:

|
$B$G(B1.9.3$B$K$OL5M}$,$"$j$=$&$G$9!#(B1.9.4$BAw$j$K;[email protected]$7$^$9!#(B

$B5nG/$9$0$KF~$l$F$*$1$P$h$+$C$?!#(B

$B!!$3$N7o$C$F$b$&La$7$?$s$G$7$g$&$+!%(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