hbkk
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # gemを作ろうの会 ## 初めに gemを作ってみよう会です 簡単なgemを作ってみてlocalにインストールして遊んでみよう企画です 諸事情で公開はやりません ## 作業ディレクトリに移動する 好きなディレクトリを作るか作業ディレクトリに入ってください 私の場合は `$ cd environment/kenchaso` ## gemのテンプレートを作成する `$ bundle gem kirakira_name` ※この際ネームスペースをハイフンで区切るとgem内のディレクトリが分かれてしまうので注意 lib以下のディレクトリlib/kirakira/nameみたいになるので意図しなければアンダースコアを使った方が良いらしい いろいろ聞かれると思うので都度答えていきましょう > 初めてgemを作る場合、 > MIT Licenseを使用するかどうか > Code of Conductを含めるかどうか > 聞かれます。今回はどちらもyesにしました。 > MIT Licenseとは: > 要約すると、MIT Licenseとは次のようなライセンスである。 > このソフトウェアを誰でも無償で無制限に扱って良い。ただし、著作権表示および本許諾表示をソフトウェアのすべての> 複製または重要な部分に記載しなければならない。 > 作者または著作権者は、ソフトウェアに関してなんら責任を負わない。 > [MIT License - Wikipedia](https://ja.wikipedia.org/wiki/MIT_License) より > MIT Licenseのコードを利用する場合、利用者はその旨をコード内かその他のファイルに記載する必要があります。詳細は [こちら](https://qiita.com/lovee/items/484ae3fc038314a64ee2#mit-license) を参考にしてください。 > Code of Conductについては [こちら](https://qiita.com/nstoym/items/ca711287117074a810aa) を参考にしてください。 [【Ruby】gemの作り方から公開まで - Qiita](https://qiita.com/9sako6/items/72994b8b1c00af4e61fe#1-%E9%9B%9B%E5%BD%A2%E3%82%92%E4%BD%9C%E3%82%8B) ## git commitしとこう `$ git add .` `$ git commit -m"first commit"` ## gem_name.gemspecを修正する `kirakira_name.gemspec`の"TODO"というワードが含まれているところを修正する ※"TODO"というワードが含まれていると`bundle install`できない ```ruby # frozen_string_literal: true require_relative "lib/kirakira_name/version" Gem::Specification.new do |spec| spec.name = "kirakira_name" spec.version = KirakiraName::VERSION spec.authors = ["hogehoge"] spec.email = ["hogehoge@example.com"] spec.summary = "return kirakira_name" spec.description = "KirakiraName.methods" spec.homepage = "https://example.com" spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0") spec.metadata["allowed_push_host"] = "https://example.com" spec.metadata["homepage_uri"] = spec.homepage spec.files = Dir.chdir(File.expand_path(__dir__)) do `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) } end spec.bindir = "exe" spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) } spec.require_paths = ["lib"] end ``` `$ bundle install` ## lib以下にコードを書いていく ### lib/kirakira_name.rb ```ruby # frozen_string_literal: true require_relative "kirakira_name/version" module KirakiraName class Error < StandardError; end def self.random_one # でっかい配列からランダムに1個の配列を取得する [["一護", "いちご"], ["雨竜", "うりゅう"], ["泰虎", "やすとら"]].sample(1).flatten end end ``` ## 動作確認してみよう `$ ./bin/console` ``` ╭─○ ~/envirnment/kenchaso/kirakira_name ?(main) ╰─○ $ ./bin/console irb(main):001:0> KirakiraName.random_one => ["雨竜", "うりゅう"] irb(main):002:0> KirakiraName.random_one => ["泰虎", "やすとら"] irb(main):003:0> KirakiraName.random_one => ["一護", "いちご"] irb(main):004:0> KirakiraName.random_one => ["白哉", "びゃくや"] irb(main):006:0> exit ``` ## 配列をyaml形式のデータに置き換えてみる 上のコードみたいに配列でデータ取得するとデータ増えた時汚くなっちゃうのでyaml形式のファイルにデータを置き換えてみよう lib以下にdataというディレクトリを作り、names.ymlというフォルダを作る ### lib/data/names.yml ``` - ["宝冠", "てぃあら"] - ["紅多", "べーた"] - ["出誕", "でるた"] - ["晴空", "はるく"] - ["瑠火", "るか"] - ["俐亜武", "りあむ"] - ["蓮", "れん"] - ["彩華", "いろは"] - ["心美", "ここみ"] - ["蓮音", "れんおん"] - ["愛紗", "あいしゃ"] - ["衣音", "いおん"] - ["登生", "とうい"] - ["心音", "ここね"] - ["弥安", "びあん"] - ["湖々", "ここ"] - ["玲夢", "りむ"] - ["来蘭", "らら"] - ["亘利翔", "ぎりしゃ"] - ["朱李埜", "とりの"] - ["寿希也", "じゅきや"] - ["寿以輝", "じゅいき"] - ["仁花", "にか"] - ["葉紐", "ぱにゅ"] - ["光", "ちゃくら"] - ["乃晏", "のあん"] - ["陸離", "りくり"] - ["添", "てん"] - ["花鳥", "あとり"] - ["希妃梛", "まひな"] - ["伽羅", "きゃら"] - ["澄海", "すかい"] - ["詠斗", "えいと"] - ["在波", "あるは"] - ["米呂", "まいろ"] - ["愛流", "あいる"] - ["美勇士", "みゅうじ"] - ["奈瑞菜", "なずな"] - ["空太", "こうた"] - ["桜深", "おうみ"] - ["礼夢", "らいむ"] - ["甲子園", "こうしえん"] - ["友里亜", "ゆりあ"] - ["紗音琉", "さとね"] - ["陽菜", "ひな"] - ["陸", "りく"] - ["陽葵", "ひまり"] - ["奏音", "そら"] ``` > [YAML](http://www.yaml.org/start.html) (YAML Ain’t Markup Language) とは、構造化されたデータを表現するためのフォーマットです。YAML は次のような用途に向いています。 > * 各種設定ファイル > * データ保存用 (シリアライゼーション) > * データ交換用フォーマット > * ログファイル > YAML の目的は XML と似ていますが、XML と比べて次のような利点があります (YAML と XML との詳しい比較はセクション『 [XML との比較](https://magazine.rubyist.net/articles/0009/0009-YAML.html#l13) 』をご覧ください)。 > * 読みやすい ―― YAML ではインデントを使ってデータの階層構造を表すため、人間にとって非常に読みやすいです。 > * 書きやすい ―― YAML では XML のような終了タグが必要ないので、人間にとって非常に書きやすいです。 > * わかりやすい ―― YAML ではデータを「配列」「ハッシュ」「スカラー (数値や文字列や真偽値)」だけで表すため、人間にとって非常に理解しやすく、またプログラミングも容易です。 [プログラマーのための YAML 入門 (初級編)](https://magazine.rubyist.net/articles/0009/0009-YAML.html) 以下今回用意したデータの`-`は配列を表すので多元配列になってます ``` [["紗音琉", "さとね"], ["陽菜", "ひな"], ["陸", "りく"], ["陽葵", "ひまり"]] ``` こんな感じ yamlファイルを読み込むようにライブラリ読み込んだりパス書いたりします ```ruby # frozen_string_literal: true require_relative "kirakira_name/version" # yaml形式のデータを読み込むためにライブラリを読み込み require 'yaml' module KirakiraName class Error < StandardError; end def self.random_one # lib/data/names.ymlを読み込むように書き換え YAML.load_file(File.expand_path(File.join('..', 'data', 'names.yml'), __FILE__)).sample(1).flatten end end ``` [library yaml (Ruby 3.0.0 リファレンスマニュアル)](https://docs.ruby-lang.org/ja/latest/library/yaml.html) ## 動作確認してみよう `$ ./bin/console` ``` ╭─○ ~/envirnment/kenchaso/kirakira_name ?(main) ╰─○ $ ./bin/console irb(main):001:0> KirakiraName.random_one => ["陽菜", "ひな"] irb(main):002:0> KirakiraName.random_one => ["光", "ちゃくら"] irb(main):003:0> KirakiraName.random_one => ["在波", "あるは"] irb(main):004:0> KirakiraName.random_one => ["彩華", "いろは"] irb(main):005:0> exit ``` ## git commitしとこう `$ git add .` `$ git commit -m"finish create gem"` ## ローカルに作ったgemをインストールしよう - gemをビルドする `$ bundle exec rake build` - 出来たpkg/kirakira_name-0.1.0.gemっていうgemをローカルにインストールする `gem install pkg/kirakira_name-0.1.0.gem` ``` ╭─○ ~/envirnment/kenchaso/kirakira_name ?(main) ╰─○ $ bundle exec rake build kirakira_name 0.1.0 built to pkg/kirakira_name-0.1.0.gem. ╭─○ ~/envirnment/kenchaso/kirakira_name ?(main) ╰─○ $ gem install pkg/kirakira_name-0.1.0.gem Successfully installed kirakira_name-0.1.0 Parsing documentation for kirakira_name-0.1.0 Installing ri documentation for kirakira_name-0.1.0 Done installing documentation for kirakira_name after 0 seconds 1 gem installed ``` こんな感じ これでgemが使えるようになった ちなみに上の処理を1コマンドで出来る`$ bundle exec rake install`ってのもあります ``` $ bundle exec rake install => kirakira_name 0.1.0 built to pkg/kirakira_name-0.1.0.gem. => kirakira_name (0.1.0) installed. ``` ## irbを起動して遊んでみる `$ irb` ``` ╭─○ ~/envirnment/kenchaso/kirakira_name (main) ╰─○ $ irb irb(main):001:0> require 'kirakira_name' => true irb(main):002:0> KirakiraName.random_one => ["礼夢", "らいむ"] irb(main):003:0> KirakiraName.random_one => ["玲夢", "りむ"] irb(main):004:0> KirakiraName.random_one => ["仁花", "にか"] irb(main):005:0> exit ``` こんな感じでランダムで名前が返ってきます ## gemをアンインストールしとこう `$ gem uninstall kirakira_name` ``` ╭─○ ~/envirnment/kenchaso/kirakira_name (main) ╰─○ $ gem uninstall kirakira_name Successfully uninstalled kirakira_name-0.1.0 ``` ## 参考文献 [【Ruby】gemの作り方から公開まで - Qiita](https://qiita.com/9sako6/items/72994b8b1c00af4e61fe) [Bundler: bundle gem](https://bundler.io/man/bundle-gem.1.html) ## 終わりに 今回はざっくりとgemの作り方がなんとなく分かればいいかなくらいのゆるい感じでやりました ちゃんと作って公開したい時は参考文献に貼ったやつみたりしてやってみてください

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully