SlideShare a Scribd company logo
Make your own Gem
林政億 Jimmy
Gem 介紹
● bundle 工具
● gem 的原理及如何使用
● 如何打包一個gem
● 打包一個 Assets gem
Bundle 簡介
● http://bundler.io/
● 是管理Gem相依性的工具,執行bundle install時,會根據
應用程式目錄中Gemfile的設定,檢查指定的Gem與相依
套件是否已安裝
What is Gemfile
● Gemfile 是用來管理Gem的檔案,應用程式會使用的
Gem,一定要在Gemfile中設定,預設Rails Project 產生的
都會連到 https://rubygems.org
● 可以使用 bundle init 產生 gemfile
What is Gemfile
bundle install & bundle update
● bundle install 連線伺服器,會檢查Gem更新與相依性,必
要時下載安裝,最後應用程式使用了哪些Gem與版本,會
記錄在Gemfile.lock之中,如果Gemfile有變動,執行
bundle install會查看Gemfile.lock,瞭解已安裝了哪些
Gem,僅安裝更新的Gem,並將新使用到的Gem資訊記錄
至Gemfile.lock。
bundle update
● 可以執行bundle update gemname指定更新Gem,如果僅
執行bundle update,就會重新確認Gem更新與相依性,重
新產生Gemfile.lock。
Ruby Gem
Ruby Gem
簡單的說就是一個 ruby gem的套件管理平台
官方網站: https://rubygems.org/
Ruby Gem
● gem install [gemfile name]
● gem uninstall [gemfile name]
● gem search [gemfile name]
● gem search [gemfile name] -d
● gem list
● gem server
●
Ruby Gems Toolbox
● https://www.ruby-toolbox.com/
Gem Environment
Where is Gems
● 存放在 ruby 的目錄的 gems 裡
● 例如使用 rvm 來管理 ruby
○ /Users/xxx/.rvm/gem/ruby-xxx/gems
○ 每個 ruby 版本都會存放一份這些 gems
Rails 中如何使用 gem
● 設定在 gemfile 中
● 使用的方式如下
○ gem 'rails'
○ gem 'rails' ,'4.1.6'
○ gem 'nokogiri', :git => 'https://github.com/tenderlove/nokogiri.git', :
branch => '1.4'
○ gem 'extracted_library', :path => './vendor/extracted_library'
Make your own Gem
Make your own Gem
1. bundle gem firstGem
2. 修改 firstGem.gemspec
3. 修改 lib/firstGem.rb
4. gem build FirstGem.gemspec
5. 測試 gem
Step 1 : bundle gem firstGem
Step 2 : 修改 firstGem.gemspec
Step 3 : 修改 lib/ firstGem.rb
Step 4 : gem build FirstGem.gemspec
Step 5 : 測試
gem install firstGem
查看安裝 gem 的內容
Step 5 : 測試
使用 irb 測試
將 Gem push 到 RubyGem
將key存到本機中
push gem 到 rubygem
將 Gem push 到 RubyGem
Make your assets Gem
試著來打包 TimeUtils
Assets 位置
● app/assets
● lib/assets
● vender/assets
只要放到這三個其中一個,就可以直接在 application.js
或 application.css 中直接 require使用了
1. vendor/assets/javascripts
2. vendor/assets/stylesheets
3. app/assets/javascripts
4. app/helpers/
與 assets 相關多出來的檔案
Step1: 把 assets 放到project
在 vendor/assets/javascripts 載入 jquery.timeago.js
官方JS 位置: https://github.com/rmm5t/jquery-timeago
Step2: 載入JS
在 app/assets/javascripts 新增 timeago.js
Step3: 撰寫 helper
Step4: 使用 Engine
Step5: 修改 gemspec
Step6: 測試
1. 在 Rails Project 中 的 application.js 載入 timeago
2. Gemfile 載入 gem 的位置
Step6: 測試
範例參考來源: http://brandonhilkert.com/books/build-a-ruby-gem/?
utm_source=youtube&utm_medium=web&utm_campaign=rails-engine#pricing
The End
參考資料
http://bundler.io/v1.8/gemfile.html
http://guides.rubygems.org/make-your-own-gem/#introduction
http://asciicasts.com/episodes/245-new-gem-with-bundler
http://blog.xdite.net/posts/2012/01/04/how-to-pack-a-gem
http://guides.rubygems.org/make-your-own-gem/

More Related Content

Featured

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
GetSmarter
 

Featured (20)

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 

Make your own gem