A Ruby Programming Episode: Ruby is my glasses, my stationery, and my language

941b170e5a115a295cccb5f5cdf0a800?s=47 Koji SHIMADA
November 24, 2017

A Ruby Programming Episode: Ruby is my glasses, my stationery, and my language

福岡Ruby会議02での発表資料です。
http://regional.rubykaigi.org/fukuoka02/

941b170e5a115a295cccb5f5cdf0a800?s=128

Koji SHIMADA

November 24, 2017
Tweet

Transcript

  1. 26.
  2. 28.

    string = '01234' n = 2 string[0, n] #=> `01`

    string[1, n] #=> `12` string[2, n] #=> `23` n = 3 string[0, n] #=> `012` string[1, n] #=> `123` string[2, n] #=> `234` ఴࣈΛҠಈͤ͞ͳ͕Β෦෼จࣈྻΛूΊ͍͚ͯ͹͍͍ʁ
  3. 30.

    def slices(n) slices = [] 0.upto(@string.length-n) do |i| slices <<

    @string[i, n] end slices end Ҋ̍ จࣈྻ಺ͷ֤จࣈΛͣΒ͠ͳ͕Βɺ௕͞Oͷ࿈ଓ͢Δ෦෼จࣈྻΛ औΓग़͍ͯ͘͠ͱ͍͏໰୊ʁ
  4. 32.

    def slices(n) slices = [] 0.upto(@string.length-n) do |i| slices <<

    @string[i, n] end slices end Ҋ̍ ࠷ޙʹ໭Γ஋ΛΘ͟Θ͟ॻ͔ͳͯ͘͸͍͚ͳ͍ͷ͕ؾʹͳΔ
  5. 33.

    def slices(n) slices = [] 0.upto(@string.length-n) do |i| slices <<

    @string[i, n] end slices end Ҋ̍ ࠷ޙʹ໭Γ஋ΛΘ͟Θ͟ॻ͔ͳͯ͘͸͍͚ͳ͍ͷ͕ؾʹͳΔ &OVNFSBCMFXJUI@PCKFDUΛ࢖͓͏
  6. 35.

    def slices(n) 0.upto(@string.length-n).with_object([]) do |i, slices| slices << @string[i, n]

    end end Ҋ̎ Ϧετ͔ΒҰͭͣͭऔΓग़ͯ͠Ճ޻͠ͳ͕Β ผͷϦετʹೖΕͯΔͷͰ͸
  7. 36.

    def slices(n) 0.upto(@string.length-n).with_object([]) do |i, slices| slices << @string[i, n]

    end end Ҋ̎ Ϧετ͔ΒҰͭͣͭऔΓग़ͯ͠Ճ޻͠ͳ͕Β ผͷϦετʹೖΕͯΔͷͰ͸ &OVNFSBCMFNBQͰྑ͍ͷͰ͸
  8. 38.

    def slices(n) 0.upto(@string.length-n).map do |i| @string[i, n] end end Ҋ̏

    ௕͞Oͷ࿈ଓ͢Δ෦෼จࣈྻΛऔΓग़ͤΔఴࣈΛ ରԠ͢Δ஋ʹม׵͢Δ໰୊ʁ
  9. 40.

    def slices(n) 0.upto(@string.length-n).map do |i| @string[i, n] end end Ҋ̏

    <>ͰΞΫηε͍ͯ͠Δͷ͸ந৅౓͕߹ͬͯͳ͍ؾ͕͢Δ
  10. 41.

    def slices(n) 0.upto(@string.length-n).map do |i| @string[i, n] end end Ҋ̏

    <>ͰΞΫηε͍ͯ͠Δͷ͸ந৅౓͕߹ͬͯͳ͍ؾ͕͢Δ 4USJOHTMJDFʹͯ͠ҙਤΛίʔυͰද໌͠Α͏
  11. 44.

    Ҋ̐ def slices(n) 0.upto(@string.length-n).map do |i| @string.slice(i, n) end end

    ͚ͩ͜͜ίʔυ͚ͩͰ͏·͘ҙਤΛ఻͖͑Ε͍ͯͳͯ͘ؾʹͳΔ
  12. 45.

    Ҋ̐ def slices(n) 0.upto(@string.length-n).map do |i| @string.slice(i, n) end end

    ͚ͩ͜͜ίʔυ͚ͩͰ͏·͘ҙਤΛ఻͖͑Ε͍ͯͳͯ͘ؾʹͳΔ ͍ͬͨΜશ෦TMJDFͯ͠͠·͔ͬͯΒαΠζ͕߹Θͳ͍΋ͷΛ ϑΟϧλ͢ΔɺͩͱͲ͏͔ͳʁ
  13. 48.

    Ҋ̑ def slices(n) 0.upto(@string.length).map {|i| @string.slice(i, n) }.select {|s| s.length

    == n } end ͜͜͸ఴࣈΛશ෦औΔͱ͍͏෩ʹ΋ॻ͚ͦ͏ʁ
  14. 50.

    Ҋ̒ def slices(n) @string.chars.each_index.map {|i| @string.slice(i, n) }.select {|s| s.length

    == n } end TMJDFͷڍಈΛ೺Ѳͯ͠ͳ͍ͱԿͰTFMFDU͠ͳ͍ͱ͍͚ͳ͍͔ Θ͔Βͳ͍ͷͰඍົ͔ͳ͋
  15. 51.

    Ҋ̒ def slices(n) @string.chars.each_index.map {|i| @string.slice(i, n) }.select {|s| s.length

    == n } end TMJDFͷڍಈΛ೺Ѳͯ͠ͳ͍ͱԿͰTFMFDU͠ͳ͍ͱ͍͚ͳ͍͔ Θ͔Βͳ͍ͷͰඍົ͔ͳ͋ ࠷ॳʹTFMFDU͔ͯ͠ΒTMJDF͢ΔͩͱͲ͏ͩΖ͏ʁ
  16. 53.

    Ҋ̓ def slices(n) @string.chars.each_index.select {|i| i + n <= @string.length

    }.map {|i| @string.slice(i, n) } end ͬͪ͜ͷํ͕·ͩTMJDFͷڍಈΛؾʹ͠ͳͯ͘΋ಡΊΔ෼ ϚγͰ͔͢Ͷ͑ɻͰ΋΋͏ͪΐͬͱ͔ͳ͋
  17. 60.

    ࠷ऴҊ class Series def initialize(string) @string = string end def

    slices(n) @string.chars.each_cons(n).map(&:join) end end
  18. 61.
  19. 67.
  20. 85.