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

%q is for Quine

Koichi ITO
December 19, 2022

%q is for Quine

Koichi ITO

December 19, 2022
Tweet

More Decks by Koichi ITO

Other Decks in Programming

Transcript

  1. 2VJOFجຊจܕͷಡΈॱ %q is for Quine !LPJD

  2. w ௒ઈٕ޼ϓϩάϥϛϯά΁ͷ໳ w ଏੈͷརӹͱؔ܎ͷͳ͍͸ͳ͠ ࠓ೔ͷ͸ͳ͠

  3. 53*$,࡞඼ΑΓ ࢲͷ޷͖ͳٕ޼࡞඼ $ruby.is_a?(Object){|oriented| language} IUUQTHJUIVCDPNUSJDUSJDLCMPCNBTUFSVOBLFOUSZSC VTBL࡞

  4. 53*$,ۚ৆ ௒ઈٕ޼࡞඼ UPNQOH࡞ IUUQTUXJUUFSDPNUPNQOHTUBUVT

  5. 3VCZ,BJHJͷ ห౰εϙϯαʔΑΓ &4. *ODͷΞεΩʔΞʔτ࡞඼ GLJOP࡞ IUUQTHJTUHJUIVCDPNGLJOPDFCFFBFBBBBGDFpMF[email protected]SC w FWBM X 

     ͱ͍͏ఆੴ͕࢖ΘΕ͍ͯΔ w ͜͏͍ٕͬͨ޼ͷجૅΛֶ΂Δॻ੶͸͋Δʁ
  6. ٕज़ධ࿦ࣾ

  7. w ιϑτ΢ΣΞ։ൃͷϓϩΛ໨ࢦͨ͢Ί ͷॻ੶Ͱ͸ͳ͍ w ϓϩάϥϛϯάͷ࣮༻ੑͱ͍͏ࡶ೦Λ ๨ΕΔ͜ͱͰɺϓϩάϥϛϯάຊདྷͷ ָ͠͞Λ࠶ൃݟ͢Δ͖͔͚ͬʹͳΔ w ஶऀͷ3VCZίϛολʔNBNF͞Μ͸ 8PSME/P*0$$$1MBZFS

    ͋ͳͨͷ஌Βͳ͍௒ઈٕ޼ϓϩάϥϛϯάͷੈք
  8. Ͱ͖Δ͚ͩฏқʹ Ͱ͖Δ͚ͩ೉ղʹ ࣮༻తʹ ඇ࣮༻తʹ ௒ઈٕ޼ϓϩάϥϛϯά ͓࢓ࣄͷϓϩάϥϛϯά ͋ͳͨͷ஌Βͳ͍௒ઈٕ޼ϓϩάϥϛϯάͷੈք

  9. 2VJOF

  10. w ࣗ෼ࣗ਎ΛධՁͯ͠ɺࣗ෼ࣗ਎Λग़ྗ͢Δ ϓϩάϥϜͰ͢ 2VJOFͱ͸ʁ % ruby -e 'eval s=%q(puts %(eval

    s=%q(#{s})))' eval s=%q(puts %(eval s=%q(#{s})))
  11. ࠓճ୊ࡐͱ͢Δجຊจܕ eval s=%q(puts %(eval s=%q(#{s})))

  12. lͲͷΑ͏ʹಈ͔͘ɺ ߟ͑ͯΈ͍ͯͩ͘͞ IUUQTUXJUUFSDPNNPSJNPSJIPHFTUBUVT ʰ͋ͳͨͷ஌Βͳ͍௒ઈٕ޼ϓϩάϥϛϯάͷੈքʱQΑΓ ʰͲ͏ॻ͘ʱͳΒ͵ʰͲ͏ಡΉʱͱ͍ͬͨײ͡Ͱɺ ͍ͪಡऀͱͯ͠ߟ͑ͯΈ·ͨ͠

  13. FWBMͷલʹҾ਺ͷ୅ೖ͕ߦΘΕ·͢ ࠷ॳͷΩʔϙΠϯτ eval s=%q(puts %(eval s=%q(#{s})))

  14. ୅ೖ͞Ε͍ͯΔͷ͸Rͱ͍͏γϯάϧΫΥʔτ૬౰ͷૉ ๿ͳจࣈྻϦςϥϧͰ͢ɻ͜͜Ͱจࣈྻʹࣜల։͕ॻ͔Ε ͍ͯ·͕͢ɺγϯάϧΫΥʔτͳͷͰࣜల։͞Εͣɺͦͷ ··ͷจࣈྻͱͯ͠ѻΘΕ·͢ɻ ೉͍͠ͷ͸จࣈྻ eval s=%q(puts %(eval s=%q(#{s})))

  15. ه๏Λ ಡΈସ͑Δ IUUQTUXJUUFSDPNNPSJNPSJIPHFTUBUVT ௒ઈٕ޼ϓϩάϥϚʔޚ༻ୡͬΆ͍ ه๏Λ Α͘ݟΔޓ׵ΫΥʔτʹಡΈସ͑ͯΈ·͠ΐ͏ RJTGPS2VJOF

  16. ͔͜͜Βه๏׳Ε͍ͯ͠ͳ͍ਓͷͨΊศٓతʹɺ͜ͷ จࣈྻΛಉٛͷΫΥʔτه๏ͰಡΈସ͑ͯΈ·͠ΐ͏ɻ ·ͣҰ൪֎ଆͷRه๏ΛγϯάϧΫΥʔτʹ͠·͢ɻ RͷಡΈସ͑ -eval s=%q(puts %(eval s=%q(#{s}))) +eval s='puts

    %(eval s=%q(#{s}))' গ͠ಡΈ΍͘͢ͳͬͨʁ
  17. ه๏ΛμϒϧΫΥʔτʹ͠·͢ɻ ه๏ͷಡΈସ͑ -eval s='puts %(eval s=%q(#{s}))' +eval s='puts "eval s=%q(#{s})"'

    ΋͏গ͠ಡΈ΍͘͢ͳͬͨʁ
  18. ΤεέʔϓͰಡΈͮΒ͘ͳΔͷͰ͕͢ɺ಺ଆ ͷR΋γϯάϧΫΥʔτʹஔ͖׵͑·͢ɻ RͷಡΈସ͑ -eval s='puts "eval s=%q(#{s})"' +eval s='puts "eval

    s=\'#{s}\'"'  ͸͍ͭ͘ॏͶͯ΋ΤεέʔϓෆཁͳͨΊ ௒ઈٕ޼ϓϩάϥϚʔޚ༻ୡΒ͍͠
  19. શମͱͯ͠͸γϯάϧΫΥʔτͰғ·Εͨɺࣜల ։ΛධՁ͠ͳ͍ͨͩͷจࣈྻͰ͢ɻ܁Γฦ͠ʹͳ Γ·͕͢ɺ͜ͷஈ֊Ͱ͸\T^͸ධՁ͞Ε·ͤΜɻ ಡΈସ͑ͨ͠จࣈྻΛݟͯΈ·͠ΐ͏ eval s='puts "eval s=\'#{s}\'"'

  20. ͜͜ͰϩʔΧϧม਺Tʹ͸γϯάϧΫΥʔτ Ϧςϥϧͷจࣈྻ͕୅ೖ͞Ε·͢ɻ 2VJOFͷશମ૾ʹ໭͍͖ͬͯ·͠ΐ͏ eval s='puts "eval s=\'#{s}\'"'

  21. ୅ೖޙʹFWBMʹ୅ೖͨ͠จࣈྻΛධՁ͠· ͢ɻͳ͓͜ͷ࣌఺ͰϩʔΧϧม਺Tʹ͸จࣈ ྻ͕୅ೖ͞Ε͍ͯ·͢ɻ Ҿ਺ΛFWBMͰධՁ͢Δ eval s='puts "eval s=\'#{s}\'"'

  22. FWBM͞ΕΔ಺༰ͱͯ͠ɺ͜͜·Ͱݟ͖ͯͨจ ࣈྻΛจʹͯ͠ݟͯΈ·͠ΐ͏ɻ FWBMͰධՁ͞ΕΔจ eval s='puts "eval s=\'#{s}\'"'

  23. QVUT͢Δ಺༰͸͜͜ͰධՁ͞ΕΔμϒϧ ΫΥʔτจࣈྻͰ͢ɻ ͯࣜ͞ల։\T^ʹೖΔ஋͸ʁ ඪ४ग़ྗ͢Δจࣈྻͷ૊Έཱͯ eval s='puts "eval s=\'#{s}\'"'

  24. μϒϧΫΥʔτ͸ࣜల։͞ΕΔͷͰɺ\T^ʹ͸FWBMධ Ձલʹจࣈྻ୅ೖ͍ͯ͠ΔϩʔΧϧม਺T͕ల։͞Ε· ͢ɻϩʔΧϧม਺Tʹೖ͍ͬͯΔจࣈྻΛ࠶ܝ͠·͢ɻ ϩʔΧϧม਺Tͷจࣈྻ͸ίϨ eval s='puts "eval s=\'#{s}\'"'

  25. ͜ͷจࣈྻΛࣜల։ʹೖΕͯΈ·͠ΐ͏ɻ ˞ͨͩ͠μϒϧΫΥʔτจࣈྻ΁ͷΤεέʔϓ͕ෆ ଍͍ͯ͠Δ͜ͷίʔυ͸ಈ͖·ͤΜɻ࣍ϖʔδͰ ϩʔΧϧม਺TʹΑΔࣜల։ -"eval s=\'#{s}\'" +"eval s=\'puts "eval s=\'#{s}\'"\'"

  26. ࣜͷల։ޙͳͷͰɺҙຯ߹͍తʹγϯάϧ ΫΥʔτʹஔ͖׵͑·͢ɻ ϩʔΧϧม਺TʹΑΔࣜల։ -"eval s=\'puts "eval s=\'#{s}\'"\'" +'eval s=\'puts "eval

    s=\'#{s}\'"\''
  27. ϓϩάϥϜࣗ਎Λ࠶ؼతʹग़ྗ͍ͯ͠Δ͜ͱ͕Θ͔Γ·͢ɻ࣍ ϖʔδ͔ΒɺศٓతʹΫΥʔτஔ׵͍ͯͨ͠෦෼Λɺຊདྷͷ ه๏ʹ໭͠·͠ΐ͏ɻ QVUTग़ྗ͞ΕΔจࣈྻͷݪܕ puts 'eval s=\'puts "eval s=\'#{s}\'"\''

  28. ·ͣ͸಺ଆͷγϯάϧΫΥʔτΛRʹ͠· ͢ɻ ಡΈସ͑ΛRʹ໭͢ -"eval s=\'puts "eval s=\'#{s}\'"\'" +"eval s=\'puts "eval

    s=%q(#{s})"\'"
  29. ࣍ʹμϒϧΫΥʔτΛه๏ʹ͠·͢ɻ ಡΈସ͑Λه๏ʹ໭͢ -"eval s=\'puts "eval s=%q(#{s})"\'" +"eval s=\'puts %(eval s=%q(#{s}))\'"

  30. ࠷ޙʹ֎ଆͷγϯάϧΫΥʔτΛRʹ͠·͢ɻ ه๏͸͍ͭ͘ॏͶͯ΋όοΫεϥογϡෆཁͱ͍͏ ͜ͱͰɺ2VJOFͷΠσΟΦϜͱͳ͍ͬͯΔΑ͏Ͱ͢ɻ ಡΈସ͑ΛRʹ໭͢ -"eval s=\'puts %(eval s=%q(#{s}))\'" +"eval s=%q(puts

    %(eval s=%q(#{s})))" େࣄͳ͜ͱͳͷͰ΋͏Ұ౓
  31. ୊ࡐͷ2VJOFΛ࠶ܝ eval s=%q(puts %(eval s=%q(#{s}))) 2VJOFͷίʔυϦʔςΟϯά͕Ұप͠·ͨ͠ɻ ධՁ͞ΕΔಡΈॱͰ໨͕ಈ͘Α͏ʹͳΓ·͔ͨ͠ʁ લϖʔδ·Ͱݟ͖ͯͨจࣈྻ͸࠷ॳʹݟͨ 2VJOFͦͷ΋ͷͰ͢ɻ

  32. w SVCZίϚϯυΛ࣮ߦͯ͠ɺ୊ࡐͷجຊจ ܕΛ࣮ߦͯ͠Έ·͠ΐ͏ɻ 2VJOFͷ࣮ߦ % ruby -e 'eval s=%q(puts %(eval

    s=%q(#{s})))' eval s=%q(puts %(eval s=%q(#{s})))
  33. w ࣗ෼ࣗ਎ͷग़ྗΛ࠶౓ධՁͨ݁͠Ռ΋ࣗ෼ ࣗ਎Ͱ͢ 2VJOFͷ࣮ߦ % ruby -e 'eval s=%q(puts %(eval

    s=%q(#{s})))' \ | ruby eval s=%q(puts %(eval s=%q(#{s})))
  34. w ࣗ෼ࣗ਎ͷग़ྗΛ࠶ʑ౓ධՁͨ݁͠Ռ΋ࣗ ෼ࣗ਎Ͱ͢ 2VJOFͷ࣮ߦ % ruby -e 'eval s=%q(puts %(eval

    s=%q(#{s})))' \ | ruby | ruby eval s=%q(puts %(eval s=%q(#{s})))
  35. w ࣗ෼ࣗ਎ͷग़ྗΛ࠶ʑʑ౓ධՁͨ݁͠Ռ΋ ࣗ෼ࣗ਎Ͱ͢ 2VJOFͷ࣮ߦ % ruby -e 'eval s=%q(puts %(eval

    s=%q(#{s})))' \ | ruby | ruby | ruby eval s=%q(puts %(eval s=%q(#{s})))
  36. ࠓճ୊ࡐͱͨ͠جຊจܕ eval s=%q(puts %(eval s=%q(#{s}))) ͜ͷ2VJOFͷઆ໌͸͜͜·ͰͰ͢

  37. Կͷ໾ʹ΋ཱͨͳ͍ ϓϩάϥϜΛָ͠ΉจԽ IUUQTXXXZPVUVCFDPNXBUDI W5#ON((VV

  38. l ੒ख़ͨ͠จԽɺྫ͑͹ԻָͰ΋ָේΛٯ͞ʹͯ͠ ΋஄͘͜ͱ͕Ͱ͖ΔԻָͱ͔ͦ͏͍͏΋ͷ͕͋ͬ ͯɺ தུ ੒ख़ͨ͠จԽʹ͸͜͏͍͓ͬͨ༡ͼ ͕ൃల͍ͯͯ͠ɺϓϩάϥϛϯάͱ͍͏෼໺΋ ੒ख़ͨ͠จԽʹͳ͍ͬͯΔͱ͍͑ΔΜͰ͔͢Ͷ !LJOBCB

  39. w ஶऀͷԕ౻ NBNF ͞Μ͸ 8PSME/P*0$$$1MBZFS w ୭͕ॻ͍ͨॻ੶ɺॏཁ w ਺ʑͷٕ޼͕঺հ͞Ε͍ͯΔ w

    ϓϩάϥϛϯάͷਂ෵΍७ਮͳ ָ͠͞Λࢥ͍ฦ͖͔͚ͬ͢ʹͳ Δ͔΋ ͋ͳͨͷ஌Βͳ͍௒ઈٕ޼ϓϩάϥϛϯάͷੈք
  40. % cat esm_bento.rb | tee /dev/stderr | ruby | grep

    --color . eval( %w( put s(([ 2*7, 2*31 ,1,3 *3*5 ,2*2 *2*2 *3]. map{ |c|c .to_ s(5*7 ).gs ub("1 "," \s") .upca se}. zip( [2*5* 61*1 801, 2*5*3 931, 811* 1871, 2*11 *350 27,7* 11*1 259* 20389 *764 171].map{|c|c.to_s(2*2*3*3)}))*""))*"") Enjoy Ruby with Agile Development https://gist.github.com/fkino/c4e73594b0e9eaea9621a273aa1fc6e8