Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ruby講義 第11回

Ruby講義 第11回

継承、Mix-in

Kuniaki IGARASHI

June 20, 2012
Tweet

More Decks by Kuniaki IGARASHI

Other Decks in Technology

Transcript

  1. ϨγϐΦϒδΣΫτ DMBTT3FDJQF EFGUJUMF !UJUMF FOE EFGUJUMF U !UJUMFU !VQEBUFE@BU5JNFOPX FOE

     !UJUMFDIFFTFDBLF !BVUIPSJHBSBTIJ  FOE ϨγϐΦϒδΣΫτͷ໾ׂͱ੹೚ Ϩγϐʹؔ͢Δσʔλͱ खଓ͖Λ࣋ͪ·͢ɻ Ϩγϐʹؔ͢ΔσʔλΛ؅ ཧ͠·͢ɻ σʔλ΁ͷΞΫηε͸ॴఆ ͷϝιουΛ௨͓ͯ͡ئ͍ ͠·͢ɻ ެ։κʔϯ  ɹ ඇެ։κʔϯ σʔλΛ֎͔Βউखʹॻ͖׵͑ΔͱϨγϐΦϒδΣΫτ͸੹೚ΛՌͨͤͳ͍ σʔλ ม਺ ͸֎෦͔Β͸ΞΫηεͰ͖ͳ͘͢΂͖ 3VCZͷDMBTT͸࠷ॳ͔Βͦ͏ͳΔΑ͏ʹઃܭ͞Ε͍ͯ·͢
  2. QVCMJD QSJWBUF ϝιουͷެ։ɾඇެ։Λ੍ޚͰ͖·͢ɻ DMBTT"DDFTT5FTU QVCMJD EFGTIPX QVUTQVCMJDNFUIPE FOE ͜͜ʹॻ͍ͨϝιου͸QVCMJD QSJWBUF

    EFGTFDSFU QVUTQSJWBUFNFUIPE FOE ͜͜ʹॻ͍ͨϝιου͸QSJWBUF FOE PCK"DDFTT5FTUOFX PCKTIPXQVCMJDNFUIPE PCKTFDSFU QVCMJD Ҏ߱ͷϝιουΛެ։ϝιουʹ͠ ·͢ɻ ·ͨ͸ɺԿ΋ॻ͔ͳ͍ͱ QVCMJDʹͳΓ·͢ QSJWBUF Ҏ߱ͷϝιουΛඇެ։ϝιουʹ ͠·͢ɻඇެ։ϝιου͸ɺΦϒ δΣΫτ಺෦͔Β͸ݺͼग़ͤ·͢ ͕ɺ֎෦͔Β͸ݺͼग़ͤ·ͤΜɻ ˞QSPUFDUFEͱ͍͏ͷ΋͋Δͷ Ͱ͕͢ɺ໓ଟʹ࢖Θͳ͍ͷͰলུ
  3. ΦϒδΣΫτͷ಺෦ͱ֎෦ DMBTT"DDFTT5FTU EFGTIPX TFDSFU͜͜͸಺෦ FOE QSJWBUF EFGTFDSFU QVUTQSJWBUFNFUIPE FOE FOE

    PCK"DDFTT5FTUOFX PCKTFDSFU͜͜͸֎෦ ಺෦ ͦͷΦϒδΣΫτΫϥεͷϝιου ͷதɻͦͷΦϒδΣΫτΫϥεͷ DMBTTʙFOEͷؒɻ ֎෦ ͦΕҎ֎ ·ͨ͸ɺ QSJWͷΑ͏ʹϝιου໊͚ͩͰݺ΂Δͱ ͜Ζ͕಺෦ɺ PCKQSJWͷΑ͏ʹ ΦϒδΣΫτϝιου໊Ͱ ݺͿͱ͜Ζ͕֎෦Ͱ͢ɻ ಺෦
  4. ܧঝ DMBTT#PPL BUUS@BDDFTTPSUJUMF QSJDF FOE DMBTT.BHB[JF BUUS@BDDFTTPSUJUMF QSJDF OVNCFS FOE

    ͨͱ͑͹#PPLΫϥεͱ.BHB[JOFΫϥε ࡶࢽ Λ࡞Δͱ͠·͢ɻ ผʑʹఆٛΛॻ͍ͯ΋͍͍ͷͰ͕͢ɺڞ௨߲ ΋ͨ͘͞Μ͋Γ·͢ɻ
  5. ܧঝ DMBTT#PPL BUUS@BDDFTTPSUJUMF QSJDF FOE DMBTT.BHB[JF BUUS@BDDFTTPSUJUMF QSJDF OVNCFS FOE

    ͨͱ͑͹#PPLΫϥεͱ.BHB[JOFΫϥε ࡶࢽ Λ࡞Δͱ͠·͢ɻ ผʑʹఆٛΛॻ͍ͯ΋͍͍ͷͰ͕͢ɺڞ௨߲ ΋ͨ͘͞Μ͋Γ·͢ɻ
  6. ܧঝ DMBTT#PPL BUUS@BDDFTTPSUJUMF QSJDF FOE DMBTT.BHB[JOF#PPL BUUS@BDDFTTPSOVNCFS FOE ͦΜͳͱ͖͸ɺܧঝΛ࢖͏ͱศརͰ͢ɻ DMBTT.BHB[JOF#PPLͱॻ͘͜ͱͰɺ#PPLΫϥεΛܧঝͨ͠

    .BHB[JOFΫϥεΛఆٛͰ͖·͢ɻ.BHB[JOFΫϥε͸#PPLΫϥε ͷੑ࣭Λड͚ܧ͗·͢ɻԿΛड͚ܧ͙͔Λ࣍ͷϖʔδͰղઆ͠·͢ɻ DMBTT#PPL BUUS@BDDFTTPSUJUMF QSJDF FOE DMBTT.BHB[JOF BUUS@BDDFTTPSUJUMF QSJDF OVNCFS FOE ಉ͡ಈ࡞ ܧঝΛ࢖ͬͨॻ͖ํ ܧঝΛ࢖Θͳ͍ॻ͖ํ
  7. ܧঝ DMBTT#PPL BUUS@BDDFTTPSUJUMF QSJDF FOE DMBTT.BHB[JF#PPL BUUS@BDDFTTPSOVNCFS FOE ྫ͑͹#PPLΫϥεΛܧঝͨ͠ .BHB[JOFΫϥε͸ɺUJUMFͱQSJDFΛ

    ड͚ܧ͍Ͱ͍·͢ɻྫ͑͹ɺˣͷΑ͏ͳ ίʔυΛॻ͘͜ͱ͕Ͱ͖·͢ɻ DMBTTΫϥε໊εʔύʔΫϥε໊ Ϋϥεͷఆٛ FOE ܧঝ͢Δ৔߹ͷॻࣜ εʔύʔΫϥεͱ͸ɺܧঝݩͷ Ϋϥε ਌Ϋϥε Ͱ͢ɻ ܧঝͨ͠Ϋϥε͸ɺ਌ΫϥεͷશͯͷΠϯελϯεม਺ɺϝιο υͳͲΛड͚ܧ͗·͢ɻ NBHB[JOF.BHB[JOFOFX NBHB[JOFUJUMF$BO$BN QNBHB[JOFUJUMF$BO$BN
  8. ܧঝԋश໰୊ ӈͷ#PPLΫϥεΛܧঝͨ͠ %JHJUBM#PPL ిࢠॻ੶ Ϋ ϥεΛ࡞͍ͬͯͩ͘͞ɻ %JHJUBM#PPLΫϥεʹӈͷΑ ͏ͳpMFOBNFϝιουΛ࣮ ૷͍ͯͩ͘͠͞ɻ %JHJUBM#PPLΫϥεͷΠϯε

    λϯεΦϒδΣΫτΛ࡞੒͠ ͯɺ!UJUMFʹ.PNPΛηο τ͠ɺpMFOBNFϝιουΛ ݺͼग़͍ͯͩ͘͠͞ɻ DMBTT#PPL EFGUJUMF !UJUMF FOE EFGUJUMF U !UJUMFU FOE FOE ͜͜ʹ%JHJUBM#PPLΫϥεΛॻ͘  ಉ͡ϑΝΠϧͰ#PPLͷԼ pMFOBNFϝιου͸ҎԼɻ EFGpMFOBNF !UJUMF QEG FOE
  9. ܧঝԋश໰୊ղ౴ ӈͷ#PPLΫϥεΛܧঝͨ͠ %JHJUBM#PPL ిࢠॻ੶ ΫϥεΛ࡞ͬ ͍ͯͩ͘͞ɻ %JHJUBM#PPLΫϥεʹӈͷΑ͏ͳ pMFOBNFϝιουΛ࣮૷ͯͩ͘͠͞ ͍ɻ%JHJUBM#PPLΫϥεͷΠϯελϯ εΦϒδΣΫτΛ࡞੒ͯ͠ɺ!UJUMF

    ʹ.PNPΛηοτ͠ɺpMFOBNFϝ ιουΛݺͼग़͍ͯͩ͘͠͞ɻ DMBTT#PPL EFGUJUMF !UJUMF FOE EFGUJUMF U !UJUMFU FOE FOE DMBTT%JHJUBM#PPL#PPL EFGpMFOBNF !UJUMF QEG FOE FOE FCPPL%JHJUBM#PPLOFX FCPPLUJUMF.PNP QFCPPLpMFOBNF.PNPQEG QFCPPLUJUMF.PNP
  10. ܧঝԋश໰୊ղ౴ DMBTT#PPL EFGUJUMF !UJUMF FOE EFGUJUMF U !UJUMFU FOE FOE

    DMBTT%JHJUBM#PPL#PPL EFGUJUMF <FCPPL> !UJUMF FOE FOE FCPPL%JHJUBM#PPLOFX FCPPLUJUMF.PNP QFCPPLUJUMF<FCPPL>.PNP ҎԼͷ#PPLΫϥεΛܧঝͨ͠%JHJUBM#PPL ి ࢠॻ੶ ΫϥεΛ࡞͍ͬͯͩ͘͞ɻ %JHJUBM#PPLΫϥεʹ਌Ϋϥεʹ͋Δϝιουͱ ಉ໊ͷUJUMFϝιουΛ࣮૷͢ΔͱͲ͏ͳΔͰ͠ΐ ͏͔ɻ%JHJUBM#PPLΫϥεͷΠϯελϯεΦϒ δΣΫτΛ࡞੒ͯ͠ɺ!UJUMFʹ.PNPΛηοτ ͠ɺUJUMFϝιουΛݺͼग़͍ͯͩ͘͠͞ɻ ܧঝͨ͠ΫϥεͰ਌Ϋϥεͱಉ໊ͷϝιο υ͕͋Δ৔߹͸ɺϝιουఆ্͕ٛॻ͖͞ Ε·͢ɻҎԼͷΑ͏ʹɺ#PPLΫϥεʹ͸ Өڹ͸ग़·ͤΜɻ CPPL#PPLOFX CPPLUJUMF.PNP QCPPLUJUMF.PNP
  11. ܧঝԋश໰୊4ղ౴ DMBTT#PPL EFGJOGPSNBUJPO QVUT(SFBU#PPL FOE FOE DMBTT%JHJUBM#PPL#PPL EFGJOGPSNBUJPO QVUT*OGPSNBUJPO TVQFS

    FOE FOE EJHJUBM@CPPL%JHJUBM#PPLOFX EJHJUBM@CPPLJOGPSNBUJPO ܧঝͨ͠ΫϥεͰ਌Ϋϥεͱಉ໊ͷϝιο υ͕͋Δ৔߹ɺϝιου͸ܧঝͨ͠Ϋϥε ͷ΋ͷ͕༏ઌͯ͠ݺ͹Ε·͢ɻ͔͠͠ɺ਌ ΫϥεͷϝιουΛ্ॻ͖ͯ͠͠·͏Θ͚ Ͱ͸ͳ͘ɺTVQFSͱ͍͏໋ྩΛ࢖͏ͱ਌ Ϋϥεͷಉ໊ϝιουΛݺͼग़͢͜ͱ͕Ͱ ͖·͢ɻ ҎԼͷ#PPLΫϥεΛܧঝͨ͠ %JHJUBM#PPL ిࢠॻ੶ ΫϥεΛ࡞Γɺ ਌Ϋϥεʹ͋Δϝιουͱಉ໊ͷϝιου Λ࡞Γɺϝιου಺ͰTVQFSΛݺͼग़͠ ͯಈ࡞Λ֬ೝͯ͠Έ͍ͯͩ͘͞ɻ
  12. ॏෳ͕ଟ͍ίʔυ ॏෳ͕ଟ͍ίʔυ͸ɺಈ࡞Λมߋ͍ͨ͠ͱ͖ʹͨ͘͞Μॻ͖ ׵͑Δඞཁ͕͋Γ·͢ɻ DMBTT"MJDF EFGIJ ͜Μͪ͸ FOE FOE DMBTT#PC EFGIJ

    ͜Μͪ͸ FOE FOE ͜Μͪ͸Λ IJʹ มߋ͍ͨ͠ DMBTT"MJDF EFGIJ IJ FOE FOE DMBTT#PC EFGIJ IJ FOE FOE ͜ͷ৔߹͸Օॴ௚͞ͳ͍ͱ͍͚ͳ ͍͚ͲɺճͰࡁ·ͤΒΕͳ͍͔ʁ
  13. .PEVMF ΫϥεͰϞδϡʔϧΛJODMVEF͢ΔͱɺNPEVMFʹఆٛ͠ ͯ͋ΔϝιουΛΫϥε΁௥Ճ͢Δ͜ͱ͕Ͱ͖·͢ɻ NPEVMF(SFFUJOH EFGIFMMP QVUT)FMMP FOE FOE DMBTT"MJDF JODMVEF(SFFUJOH

    FOE BMJDF"MJDFOFX BMJDFIFMMPIFMMP JODMVEFͨ͠"MJDFΫϥεʹ ͸IFMMPϝιου͕௥Ճ͞Ε ·͢ɻ ϞδϡʔϧΛఆٛ͠·͢ɻ Ϟδϡʔϧʹ͸ϝιουΛ ఆٛ͠·͢ɻ
  14. .PEVMF NPEVMF(SFFUJOH EFGIFMMP QVUT)FMMP FOE FOE DMBTT"MJDF JODMVEF(SFFUJOH FOE DMBTT"MJDF

    EFGIFMMP QVUT)FMMP FOE FOE JODMVEFͨ͠"MJDFΫϥε͸ӈͷΑ͏ʹॻ͍ͨ͜ͱͱಉ͡ʹ ͳΓ·͢ɻ ಉ͡
  15. .PEVMF ෳ਺ͷΫϥεͰಉ͡ϝιουΛར༻͍ͨ͠ͱ͖ʹNPEVMF Λ࢖͏ͱॏෳΛͳͤ͘ΔͷͰศརͰ͢ɻ NPEVMF(SFFUJOH EFGIFMMP QVUT)FMMP FOE FOE DMBTT"MJDF JODMVEF(SFFUJOH

    FOE BMJDF"MJDFOFX BMJDFIFMMPIFMMP DMBTT#PC JODMVEF(SFFUJOH FOE CPC#PCOFX CPCIFMMPIFMMP ΋͠΋IFMMPϝιουͰදࣔ͢ Δ)FMMPΛ͜Μʹͪ͸ʹม͍͑ͨ৔ ߹͸͜ͷϞδϡʔϧ͚ͩมߋ͢Ε͹ྑ͍
  16. .PEVMFԋश໰୊4ղ౴ ԋश໰୊Ͱॻ͍ͨ(SFFUJOH ϞδϡʔϧΛผͷϑΝΠϧ΁อଘ͍ͯͩ͘͠͞ɻSFRVJSFจΛ ࢖ͬͯಡΈࠐΈɺಉ༷ͷಈ࡞Λ͢ΔίʔυΛॻ͍͍ͯͩ͘͞ɻ SFRVJSFNPEVMF@HSFFUJOH DMBTT"MJDF JODMVEF(SFFUJOH FOE BMJDF"MJDFOFX BMJDFIJIJ

    NPEVMF(SFFUJOH EFGIJ QVUTIJ FOE FOE BMJDFSC NPEVMF@HSFFUJOHSC ˞ී௨͸͜Μͳ௕͍ϑΝΠϧ໊Λ෇ ͚ͣʹNPEVMFSCͱ͠·͢ɻ͜͜ Ͱ͸ϑΝΠϧ໊ͱϞδϡʔϧ໊Λ෼ ͚ͯઆ໌͔ͨͬͨ͠ͷͰɾɾɾ ˢSFRVJSF͸ϑΝΠϧ໊ ˢJODMVEF͸Ϟδϡʔϧ໊ SVCZBMJDFSC