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

Ruby講義第8回

 Ruby講義第8回

演習の回
破壊的メソッド

Kuniaki IGARASHI

May 31, 2012
Tweet

More Decks by Kuniaki IGARASHI

Other Decks in Technology

Transcript

  1. "SSBZTPSU@CZϝιου BSSBZ<  > SFTVMUBSSBZTPSU@CZEPcJc J FOE QSFTVMU "SSBZͷத਎ΛϒϩοΫͷධՁ݁ՌͰฒͼସ͑ <

     > αϯϓϧίʔυ ճ໨JධՁ݁Ռ ճ໨JධՁ݁Ռ ճ໨JධՁ݁Ռ ࣮ߦ݁Ռ
  2. 8JLJQFEJBΞΫηε਺σʔλ KBC$ KBC$$#$"'$#'$#%$"$"$$"$$"' $#%$#$"$"'$#'$#%$"'$#'$#% $"'$#%$"$$"'$#%$"$$"$"%$"' $#'$#%$"'$#%$" KBC&&#&&&#&&""&% "&#&&"#&'&"&$"$@ &$#&@&#& KBC&"&"&"%&"

    &&"&"&"&&##&% &#@&%&#$&#'&"#&## &&"%&#&"&"' &&"& KBC&"&"&"%&" &&"&"&"&&##&% &#@&"&&#" &"&"&"%&" &&"&"&"&"'&'#$'   ͍ͬͯ͏σʔλ͕਺ेສߦ
  3. 8JLJQFEJBͷΞΫηε਺ղੳ FODPEJOHVUG SFRVJSFDHJ pMFOBNFQBHFDPVOUTKBUYU pMF'JMFPQFO pMFOBNF S65' MJTU<> XIJMFUFYUpMFHFUT CFHJO

    OFYUVOMFTTUFYUd?KB EBUBUFYUTQMJU I\UJUMF$(*VOFTDBQF EBUB<> DPVOUEBUB<>^ MJTUQVTII SFTDVF&YDFQUJPOF QF FOE FOE pMFDMPTF DPVOUॱʹιʔτ SFTVMUMJTUTPSU@CZEPcJc J<DPVOU>UP@J FOE τοϓදࣔ SFTVMUSFWFSTFpSTU  FBDIEPcJc QVUTJ FOE
  4. ௐ΂ͨͱ͜ΖɺҎԼͷΑ͏ʹͳͬͯ·ͨ͠ XIJMFUFYUpMFHFUT CFHJO OFYUVOMFTTUFYUd?KB EBUBUFYUTQMJU I\UJUMF$(*VOFTDBQF EBUB<> DPVOUEBUB<>^ MJTUQVTII SFTDVF&YDFQUJPOF

    QF FOE FOE จࣈίʔυม׵ʹΪϒΞοϓ͢Δσʔλ͸ɺҎԼͷྫ֎͕ൃੜ "SHVNFOU&SSPSJOWBMJECZUFTFRVFODFJO65' ྫ֎͕ൃੜͨ͠৔߹ɺ͜ͷॲཧ͕ߦΘΕΔɻ ั·͑ͨྫ֎͕ม਺FʹೖΓɺͦΕΛද͍ࣔͯ͠Δɻ ௐࠪͷ݁ՌɺͲ͏΍Β8JLJQFEJBαΠτͰެ։͞Ε͍ͯΔΞΫηε਺σʔλʹ͸ɺͱ ͖Ͳ͖จࣈίʔυม׵ʹΪϒΞοϓ͢Δσʔλͷߦ͕͋ΔΑ͏Ͱ͢ɻ ͜͜Ͱ͸ɺQFͷߦΛQFͱίϝϯτΞ΢τ͢Δ͜ͱʹ͠·͢ɻ ʢม׵Ͱ͖ͳ͍σʔλͷߦ͸ແࢹ͢Δ͜ͱʹͳΓ·͢ɻʣ ˞ਖ਼֬ʹղੳ͍ͨ͠৔߹͸΋͏গ͕͠Μ͹Δํ๏΋͋Γ·͕͢ɺࠓճ͸਺ेສߦͷ͏ ͪͷ਺ߦͳͷͰແࢹͰ͖Δͱߟ͑·͢ɻ ˞ͦΕ͸ͦΕͱͯ͠ɺ഑ͬͨσʔλϑΝΠϧ΋Ұ෦໰୊͕͋ͬͨͷͰ࠶഑෍͠·͢ɻ
  5. 8JLJQFEJBͷΞΫηε਺ղੳ FODPEJOHVUG SFRVJSFDHJ pMFOBNFQBHFDPVOUTKBUYU pMF'JMFPQFO pMFOBNF S65' MJTU<> XIJMFUFYUpMFHFUT CFHJO

    OFYUVOMFTTUFYUd?KB EBUBUFYUTQMJU I\UJUMF$(*VOFTDBQF EBUB<> DPVOUEBUB<>^ MJTUQVTII SFTDVF&YDFQUJPOF QF FOE FOE pMFDMPTF DPVOUॱʹιʔτ SFTVMUMJTUTPSU@CZEPcJc J<DPVOU>UP@J FOE τοϓදࣔ SFTVMUSFWFSTFpSTU  FBDIEPcJc QVUTJ FOE
  6. 8JLJQFEJBͷΞΫηε਺ղੳԋश લड़ͷίʔυΛ࣮ߦ͍ͯͩ͘͠͞ɻSCϑΝΠϧͱಉ͡ϑΥϧμʹ QBHFDPVOUTKBUYUΛஔ͍࣮ͯߦ͍ͯͩ͘͠͞ɻ IUUQCJUMZXQEBUB͔Βμ΢ϯϩʔυͰ͖·͢ɻ[JQѹॖͯ͋͠ Γ·͢ɻϒϥ΢βͰμ΢ϯϩʔυͯ͠ղౚ͢Δ͔ɺ-JOVYͷਓ͸Լهͷ˞ͷ ํ๏Ͱ΋͔·͍·ͤΜɻ ʢ࣮ߦ͕࣌ؒ௕͍ͷͰɺ࣮ࡍͷσʔλϑΝΠϧ͔Β೔ຊޠ෦෼͚ͩൈ͖ग़ͯ͠ খͯ͋͘͞͠Γ·͢ɻʣ چɿI\UJUMF$(*VOFTDBQF EBUB<>

    DPVOUEBUB<>^ ৽ɿI\UJUMF$(*VOFTDBQF EBUB<> FODPEF DQ DPVOUEBUB<>^ ˞8JOͰग़ྗ͕aV'%%aVaV&aV&ͱͳΔਓ͸ߦ໨෇ۙΛҎ ԼͷίʔυͰࠩ͠ସ͍͑ͯͩ͘͞ɻDQ͸จࣈίʔυͰɺTIJGU@KJTͷXJO൛Ͱ͢ɻ ˞-JOVYͰͷμ΢ϯϩʔυํ๏ͷྫ ୺຤Λ։͍ͯιʔείʔυ͕͋ΔϑΥϧμͰҎԼΛ࣮ߦɻXHFU͸TIFMMͰ࢖͑Δμ΢ϯ ϩʔμʔͰ͢ɻVO[JQ͸[JQղౚίϚϯυͰ͢ɻ XHFUIUUQCJUMZXQEBUB VO[JQXQEBUB
  7. BCD ࣮ߦલ ΦϒδΣΫτ "#$ BCDVQDBTF BCDVQDBTF ࣮ߦޙ ΦϒδΣΫτ VQDBTF ΦϒδΣΫτ

    ͕ม਎͢Δ BCD ࣮ߦલ ΦϒδΣΫτ "#$ ࣮ߦޙ ৽ΦϒδΣΫτ VQDBTF BCD چΦϒδΣΫτ ม׵ͨ͠ ৽͍͠ ΦϒδΣΫτ ͕ෳ੡͞ΕΔ
  8. VQDBTFΛ࢖͏ίʔυ BBCD CBVQDBTF QVUTC BCD "#$ C BCDVQDBTF BCD ࣮ߦલ

    ΦϒδΣΫτ "#$ ࣮ߦޙ VQDBTF ม׵ͨ͠ ৽͍͠ ΦϒδΣΫτ ͕ෳ੡͞ΕΔ BCD چΦϒδΣΫτ B BCD B ৽ΦϒδΣΫτ ෳ੡͞ΕͨΦϒδΣΫτΛ ผͷม਺ʹೖΕΔ
  9. BCD ࣮ߦલ ΦϒδΣΫτ "#$ BCDVQDBTF ࣮ߦޙ ΦϒδΣΫτ VQDBTF ΦϒδΣΫτ ͕ม਎͢Δ

    ഁյతϝιου VQDBTFͷΑ͏ʹΦϒδΣΫτͷ಺༰Λ มߋ͢Δ΋ͷΛʮഁյతϝιουʯͱݴ͍·͢ ϝιου໊຤ඌʹ͕͍ͭͨΒഁյతϝιου ʢͨͩ͠ɺഁյతϝιουͰ΋͕෇͔ͳ͍΋ͷ΋͋Δʣ
  10. ԋश໰୊ "SSBZΦϒδΣΫτͷཁૉ͕ح਺ͷ߲໨Λશͯ଍͢ ίʔυΛॻ͍͍ͯͩ͘͞ɻ ྫ͑͹BSSBZ<    >ͷ৔߹ɺ  

      ͕දࣔ͞ΕΕ͹0,Ͱ͢ɻ ώϯτɿح਺͔Ͳ͏͔ௐ΂Δͷ͸'JYOVNPEE ϝιου PEE ˠUSVF PEE ˠGBMTF ͪͳΈʹۮ਺͔ௐ΂Δͷ͸'JYOVNFWFO ϝιουͰ͢ɻ
  11. ԋश໰୊ "SSBZΦϒδΣΫτͷཁૉ͕ح਺ͷ߲໨Λશͯ଍͢ίʔυΛॻ͍͍ͯͩ͘͞ɻ ྫ͑͹BSSBZ<    >ͷ৔߹ɺ   ͕දࣔ͞ΕΕ͹

    0,Ͱ͢ɻ ώϯτɿح਺͔Ͳ͏͔ௐ΂Δͷ͸'JYOVNPEE ϝιου PEE ˠUSVF PEE ˠGBMTF ͪͳΈʹۮ਺͔ௐ΂Δͷ͸'JYOVNFWFO ϝιουͰ͢ɻ BSSBZ<    > TVN BSSBZFBDIEPcJc TVN JJGJPEE FOE QTVN ߦͰ͔ͬ͜Α͘ॻ͘͜ͱ΋Ͱ͖·͢ɻ <    >TFMFDU\cJcJPEE ^JOKFDU 
  12. ԋश໰୊ <\UJUMFB QSJDF^ \UJUMFC QSJDF^ \UJUMFD QSJDF^> ͱ͍͏ΦϒδΣΫτ͔ΒɺҎԼͷΑ͏ͳΦϒδΣΫτΛ࡞ΔίʔυΛॻ͍͍ͯͩ͘͞ɻ <\UJUMFB QSJDF

    TQFDJBM-PXQSJDF^ \UJUMFC QSJDF^ \UJUMFD QSJDF TQFDJBM-PXQSJDF^> JUFNT<\UJUMFB QSJDF^ \UJUMFC QSJDF^ \UJUMFD QSJDF^> JUFNTFBDIEPcJUFNc JUFN<TQFDJBM>-PXQSJDFJGJUFN<QSJDF> FOE QJUFNT <\UJUMFB QSJDF TQFDJBM-PXQSJDF^ \UJUMFC QSJDF^ \UJUMFD QSJDF TQFDJBM-PXQSJDF^>
  13. ԋश໰୊ ͋Δ"SSBZΦϒδΣΫτ͕༩͑ΒΕͨͱ͖ɺ ྫ ͑͹<  > ͦͷதͰҎԼͷ਺ࣈ͕͍ͭ͋͘ Δ͔දࣔ͢ΔίʔυΛॻ͍͍ͯͩ͘͞ɻ BSSBZ< 

    > DPVOU BSSBZFBDIEPcJc DPVOU JGJ FOE QDPVOU BSSBZ<  > QBSSBZDPVOU\cJcJ^ ผղɹDPVOUϝιουʹϒϩοΫΛ౉͢͜ͱͰΧ΢ϯτͰ͖·͢ɻ ϒϩοΫ͸EPFOEͷมΘΓʹ\^Ͱॻ͘͜ͱ΋Ͱ͖·͢ɻ
  14. ԋश໰୊ )BTIͷόϦϡʔͷதʹͱ͍͏จࣈؚ͕·ΕΔͱ͖ʹɺͦͷό ϦϡʔΛେจࣈʹม׵ͨ͠)BTIΛ࡞ΔίʔυΛॻ͍͍ͯͩ͘͞ɻ ྫ͑͹ɺ \BMJDFZFBI CPCZP MJOEBXPX^ ˢͱ͍͏)BTIΛˣʹͰ͖Ε͹0,Ͱ͢ɻ \BMJDFZFBI CPC:0

    MJOEB808^ I\BMJDFZFBI CPCZP MJOEBXPX^ IFBDIEPcLFZ WBMVFc I<LFZ>WBMVFVQDBTFJGWBMVFd FOE QI \BMJDFZFBI CPC:0 MJOEB808^
  15. ԋश໰୊ BMJDF CPC DBSPMͷࢼݧͷ఺਺͕࣍ͷΑ͏ʹ)BTIʹ֨ೲ͞Ε͍ͯ·͢ɻ BMJDF\FOHMJTI NBUI IJTUPSZ^ CPC\FOHMJTI NBUI IJTUPSZ^

    DBSPM\FOHMJTI NBUI IJTUPSZ^ ֤Պ໨ͷฏۉ఺Λग़ྗ͍ͯͩ͘͠͞ BMJDF\FOHMJTI NBUI IJTUPSZ^ CPC\FOHMJTI NBUI IJTUPSZ^ DBSPM\FOHMJTI NBUI IJTUPSZ^ QVUTFOHMJTIBWFSBHF  BMJDF<FOHMJTI> CPC<FOHMJTI> DBSPM<FOHMJTI>  QVUTNBUIBWFSBHF  BMJDF<NBUI> CPC<NBUI> DBSPM<NBUI>  QVUTIJTUPSZBWFSBHF  BMJDF<IJTUPSZ> CPC<IJTUPSZ> DBSPM<IJTUPSZ> 
  16. ԋश໰୊ʲ্ڃʳ4ղ ͋ΔςΩετϑΝΠϧ͔ΒɺBͱ͍͏จࣈྻΛؚΉߦͩ ͚Λநग़ͨ͠ผͷςΩετϑΝΠϧΛ࡞͍ͬͯͩ͘͞ɻ ྫɿ BMJDF CPC DBSPM BMJDF DBSPM ϑΝΠϧ͔ΒಡΈࠐΈ

    JODMVEFE<>ग़ྗ༻σʔλΛ֨ೲ͢Δ"SSBZ JO@pMFOBNFJOUYU 'JMFPQFO JO@pMFOBNF S65' EPcJO@pMFc XIJMFUFYUJO@pMFHFUT JODMVEFEUFYUJGUFYUdB FOE FOE ϑΝΠϧ΁ॻ͖ࠐΈ PVU@pMFOBNFPVUUYU 'JMFPQFO PVU@pMFOBNF X65' EPcPVU@pMFc JODMVEFEFBDIEPcJc PVU@pMFQVUTJ FOE FOE ͭͷϑΝΠϧΛPQFO͠·͢ɻ ग़ྗଆ͸X65' ॻ͖ࠐΈ Ͱ͢ɻ ·ͨɺ'JMFPQFOʹϒϩοΫΛ ౉͢ͱϒϩοΫ׬ྃ࣌ʹࣗಈͰ DMPTF͠·͢
  17. ԋश໰୊ʲ্ڃʳ4ղ ͋ΔςΩετϑΝΠϧ͔ΒɺBͱ͍͏จࣈྻΛؚΉߦͩ ͚Λநग़ͨ͠ผͷςΩετϑΝΠϧΛ࡞͍ͬͯͩ͘͞ɻ ྫɿ BMJDF CPC DBSPM BMJDF DBSPM JO@pMFOBNFJOUYU

    PVU@pMFOBNFPVUUYU 'JMFPQFO JO@pMFOBNF S65' EPcJO@pMFc 'JMFPQFO PVU@pMFOBNF X65' EPcPVU@pMFc XIJMFUFYUJO@pMFHFUT PVU@pMFQVUTUFYUJGUFYUdB FOE FOE FOE JOͱPVUΛಉ࣌ʹ։͘ॻ͖ํͰ͢ɻ݁Ռ֨ೲ༻ͷ"SSBZ͕ෆཁʹͳΔͷͰ εοΩϦͰ͢ɻ