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 3 years have passed since last update.

Windows上でRubyを使えるようにする

Last updated at Posted at 2021-07-24

はじめに

Ruby製OSS(Redmineのプラグイン)を修正する機会があったがうまく動かなかった。
そのため、ローカルにRubyの開発環境を用意して調査することにした。
本記事は、Windows上にRubyを実行できる環境を用意した際の作業ログです。

IDE(エディタ)の選定

ググったところ、いくつかのIDEが見つかった1 2
JetBrainsのツールであるRubyMineが気になったが、有償だったのでとりあえずはVSCodeで済ませることにした。

環境情報

  • Windows 10
  • Ruby 2.4.10-12.6.8-1
  • Visual Studio Code 1.58.2
  • Ruby (VSCodeのプラグイン) v0.28.1

作業ログ

Rubyのインストール

以下から、RubyInstallerをダウンロードする。

Redmineのバージョン3に合わせて2.4を使用する。
2.4はサポートが終了している4が、ダウンロード元のアーカイブのページからダウンロードできる。

ダウンロードしたインストーラーを実行する。

License Agreement

Destination and Optional Tasks

Select Components

Finish

全てデフォルトの設定でインストールを進めた。

インストール後、コマンドプロンプトが立ち上がり、MSYS2のインストールが始まった。

Install MSYS2 and MINGW

  • MYSY2
    • CygwingのようなWindowsでBash等を実行したりできるターミナルツール5
  • MINGW
    • Windowsでgccを利用するためのツール6

両者ともインストールすることが推奨されているようなので73を指定する。

Installation succeeded

C:\Windows\system32>ruby -v
ruby 2.4.10p364 (2020-03-31 revision 67879) [x64-mingw32]

Rubyのインストールはできた!

VSCodeプラグインのインストール

VSCodeはインストール済。
VSCodeのExtemsionsから、rubyのキーワードで検索。

plugins

一番上のスターが付いているRubyという名前のプラグインが評価も高く、ぱっと見一番ダウンロード数が多いので使ってみる。
上図だと、最新バージョンのv0.28.1ではCIとビルドが通ってないように見えるが、リリースされてるので気にせず使う。

installボタンを押下し、対象プラグインをインストールする。

サンプルプログラムを動かす

ワーキングディレクトリを作成し、helloworld.rbという名前のファイルを作成。

working directory

作成したファイルをVSCodeで開き、以下の行を追加する。

print "Hello World!"

VSCode上部のTerminal > New Terminalの順に押下し、コンソール(PowerShell)を開く。

terminal

PS C:\Users\1417n> cd .\work\
PS C:\Users\1417n\work> ruby .\helloworld.rb
Hello World!

Rubyを実行できることを確認できた。

ライブラリを使用してみる

HTTPクライアントのライブラリであるnet/httpを試す。

まずは、net/httpをインストールする。

PS C:\Users\1417n\work> gem install net-http
Fetching: timeout-0.1.1.gem (100%)
Successfully installed timeout-0.1.1
Fetching: io-wait-0.1.0.gem (100%)
Successfully installed io-wait-0.1.0
Fetching: net-protocol-0.1.1.gem (100%)
Successfully installed net-protocol-0.1.1
Fetching: uri-0.10.1.gem (100%)
Successfully installed uri-0.10.1
Fetching: net-http-0.1.1.gem (100%)
ERROR:  Error installing net-http:
        net-http requires Ruby version >= 2.6.0.

失敗した。
Rubyのバージョンが2.6.0以上である必要があるとのこと。
Ruby 2.4を使いたかったが、2.6代にバージョンアップする。
WindowsでRubyをバージョンアップするには、インストールと同様にRubyInstallerを使えばいいらしい8

以下のバージョンにアップデートする。
https://github.com/oneclick/rubyinstaller2/blob/master/CHANGELOG-2.6.md#rubyinstaller-268-1---2020-07-09

手順はインストールと同様なので、ログは省略。

PS C:\Users\1417n> ruby -v
ruby 2.6.8p205 (2021-07-07 revision 67951) [x64-mingw32]

再度、net-httpのインストールをトライ。

PS C:\Users\1417n> gem install net-http
Fetching net-http-0.1.1.gem
Fetching timeout-0.1.1.gem
Fetching io-wait-0.1.0.gem
Fetching net-protocol-0.1.1.gem
Fetching uri-0.10.1.gem
Successfully installed timeout-0.1.1
Successfully installed io-wait-0.1.0
Successfully installed net-protocol-0.1.1
Successfully installed uri-0.10.1
Successfully installed net-http-0.1.1
Parsing documentation for timeout-0.1.1
Installing ri documentation for timeout-0.1.1
Parsing documentation for io-wait-0.1.0
Installing ri documentation for io-wait-0.1.0
Parsing documentation for net-protocol-0.1.1
Installing ri documentation for net-protocol-0.1.1
Parsing documentation for uri-0.10.1
Installing ri documentation for uri-0.10.1
Parsing documentation for net-http-0.1.1
Installing ri documentation for net-http-0.1.1
Done installing documentation for timeout, io-wait, net-protocol, uri, net-http after 1 seconds
5 gems installed

インストールできたので、ライブラリを使ってみる。
今回は、阿部寛さんのサイトにGETリクエストを送信し、得られたhtmlを標準出力するスクリプトを作成する。

require 'net/http'

print Net::HTTP.get('abehiroshi.la.coocan.jp', '/')

作成したスクリプトを実行。

PS C:\Users\1417n\work> ruby .\try_nethttp.rb
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<meta name="GENERATOR" content="JustSystems Homepage Builder Version 20.0.6.0 for Windows">
<meta http-equiv="Content-Style-Type" content="text/css">
<title>�������̃z�[���y�[�W</title>
</head>
<frameset cols=18,82>
  <frame src="menu.htm" marginheight="0" marginwidth="0" scrolling="auto" name="left">
  <frame src="top.htm" marginheight="0" marginwidth="0" scrolling="auto" name="right">
  <noframes>
  <body></body>
  </noframes>
</frameset>
</html>

文字化けしてるが、ダウンロードできた。

本記事の内容は、以上となります。

  1. https://www.ruby.or.jp/ja/tech/install/dev_env/

  2. https://techplay.jp/column/549

  3. https://github.com/sameersbn/docker-redmine/tree/4.1.1-8

  4. https://www.ruby-lang.org/ja/news/2020/04/05/support-of-ruby-2-4-has-ended/

  5. https://www.msys2.org/

  6. https://www.javadrive.jp/cstart/install/index6.html

  7. https://github.com/oneclick/rubyinstaller2/tree/RubyInstaller-2.4.10-1#using-the-installer-on-a-target-system

  8. https://www.buildinsider.net/language/rubytips/0004

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?