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

○○からRubyへ / #toyamark

Junichi Ito
November 03, 2019

○○からRubyへ / #toyamark

富山Ruby会議01で使用したスライドです。
https://toyamarb.github.io/toyama-rubykaigi01/

スライド内で使用した動画はこちらにあります。
https://youtu.be/UZE3T5TYAvU

動画内で使用したpokeberuプログラムはこちらです。
https://github.com/JunichiIto/pokeberu

Junichi Ito

November 03, 2019
Tweet

More Decks by Junichi Ito

Other Decks in Technology

Transcript

  1. ˓˓͔ΒRuby΁
    ҏ౻ ३Ұ ʢגࣜձࣾιχοΫΨʔσϯʣ
    2019/11/03 ෋ࢁRubyձٞ01

    View full-size slide

  2. • ҏ౻ ३Ұ / @jnchito
    • RubyϓϩάϥϚʢ2012೥ʙ@ɹɹɹɹɹɹɹɹɹɹɹɹʣ
    • ฌݿݝ੢࿬ࢢࡏॅʢϦϞʔτϫʔΧʔʣ
    • ϒϩά = give IT a try (https://blog.jnito.com)
    • Qiita ૯߹1Ґ (2019೥11݄ݱࡏ)
    • ஶॻɾ༁ॻ
    - ϓϩΛ໨ࢦ͢ਓͷͨΊͷRubyೖ໳ʢٕज़ධ࿦ࣾʣ
    - Everyday Rails - RSpecʹΑΔRailsςετೖ໳ ʢLeanpubʣ
    2
    ͔ΜͨΜʹࣗݾ঺հ
    χγϫΩγʁʁ

    View full-size slide

  3. 3
    ੢࿬ࢢͬͯ͜Μͳͱ͜ΖͰ͢
    ࣗশʮ೔ຊͷ΁ͦʯ

    View full-size slide

  4. 4
    ຊ೔ͷςʔϚ
    • ͔Ε͜Ε7ʙ8೥RubyΛ࢖͖ͬͯͨࢲ
    • Ҏલ͸SIer΍ࣾ಺ΤϯδχΞͱͯ͠Java΍C#ͰϝΠϯͰ࢖͍ͬͯ·ͨ͠
    • ੲͱࠓΛൺ΂ΔͱʮͳΜ͔͍Ζ͍ΖมΘͬͨͳ͋ʯͱࢥ͏ͱ͜Ζ͕ଟʑ͋Γ·͢
    • ࢖͏ݴޠ͕มΘΔͱԿ͕Ͳ͏มΘΔͷ͔ʁ
    • Rubyະܦݧऀ͕ʮ΁ʙɺRubyͬͯͳΜ͔໘നͦ͏ʂʯͱࢥ͑ΔΑ͏ͳ৘ใΛఏڙ͠·͢
    - ϓϩάϥϛϯάܦݧ͸͋ͬͨํ͕͍͍Ͱ͕͢ɺRubyͷ஌ࣝ͸ෆཁͰ͢ʂ

    View full-size slide

  5. 5
    ͜Μͳ͜ͱΛ࿩͠·͢
    • Rubyࣄ࢝Ί
    • ଞݴޠܦݧऀ͕ؾʹͳΓͦ͏ͳ͜ͱ
    - ܕ͕ͳ͍ͱෆ҆ʁ
    - Rubyͬͯ஗͍ΜͰ͠ΐʁ
    • Rubyͷ޷͖ͳͱ͜Ζ
    • ͦͯ͠ɺίϛϡχςΟ

    View full-size slide

  6. Rubyࣄ࢝Ί

    View full-size slide

  7. 7
    Rubyͱ͸
    • ೔ຊਓϓϩάϥϚͰ͋Δɺ·ͭ΋ͱΏ͖ͻΖࢯʢMatzʣ͕։ൃͨ͠ϓϩάϥϛϯάݴޠ
    • όʔδϣϯ1.0ͷϦϦʔε͸1996೥ɺ2019೥11݄࣌఺ͷ࠷৽όʔδϣϯ͸2.6
    - ࢀߟʣຖ೥12݄25೔ʹ৽όʔδϣϯ͕ϦϦʔε͞ΕΔɻ࣍͸Ruby 2.7
    • ΦϒδΣΫτࢦ޲εΫϦϓτݴޠɺಈతܕ෇͚
    • Ruby on Railsͷొ৔Ҏ߱ʢ2004೥ʣɺRuby͸ੈքதͷϓϩάϥϚʹ࢖ΘΕ͍ͯΔ

    View full-size slide

  8. 8
    Rubyجૅ (1 / 256)
    # จࣈྻ '1'.to_s #=> "1"
    # ਺஋ 1.to_s #=> "1"
    # nil nil.to_s #=> ""͋
    # true true.to_s #=> "true"͋
    # false false.to_s #=> "false"͋
    # ਖ਼نදݱ /\d+/.to_s #=> "(?-mix:\\d+)"
    Ruby͸͢΂͕ͯΦϒδΣΫτ

    View full-size slide

  9. ɾɾɾΈ͍ͨͳ͜ͱ͸ɺ΍ͬͯΒΕͳ͍ͷͰ

    View full-size slide

  10. ಈըͰͳΜͱͳ͘งғؾΛ͔ͭΜͰ͍ͩ͘͞
    ʢ໿12෼ʣ

    View full-size slide

  11. ಈը͸YouTube ʹΞοϓͯ͠·͢↓
    https://youtu.be/UZE3T5TYAvU
    ʢ"UPZBNBSL"΁Ͱݕࡧʣ

    View full-size slide

  12. ͳΜͱͳ͘งғؾ͸͔ͭΊ·͔ͨ͠ʁ

    View full-size slide

  13. 13
    ͖ͬ͞ॻ͍ͨϙέϕϧϓϩάϥϜʢࢀߟʣ
    TABLE = [
    %w(͋ ͍ ͑ ͏ ͓),
    %w(͔ ͖ ͘ ͚ ͜),
    # লུ
    ]
    def to_chars(input)
    row_cols = input.scan(/../)
    str = ''
    row_cols.each do |row_col|
    row, col = row_col.chars.map(&:to_i)
    str << TABLE[row - 1][col - 1]
    end
    str.katakana.zen_to_han
    end
    LENGTH = 20
    def decorate(text)
    puts '-' * LENGTH
    puts "|#{text.ljust(LENGTH - 2)}|"
    puts '-' * LENGTH
    end
    to_chars('1112324493')

    View full-size slide

  14. ଞݴޠܦݧऀ͕ؾʹͳΓͦ͏ͳ͜ͱ

    View full-size slide

  15. 15
    • ܕ͕ͳ͍ͱ͍͏ΑΓɺܕΛॻ͔ͳͯ͘΋ྑ͍
    • ʮܕ͕ͳ͍ͷͬͯͲ͏ʁෆศ͡Όͳ͍ʁෆ҆͡Όͳ͍ʁʯ
    • 7ʙ8೥RubyΛॻ͍ͯΈͯΘ͔ͬͨ͜ͱ͸ɺʮҙ֎ͱେৎ෉ʯ
    ܕ͕ͳ͍ͱෆ҆ʁ

    View full-size slide

  16. 16
    • ;ͭ͏ɺಈ࡞֬ೝ͢Δ΍Μʁʢܕ͕͋ͬͯ΋ͳͯ͘΋ʣ
    • typoͰ΋ɺܕͷෆҰகͰ΋ɺΤϥʔ͕ग़ͨΒͦ͜Ͱ௚ͤ͹͍͍΍Μʁ
    • ;ͭ͏ɺςετॻ͘΍Μʁʢܕ͕͋ͬͯ΋ͳͯ͘΋ʣ
    • ϓϩάϥϜ͕͓͔͘͠ͳͬͨΒɺςετ͕མͪΔ͔Βͦ͜Ͱؾͮ͘΍Μʁ
    • ;ͭ͏ɺίʔυϨϏϡʔ͢Δ΍Μʁʢܕ͕͋ͬͯ΋ͳͯ͘΋ʣ
    • ໌Β͔ʹόάΔϩδοΫͱ͔ɺՄಡੑ͕࠷ѱͷίʔυͩͬͨΒࢦఠ͕ೖΔ΍Μʁ
    • ͋Εͬɺ࣮͸ίϯύΠϥ͕ͳͯ͘΋ͦ͜·ͰࠔΒͳ͍ͷͰ͸ɾɾɾʁ
    ͳͥେৎ෉ͳͷ͔ʁ

    View full-size slide

  17. 17
    • ιχοΫΨʔσϯ ʹ Ruby࢖ͬͯ·͢ɻͰ΋ɺίʔυ͸શવಡΈॻ͖͠΍͍͢Ͱ͢ʂ
    • લ৬ɺલʑ৬ ʹ Java΍C#࢖ͬͯ·ͨ͠ɻͰ΋ɺΫ˓ίʔυʹࢄʑۤ͠ΊΒΕ·ͨ͠ʂ
    • ͋Εͬɺܕͷ͋Δɾͳͬͯ͠ɺෆศ͞΍ෆ҆ɺίʔυͷ඼࣭ʹແؔ܎ͳͷͰ͸ʁʁ
    • ܕΑΓେࣄͳͷ͸ɺͪΌΜͱͨ͠ϓϩάϥϚ͕ͪΌΜͱͨ͠ίʔυΛॻ͘͜ͱͰͨ͠
    • ͔ͩΒɺʮRuby͸ܕ͕ͳ͍͔Βෆ҆ʯ͸੒Γཱͨͳ͍ʂʢҰ൪৺഑͢΂͖͸ϓϩάϥϚͷεΩϧʣ
    • ܕΛॻ͔ͳͯ͘΋ɺͪΌΜͱͨ͠ίʔυͳΒܕ͕ಁ͚ͯݟ͑Δ͸ͣ
    ˓˓͕Ұ൪େࣄ

    View full-size slide

  18. 18
    ͨͱ͑͹͜Μͳײ͡
    users = create_users(user_names)
    ϝιουͷ໾ׂ = ෳ਺ͷUserΦϒδΣΫτΛ࡞੒͢Δ
    Ҿ਺ = Ϣʔβʔ໊ͷ഑ྻʢ֤ཁૉ͸ͨͿΜจࣈྻʣ
    UserΦϒδΣΫτͷ഑ྻ͕ೖͬͯΔ͸ͣ
    ʢuser + ෳ਺ܗͳͷͰʣ

    View full-size slide

  19. 19
    • Ruby΍Ruby on Rails͸ɺͱͯ΋ςετίʔυ͕ॻ͖΍͍͢
    - ςετίʔυͷॻ͖΍͕͢͞ɺϓϩάϥϜʹର͢Δ҆৺ײΛิڧͯ͘͠ΕΔ
    • RubyMineΛ࢖͏ͱʢ੩తݴޠ΄ͲͰ͸ͳ͍͕ʣίʔυิ׬΍ϦϑΝΫλϦϯάΛࢧԉͯ͘͠ΕΔ
    ͪΐͬͱ͚ͩิ଍

    View full-size slide

  20. 20
    E2Eςετ͕؆୯ʹॻ͚·͢ ʢE2Eςετ = ΤϯυπʔΤϯυςετʣ
    RSpec.describe 'Sessions', type: :system do
    before do
    # ࣄલʹϢʔβʔΛ࡞੒͓ͯ͘͠
    User.create(email: '[email protected]', password: 'pass')
    end
    scenario 'ϢʔβʔʹΑΔϩάΠϯ' do
    end
    end
    visit new_user_session_path
    fill_in 'Eϝʔϧ', with: '[email protected]'
    fill_in 'ύεϫʔυ', with: 'pass'
    click_button 'ϩάΠϯ'
    expect(page).to have_content 'ϩάΠϯ͠·ͨ͠'
    ᶃ ϖʔδΛ։͘
    ᶄ ϑΥʔϜೖྗ
    ᶅ ϩάΠϯϘλϯΫϦοΫ
    ᶆ ϩάΠϯ੒ޭΛݕূ

    View full-size slide

  21. 21
    RubyMineʹΑΔίʔυิ׬ͷྫ
    • ྫʣTwitter gemΛ࢖ͬͯϓϩϑΟʔϧࣸਅURLΛऔಘ͢Δίʔυ
    ※ൃද౰೔͸ಈըͰઆ໌͠·ͨ͠

    View full-size slide

  22. ͏ʔΜɺͰ΋ɾɾɾ

    View full-size slide

  23. 23
    • ˓˓ͱ͍͏ݴޠʹൺ΂Δͱɺ͔ͨ͠ʹϕϯνϚʔΫ্ͷ࣮ߦ଎౓͸஗͍͔΋
    • ͕ɺRubyͷ஗͕͞໰୊ʹͳΔϢʔεέʔε͸ɺܾͯ͠ʮ͢΂ͯͷϢʔεέʔεʯͰ͸ͳ͍
    • ࣮ࡍʹRubyͰ։ൃΛ͍ͯͯ͠ɺʮRubyɺ͓ͤʔͳʯͱࢥ͏͜ͱ͸΄ͱΜͲͳ͍
    • ͕ɺօແͱ͍͏͜ͱ΋ͳ͍ɻϔϏʔͳόονॲཧͰ஗͞Λײͨ͜͡ͱ͸͋Δ
    - ͨͩ͠ɺ͜Ε͸RubyຊମͰ͸ͳ͘ɺݫີʹ͸RailsͷActiveRecordͷ໰୊͔΋
    - ଞͷݴޠʹॻ͖׵͑ͨΘ͚Ͱ΋ͳ͍ͷͰɺਖ਼֬ͳൺֱ͸Ͱ͖ͳ͍
    Rubyͬͯ஗͍ΜͰ͠ΐʁ

    View full-size slide

  24. 24
    https://jp.quora.com/naze-ruby-ha-hokano-gengo-to-kurabe-te-osoi-node-shou-ka
    ࢀߟɿͪ͜ΒͷQuoraͷهࣄ΋ͥͻಡΜͰΈͯʂ
    Rubyίϛολͷ
    ࡫ా͞Μ

    View full-size slide

  25. 25
    • Ruby 3.0͕໨ඪͱ͍ͯ͠ΔςʔϚ
    - ੩తܕղੳ
    - ύϑΥʔϚϯε
    - ฒߦੑ
    • Ruby 3.0͕ϦϦʔε͞ΕͨΒɺࠓͷঢ়گ΋ͪΐͬͱมΘͬͯ͘Δ͔΋ʁ
    • ຊ೔ͷদຊफଠ࿠͞Μͷট଴ߨԋʮܗͳ͠ݴޠͷͨΊͷܕʯʹ΋ཁ஫໨ʂʂ
    ࿕ใɿRuby 3.0ʹظ଴ʂ
    https://logmi.jp/tech/articles/320752

    View full-size slide

  26. ͋ͱ͸ओ؍ͰޠΓਚ͘͠·͢

    View full-size slide

  27. 27
    • ඪ४Ͱ༻ҙ͞Ε͍ͯΔAPI͕ΊͬͪΌ๛෋
    - ಛʹ഑ྻؔ࿈ͷϝιου͸ͼͬ͘Γ͢Δ͙Β͍ɺ͔Ώ͍ͱ͜Ζʹख͕ಧ͘ʂ
    - ʮ͑ɺ͋ͷϝιουͳ͍ͷʁϥΠϒϥϦ͕ඞཁʂʁʯ→ ଞͷݴޠΛ࢖͏ؾʹͳΕͳ͍
    • σόοά࣌ʹϥΠϒϥϦͷίʔυ΋ɺ΄΅100%ͦͷ৔ͰಡΊΔɺͱ͍͏҆৺ײ
    - όΠφϦ͔͠ఏڙ͞Ε͍ͯͳͯ͘ιʔείʔυ͸ҋͷதɺͱ͍͏͜ͱ͕ى͖ͳ͍
    - ͨͩ͠ɺͨ·ʹCݴޠͰ֦ு͞Ε͍ͯΔ৔߹΋͋ΔʢͳͷͰɺ΄΅100%ʣ
    Rubyͷ޷͖ͳͱ͜Ζ

    View full-size slide

  28. 28
    ศརͳ഑ྻͷϝιουͷྫ
    input = '1112324493'
    # ઌʹ͢΂ͯͷจࣈΛ੔਺ʹͯ͠͠·͏
    numbers = input.chars.map(&:to_i)
    #=> [1, 1, 1, 2, 3, 2, 4, 4, 9, 3]
    # ഑ྻͷཁૉΛ2ͭͣͭηοτʹ͢Δʢωετͨ͠഑ྻʹͳΔʣ
    row_cols = numbers.each_slice(2)
    #=> [[1, 1], [1, 2], [3, 2], [4, 4], [9, 3]]
    str = ''
    row_cols.each do |row, col|
    str << TABLE[row - 1][col - 1]
    end
    ϙέϕϧϓϩάϥϜͷผղʢ഑ྻͷeach_sliceϝιουΛ࢖͏ྫʣ
    each_slice ϝιουʹ஫໨!!

    View full-size slide

  29. 29
    ϥΠϒϥϦͷίʔυ͕ɺ΄΅100%ͦͷ৔ͰಡΊΔ
    ʢҙຯ͕ཧղͰ͖Δ͔Ͳ͏͔͸ผʹͯ͠ʣඞͣखݩʹίʔυ͕͋Δʂʂ
    ※RailsͷίʔυͷҰ෦Ͱ͢

    View full-size slide

  30. ·ͩ·ͩ͋Γ·͢

    View full-size slide

  31. 31
    • ॻ͖৺஍͕Α͍ɻॻ͘ͷָ͕͍͠ɻؾ͍͍࣋ͪ
    • ͨͩ͠ɺ͜ͷڥ஍ʹࢸΔ·Ͱ͸਺೥͔͔ͬͨʢ࠷ॳ͸೉͔ͬͨ͠Αʂʣ
    • Ͱ΋ɺ͜Ε·Ͱ࢖͖ͬͯͨݴޠͷதͰҰ൪޷͖
    • ଞͷݴޠʹ͋·Γڵຯ͕෸͔ͳ͘ͳͬͯ͠·ͬͨͷ͕ɺRubyͷ໰୊఺͔΋
    ͨͷ͍͠ʂؾ͍͍࣋ͪʂ

    View full-size slide

  32. 32
    ͨͱ͑͹ɺ͜Μͳײ͡ʁ
    def to_chars(input)
    to_c = ->((row, col)) { TABLE[row][col] }
    input
    .each_char
    .map(&:to_i)
    .map(&:pred)
    .each_slice(2)
    .map(&to_c)
    .join
    .katakana
    .zen_to_han
    end
    ϙέϕϧϓϩάϥϜΛؔ਺ܕݴޠνοΫʹॻ͘͜ͱ΋Ͱ͖Δʂʢͱ͍͏ࣗݾຬ଍wʣ
    ϥϜμʢؔ਺ΦϒδΣΫτʣ
    ϩʔΧϧม਺Λ࢖Θͣɺͻͨ͢ΒϝιουνΣʔϯͰॻ͘

    View full-size slide

  33. ͦͯ͠ɺίϛϡχςΟ

    View full-size slide

  34. 34
    • Ruby͸ίϛϡχςΟ׆ಈ͕͔͞Μ
    - ʮ஍ҬRubyͷձʯͰݕࡧ͢ΔͱɺશࠃͷRubyίϛϡχςΟ͕ͨ͘͞Μग़ͯ͘Δ
    • ๻ࣗ਎΋஍ݩͰ੢࿬.rbˍਆށ.rbͱ͍͏ίϛϡχςΟΛ΍͍ͬͯ·͢
    • ঁੑΤϯδχΞͷͨΊͷίϛϡχςΟ׆ಈ΋੝ΜʢRailsGirls΍TokyoGirls.rb౳ʣ
    • ๻͕஌ΔݶΓɺͲ͜ͷίϛϡχςΟ΋ΈΜͳ༏͍͠
    - ৽ࢀऀ΍ϓϩάϥϛϯάॳ৺ऀ͕ͳ͍͕͠Ζʹ͞ΕΔ͜ͱ͸ͳ͍͸ͣ
    ίϛϡχςΟʹࢀՃͯ͠ΈΑ͏ʂ

    View full-size slide

  35. 35
    ॳΊͯͷํ΋ා͕Βͣʹͥͻʂ
    ʢ஍ҬRubyͷձϖʔδΑΓൈਮʣ

    View full-size slide

  36. 37
    • ࠓΛ͔͞ͷ΅Δ͜ͱ4೥લɾɾɾ
    • ੢࿬.rbˍਆށ.rbͰRubyϓϩάϥϛϯάΩϟϯϓͱ͍͏߹॓ΠϕϯτΛ։͖·ͨ͠
    • ࢀՃऀืूϖʔδΛެ։ͨ͠ͱ͜Ζɺ͜Μͳ໰͍߹Θ͕ͤɾɾɾ
    ͦ΋ͦ΋ͳͥ๻͕෋ࢁʹʁ

    View full-size slide

  37. 38
    from: ෋ࢁͷഴౡ͞ΜʢRubyॳ৺ऀʣ
    ͱͬɺͱͬɺ෋ࢁΝʔʔοʂʁ

    View full-size slide

  38. 39
    ͦͯ͠൴͸΍͖ͬͯͨ

    ʢഴౡ͞Μʣ

    View full-size slide

  39. 40
    ͦͯ͠൴͸ָ͠Μͩ

    ʢͳΜ͔৯΂ͯΔʣ

    View full-size slide

  40. 41
    ͦͯ͠൴͸ؼ͍ͬͯͬͨ

    ʢͱ๻ʣ

    View full-size slide

  41. 42
    ͦΕ͔Β4೥ޙʢͭ·Γࠓ೥ʣ
    ൴ʢഴౡ͞Μʣ
    ͸͍ɺΑΖ͜ΜͰʔʂʂ

    View full-size slide

  42. 43
    ͦͯ͠๻͕΍͖ͬͯͨʢ෋ࢁʹʣ
    ͢͹Β͖͔͠ͳɺίϛϡχςΟ׆ಈ

    View full-size slide

  43. 45
    ຊ೔͓࿩ͨ͜͠͠ͱ
    • Rubyࣄ࢝ΊʢϙέϕϧϓϩάϥϜͷ࡞੒ʣ
    • ଞݴޠܦݧऀ͕ؾʹͳΓͦ͏ͳ͜ͱ
    - ܕ͕ͳ͍ͱෆ҆ʁ
    - Rubyͬͯ஗͍ΜͰ͠ΐʁ
    • Rubyͷ޷͖ͳͱ͜Ζ
    • ͦͯ͠ɺίϛϡχςΟ

    View full-size slide

  44. 46
    ຊ೔ͷςʔϚʢ͓͞Β͍ʣ
    • ͔Ε͜Ε7ʙ8೥RubyΛ࢖͖ͬͯͨࢲ
    • Ҏલ͸SIer΍ࣾ಺ΤϯδχΞͱͯ͠Java΍C#ͰϝΠϯͰ࢖͍ͬͯ·ͨ͠
    • ੲͱࠓΛൺ΂ΔͱʮͳΜ͔͍Ζ͍ΖมΘͬͨͳ͋ʯͱࢥ͏ͱ͜Ζ͕ଟʑ͋Γ·͢
    • ࢖͏ݴޠ͕มΘΔͱԿ͕Ͳ͏มΘΔͷ͔ʁ
    • Rubyະܦݧऀ͕ʮ΁ʙɺRubyͬͯͳΜ͔໘നͦ͏ʂʯͱࢥ͑ΔΑ͏ͳ৘ใΛఏڙ͠·͢
    - ϓϩάϥϛϯάܦݧ͸͋ͬͨํ͕͍͍Ͱ͕͢ɺRubyͷ஌ࣝ͸ෆཁͰ͢ʂ

    View full-size slide

  45. ͍͞͝ʹɺ͝൧ͷ࿩
    ʢεϥΠυͳ͠ʣ

    View full-size slide

  46. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠
    @jnchito
    blog.jnito.com

    View full-size slide