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

A Ruby Programming Episode: Ruby is my glasses,...

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

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

Avatar for Koji SHIMADA

Koji SHIMADA

November 24, 2017
Tweet

More Decks by Koji SHIMADA

Other Decks in Programming

Transcript

  1. 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` ఴࣈΛҠಈͤ͞ͳ͕Β෦෼จࣈྻΛूΊ͍͚ͯ͹͍͍ʁ
  2. def slices(n) slices = [] 0.upto(@string.length-n) do |i| slices <<

    @string[i, n] end slices end Ҋ̍ จࣈྻ಺ͷ֤จࣈΛͣΒ͠ͳ͕Βɺ௕͞Oͷ࿈ଓ͢Δ෦෼จࣈྻΛ औΓग़͍ͯ͘͠ͱ͍͏໰୊ʁ
  3. def slices(n) slices = [] 0.upto(@string.length-n) do |i| slices <<

    @string[i, n] end slices end Ҋ̍ ࠷ޙʹ໭Γ஋ΛΘ͟Θ͟ॻ͔ͳͯ͘͸͍͚ͳ͍ͷ͕ؾʹͳΔ
  4. def slices(n) slices = [] 0.upto(@string.length-n) do |i| slices <<

    @string[i, n] end slices end Ҋ̍ ࠷ޙʹ໭Γ஋ΛΘ͟Θ͟ॻ͔ͳͯ͘͸͍͚ͳ͍ͷ͕ؾʹͳΔ &OVNFSBCMFXJUI@PCKFDUΛ࢖͓͏
  5. def slices(n) 0.upto(@string.length-n).with_object([]) do |i, slices| slices << @string[i, n]

    end end Ҋ̎ Ϧετ͔ΒҰͭͣͭऔΓग़ͯ͠Ճ޻͠ͳ͕Β ผͷϦετʹೖΕͯΔͷͰ͸
  6. def slices(n) 0.upto(@string.length-n).with_object([]) do |i, slices| slices << @string[i, n]

    end end Ҋ̎ Ϧετ͔ΒҰͭͣͭऔΓग़ͯ͠Ճ޻͠ͳ͕Β ผͷϦετʹೖΕͯΔͷͰ͸ &OVNFSBCMFNBQͰྑ͍ͷͰ͸
  7. def slices(n) 0.upto(@string.length-n).map do |i| @string[i, n] end end Ҋ̏

    ௕͞Oͷ࿈ଓ͢Δ෦෼จࣈྻΛऔΓग़ͤΔఴࣈΛ ରԠ͢Δ஋ʹม׵͢Δ໰୊ʁ
  8. def slices(n) 0.upto(@string.length-n).map do |i| @string[i, n] end end Ҋ̏

    <>ͰΞΫηε͍ͯ͠Δͷ͸ந৅౓͕߹ͬͯͳ͍ؾ͕͢Δ
  9. def slices(n) 0.upto(@string.length-n).map do |i| @string[i, n] end end Ҋ̏

    <>ͰΞΫηε͍ͯ͠Δͷ͸ந৅౓͕߹ͬͯͳ͍ؾ͕͢Δ 4USJOHTMJDFʹͯ͠ҙਤΛίʔυͰද໌͠Α͏
  10. Ҋ̐ def slices(n) 0.upto(@string.length-n).map do |i| @string.slice(i, n) end end

    ͚ͩ͜͜ίʔυ͚ͩͰ͏·͘ҙਤΛ఻͖͑Ε͍ͯͳͯ͘ؾʹͳΔ
  11. Ҋ̐ def slices(n) 0.upto(@string.length-n).map do |i| @string.slice(i, n) end end

    ͚ͩ͜͜ίʔυ͚ͩͰ͏·͘ҙਤΛ఻͖͑Ε͍ͯͳͯ͘ؾʹͳΔ ͍ͬͨΜશ෦TMJDFͯ͠͠·͔ͬͯΒαΠζ͕߹Θͳ͍΋ͷΛ ϑΟϧλ͢ΔɺͩͱͲ͏͔ͳʁ
  12. Ҋ̑ def slices(n) 0.upto(@string.length).map {|i| @string.slice(i, n) }.select {|s| s.length

    == n } end ͜͜͸ఴࣈΛશ෦औΔͱ͍͏෩ʹ΋ॻ͚ͦ͏ʁ
  13. Ҋ̒ def slices(n) @string.chars.each_index.map {|i| @string.slice(i, n) }.select {|s| s.length

    == n } end TMJDFͷڍಈΛ೺Ѳͯ͠ͳ͍ͱԿͰTFMFDU͠ͳ͍ͱ͍͚ͳ͍͔ Θ͔Βͳ͍ͷͰඍົ͔ͳ͋
  14. Ҋ̒ def slices(n) @string.chars.each_index.map {|i| @string.slice(i, n) }.select {|s| s.length

    == n } end TMJDFͷڍಈΛ೺Ѳͯ͠ͳ͍ͱԿͰTFMFDU͠ͳ͍ͱ͍͚ͳ͍͔ Θ͔Βͳ͍ͷͰඍົ͔ͳ͋ ࠷ॳʹTFMFDU͔ͯ͠ΒTMJDF͢ΔͩͱͲ͏ͩΖ͏ʁ
  15. Ҋ̓ def slices(n) @string.chars.each_index.select {|i| i + n <= @string.length

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

    slices(n) @string.chars.each_cons(n).map(&:join) end end