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. • ҏ౻ ३Ұ / @jnchito • RubyϓϩάϥϚʢ2012೥ʙ@ɹɹɹɹɹɹɹɹɹɹɹɹʣ • ฌݿݝ੢࿬ࢢࡏॅʢϦϞʔτϫʔΧʔʣ •

    ϒϩά = give IT a try (https://blog.jnito.com) • Qiita ૯߹1Ґ (2019೥11݄ݱࡏ) • ஶॻɾ༁ॻ - ϓϩΛ໨ࢦ͢ਓͷͨΊͷRubyೖ໳ʢٕज़ධ࿦ࣾʣ - Everyday Rails - RSpecʹΑΔRailsςετೖ໳ ʢLeanpubʣ 2 ͔ΜͨΜʹࣗݾ঺հ χγϫΩγʁʁ
  2. 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͸͢΂͕ͯΦϒδΣΫτ
  3. 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')
  4. 16 • ;ͭ͏ɺಈ࡞֬ೝ͢Δ΍Μʁʢܕ͕͋ͬͯ΋ͳͯ͘΋ʣ • typoͰ΋ɺܕͷෆҰகͰ΋ɺΤϥʔ͕ग़ͨΒͦ͜Ͱ௚ͤ͹͍͍΍Μʁ • ;ͭ͏ɺςετॻ͘΍Μʁʢܕ͕͋ͬͯ΋ͳͯ͘΋ʣ • ϓϩάϥϜ͕͓͔͘͠ͳͬͨΒɺςετ͕མͪΔ͔Βͦ͜Ͱؾͮ͘΍Μʁ •

    ;ͭ͏ɺίʔυϨϏϡʔ͢Δ΍Μʁʢܕ͕͋ͬͯ΋ͳͯ͘΋ʣ • ໌Β͔ʹόάΔϩδοΫͱ͔ɺՄಡੑ͕࠷ѱͷίʔυͩͬͨΒࢦఠ͕ೖΔ΍Μʁ • ͋Εͬɺ࣮͸ίϯύΠϥ͕ͳͯ͘΋ͦ͜·ͰࠔΒͳ͍ͷͰ͸ɾɾɾʁ ͳͥେৎ෉ͳͷ͔ʁ
  5. 17 • ιχοΫΨʔσϯ ʹ Ruby࢖ͬͯ·͢ɻͰ΋ɺίʔυ͸શવಡΈॻ͖͠΍͍͢Ͱ͢ʂ • લ৬ɺલʑ৬ ʹ Java΍C#࢖ͬͯ·ͨ͠ɻͰ΋ɺΫ˓ίʔυʹࢄʑۤ͠ΊΒΕ·ͨ͠ʂ •

    ͋Εͬɺܕͷ͋Δɾͳͬͯ͠ɺෆศ͞΍ෆ҆ɺίʔυͷ඼࣭ʹແؔ܎ͳͷͰ͸ʁʁ • ܕΑΓେࣄͳͷ͸ɺͪΌΜͱͨ͠ϓϩάϥϚ͕ͪΌΜͱͨ͠ίʔυΛॻ͘͜ͱͰͨ͠ • ͔ͩΒɺʮRuby͸ܕ͕ͳ͍͔Βෆ҆ʯ͸੒Γཱͨͳ͍ʂʢҰ൪৺഑͢΂͖͸ϓϩάϥϚͷεΩϧʣ • ܕΛॻ͔ͳͯ͘΋ɺͪΌΜͱͨ͠ίʔυͳΒܕ͕ಁ͚ͯݟ͑Δ͸ͣ ˓˓͕Ұ൪େࣄ
  6. 18 ͨͱ͑͹͜Μͳײ͡ users = create_users(user_names) ϝιουͷ໾ׂ = ෳ਺ͷUserΦϒδΣΫτΛ࡞੒͢Δ Ҿ਺ =

    Ϣʔβʔ໊ͷ഑ྻʢ֤ཁૉ͸ͨͿΜจࣈྻʣ UserΦϒδΣΫτͷ഑ྻ͕ೖͬͯΔ͸ͣ ʢuser + ෳ਺ܗͳͷͰʣ
  7. 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 'ϩάΠϯ͠·ͨ͠' ᶃ ϖʔδΛ։͘ ᶄ ϑΥʔϜೖྗ ᶅ ϩάΠϯϘλϯΫϦοΫ ᶆ ϩάΠϯ੒ޭΛݕূ
  8. 25 • Ruby 3.0͕໨ඪͱ͍ͯ͠ΔςʔϚ - ੩తܕղੳ - ύϑΥʔϚϯε - ฒߦੑ

    • Ruby 3.0͕ϦϦʔε͞ΕͨΒɺࠓͷঢ়گ΋ͪΐͬͱมΘͬͯ͘Δ͔΋ʁ • ຊ೔ͷদຊफଠ࿠͞Μͷট଴ߨԋʮܗͳ͠ݴޠͷͨΊͷܕʯʹ΋ཁ஫໨ʂʂ ࿕ใɿRuby 3.0ʹظ଴ʂ https://logmi.jp/tech/articles/320752
  9. 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 ϝιουʹ஫໨!!
  10. 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ʣ ϥϜμʢؔ਺ΦϒδΣΫτʣ ϩʔΧϧม਺Λ࢖Θͣɺͻͨ͢ΒϝιουνΣʔϯͰॻ͘