0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ruby環境構築2

Last updated at Posted at 2018-12-29

はじめに

前回、Rubyの全体像をまとめた。今回はRuby、rubygems、gem環境の構築についてまとめる。

概要

Rubyにはアドオンするパッケージ的は位置づけでgemがある。rubygemsはRubyをインストールすると付随してくるコンポーネントで、gemをインストールしてくれる。

ディレクトリ構成とインストール

ディレクトリ構成

全体の主要な構成は以下になる。
image.png

インストール

  1. rubyとrubygemsのインストール(紺色)

    最初にyum install rubyでrubyをインストールする。rubygemsもインストールされる。ディレクトリは/usr/bin/の配下だ。他にもライブラリやらなんやらがインストールされる。
    なお、rubygemsにはデフォルトのgemがいくつか入っている。これらは場所は/usr/share/gems/gems/にインストールされている。

  2. gemのインストール(緑色)

    アプリケーションで使うgemをインストールする。ここでは例としてstrptimeをインストールする。コマンドはgem install strptime。これにより/home/ec2-user/配下に./gem/が作られ、その配下のgem/にインストールされる。

  3. アプリ用ディレクトリ作成とプログラム作成

    mkdirでRubyAPPディレクトリを作り、rubyapp.rbを作成する。ec2app.rbの中ではstreptimeを使っている。

strptimeを要求

require 'time'
require 'strptime'

現在時刻を表示

now = Time.now

フォーマットを定義

formatter = Strftime.new('%Y-%m-%dT%H:%M:%S.%L %z')

変換

puts formatter.exec(now)


4. プログラム実行
   `ruby ec2rubyapp.rb`で実行。めでたしめでたし。

    ```bash
[ec2-user@ip-10-0-1-62 RubyAPP]$ ruby ec2app.rb
2018-12-29T09:54:18.776 +0000

ポイント

gemはユーザ単位にインストールされる

図をみて気づいたと思うが、gem installでインストールされるgemは/home/のユーザディレクトリにインストールされる。つまり、gemはユーザ単位でインストールされるのだ。2つのユーザでgem listコマンドをたたいた場合は、それぞれ以下となる。

  • ec2-user

[ec2-user@ip-10-0-1-62 ~]$ gem list
*** LOCAL GEMS ***
bigdecimal (1.2.0)
io-console (0.4.2)
json (1.7.7)
psych (2.0.0)
rdoc (4.0.0)
strptime (0.2.3)

* tmp-user

    ```bash
[tmp-user@ip-10-0-1-62 ~]$ gem list
*** LOCAL GEMS ***
bigdecimal (1.2.0)
io-console (0.4.2)
json (1.7.7)
psych (2.0.0)
rdoc (4.0.0)
string-to-bool (0.0.1)

ユーザごとにgemのリストが異なることが分かる。/usr/share/gemsにインストールされたデフォルトのgemは、どちらでも使える。

まとめ

Ruby自体は全体にインストールされるがgemはユーザに紐づく。だからrubyのアプリケーションは各々のホームディレクトリに作るのがよい。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?