LoginSignup
38
39

More than 5 years have passed since last update.

gemとはなんじゃその① 初心者→中級者へのSTEP13/25

Posted at

gemとはなんじゃその①

はじめに

今回はgemです。ローカルのRailsのアップデート時にGemfileとかいじってた時に、そういえばgemってなんだろ...、Railsのライブラリ管理だよな...?となって色々誤解していることが顕になったので調べてみました。

gemとは

gemとはrubyのライブラリのことです。まあだから結局railsのライブラリにもなるのだけれど、正しい認識は重要。というか「gemはrailsのライブラリ」とか言ってるとruby力がないのがバレる.....

gemの扱い方

まずはターミナルでローカルにインストールされてるgemを確認してみましょう。
gem listで見ることができます。


$ gem list

*** LOCAL GEMS ***

actioncable (5.1.6.1, 5.1.0, 5.0.7.1, 5.0.7, 5.0.6, 5.0.2)
actionmailer (5.1.6.1, 5.1.0, 5.0.7.1, 5.0.7, 5.0.6, 5.0.2)
actionpack (5.1.6.1, 5.1.0, 5.0.7.1, 5.0.7, 5.0.6, 5.0.2)
actionview (5.1.6.1, 5.1.0, 5.0.7.1, 5.0.7, 5.0.6, 5.0.2)
activeadmin (1.2.1)
activejob (5.1.6.1, 5.1.0, 5.0.7.1, 5.0.7, 5.0.6, 5.0.2)
activemodel (5.1.6.1, 5.1.0, 5.0.7.1, 5.0.7, 5.0.6, 5.0.2)
activerecord (5.1.6.1, 5.1.0, 5.0.7.1, 5.0.7, 5.0.6, 5.0.2)
activesupport (5.1.6.1, 5.1.0, 5.0.7.1, 5.0.7, 5.0.6, 5.0.2)
addressable (2.5.2)
ansi (1.5.0)
arbre (1.1.1)
arel (8.0.0, 7.1.4)
ast (2.4.0)
autoprefixer-rails (8.3.0, 8.2.0)
awesome_print (1.8.0)
aws-sdk-core (2.11.65)
aws-sigv4 (1.0.2)
babel-source (5.8.35)
babel-transpiler (0.7.0)
bcrypt (3.1.12, 3.1.11)
bigdecimal (1.2.8)
bindex (0.5.0)
~~~~~~~略〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜

たくさん出てきました。次は配布されているgemを探してみましょう。せっかくなのでrails関連のgemを探します。
gem search -r railsでrailsに関わるgemを探せます。

$ gem search -r rails

*** REMOTE GEMS ***

aa-rails4 (0.6.0)
aaronchi-jrails (0.5.1)
aavkontakte-rails3 (0.1.9)
ab-experiments-rails (0.0.3)
abcjs-rails (3.0.1)
access-granted-rails (0.1.0)
access-watch-rails (0.0.3)
access_policy_rails (0.0.2)
access_watch_rails (0.1.2)
accessible-bootstrap3-rails (0.2.4)
account_kit_rails (0.0.0)
accountingjs-rails (0.0.4)
ace-rails (0.0.2)
~~~~~~~~~~~~~~~~~~略~~~~~~~~~~~~~~~~~~~~~~~~~~

たくさん出てきました。まあでもコンソールで探すより、ブラウザ上で探したいですよね。なので探す時は下記のページかgithubから探すのが良いかと思います。
ruby-toolbox
RubyGems.org

インストールする時はgem install railsなどで好きなものをインストールできます。
ですが、このインストール方法はおすすめできません。bundlerなるもので管理するが吉です。

bundler

bundle installなどで使うbundler。こいつもgemの一つで、gemを管理するものです。
gemを複数使っていると、依存関係が出てきます。バージョンが〜だの、あのgemがない〜だの言われるので、面倒です。
それをbundlerで管理します。Gemfileなるものを使って管理します。
参考にブログ作った時に使ったGemfileを見てみます。

source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.7'
gem 'bcrypt',         '3.1.11'
gem 'will_paginate',           '3.1.5'
gem 'bootstrap-will_paginate', '1.0.0'
gem 'carrierwave',             '1.1.0'
gem 'mini_magick',             '4.7.0'
gem 'fog',                     '1.40.0'
gem 'haml-rails'
# Use sqlite3 as the database for Active Record

# Use Puma as the app server
gem 'puma', '~> 3.0'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'sqlite3', '1.3.13'
  gem 'byebug', platform: :mri
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '~> 3.0.5'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end


group :test do
  gem 'rails-controller-testing', '1.0.2'
  gem 'minitest-reporters',       '1.1.14'
  gem 'guard',                    '2.13.0'
  gem 'guard-minitest',           '2.4.4'
end

group :production do
  gem 'pg', '0.18.4'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

source 'https://rubygems.org'でホスティングサーバーを指定。
gem 'rails', '~> 5.0.7' 5.0.7以上5.1未満のrailsを指定。
group :production do 〜〜 end 本番環境で使うgemを指定

こんな感じで使うgemを記載していく。
Gemfileを書き終えたら、bundle installでGemfileに記載された通りにインストールする。
その際、Gemfile.lockを作成する。このファイルがgemのバージョンをロックしてくれている。こいつのおかげで使用するgemのバージョンが変わらない。gemが更新されても、使用するgemのバージョンはそのままにしておいてくれる。
Gemfile.lockを更新したい時はbundle updateを使う。

まとめ

なんとなくイメージできてきたところで今日は終わりにします。明日はじゃあ自分でgem作って見ようかなと思ってます。楽しそうです。楽しみです。重そうです。頑張ります。

参考にしたの

Bundlerの使い方
https://qiita.com/oshou/items/6283c2315dc7dd244aef

Bundlerでgemを実用的に扱うためのまとめ
https://qiita.com/iguchi1124/items/3c17e1275b9bee4138d7#%E7%92%B0%E5%A2%83%E3%81%94%E3%81%A8%E3%81%AB%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB%E3%81%99%E3%82%8Bgem%E3%82%92%E6%8C%87%E5%AE%9A%E3%81%99%E3%82%8B

38
39
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
38
39