Rubyの事始めにSinatra環境を構築する

#はじめに
Rubyをはじめよう！と思った際、"目指すところはRuby on Railsかな"と思った。
ただ、Rubyをちまちま学習するよりかは、より実践的に学びたかったので、
Sinatraベースで学習を進めていこうと思う。

(目指すところはRailsなのに、別のフレームワークを使ってRubyを勉強するって... :sweat: )

#gemでSinatraをちゃちゃっとインストールする
ゲムじゃなくてジェムね。

```
bash-3.2# gem instal sinatra
Fetching: rack-1.6.4.gem (100%)
Successfully installed rack-1.6.4
Fetching: tilt-2.0.2.gem (100%)
Successfully installed tilt-2.0.2
Fetching: rack-protection-1.5.3.gem (100%)
Successfully installed rack-protection-1.5.3
Fetching: sinatra-1.4.7.gem (100%)
Successfully installed sinatra-1.4.7
Parsing documentation for rack-1.6.4
Installing ri documentation for rack-1.6.4
Parsing documentation for tilt-2.0.2
Installing ri documentation for tilt-2.0.2
Parsing documentation for rack-protection-1.5.3
Installing ri documentation for rack-protection-1.5.3
Parsing documentation for sinatra-1.4.7
unable to convert "\x89" from ASCII-8BIT to UTF-8 for lib/sinatra/images/404.png, skipping
unable to convert "\x89" from ASCII-8BIT to UTF-8 for lib/sinatra/images/500.png, skipping
Installing ri documentation for sinatra-1.4.7
4 gems installed
bash-3.2#
```

#gemでSinatra-contribをちゃちゃっとインストールする
```
bash-3.2# gem install sinatra-contrib
Fetching: backports-3.6.8.gem (100%)
Successfully installed backports-3.6.8
Fetching: rack-test-0.6.3.gem (100%)
Successfully installed rack-test-0.6.3
Fetching: multi_json-1.11.2.gem (100%)
Successfully installed multi_json-1.11.2
Fetching: sinatra-contrib-1.4.6.gem (100%)
Successfully installed sinatra-contrib-1.4.6
Parsing documentation for backports-3.6.8
Installing ri documentation for backports-3.6.8
Parsing documentation for rack-test-0.6.3
Installing ri documentation for rack-test-0.6.3
Parsing documentation for multi_json-1.11.2
Installing ri documentation for multi_json-1.11.2
Parsing documentation for sinatra-contrib-1.4.6
Installing ri documentation for sinatra-contrib-1.4.6
4 gems installed
```

#gemでActive Recordをちゃちゃっとインストールする
```
bash-3.2# gem install activerecord
Fetching: i18n-0.7.0.gem (100%)
Successfully installed i18n-0.7.0
Fetching: thread_safe-0.3.5.gem (100%)
Successfully installed thread_safe-0.3.5
Fetching: tzinfo-1.2.2.gem (100%)
Successfully installed tzinfo-1.2.2
Fetching: minitest-5.8.4.gem (100%)
Successfully installed minitest-5.8.4
Fetching: activesupport-4.2.6.gem (100%)
Successfully installed activesupport-4.2.6
Fetching: builder-3.2.2.gem (100%)
Successfully installed builder-3.2.2
Fetching: activemodel-4.2.6.gem (100%)
Successfully installed activemodel-4.2.6
Fetching: arel-6.0.3.gem (100%)
Successfully installed arel-6.0.3
Fetching: activerecord-4.2.6.gem (100%)
Successfully installed activerecord-4.2.6
Parsing documentation for i18n-0.7.0
Installing ri documentation for i18n-0.7.0
Parsing documentation for thread_safe-0.3.5
Installing ri documentation for thread_safe-0.3.5
Parsing documentation for tzinfo-1.2.2
Installing ri documentation for tzinfo-1.2.2
Parsing documentation for minitest-5.8.4
Installing ri documentation for minitest-5.8.4
Parsing documentation for activesupport-4.2.6
unable to convert "\x84" from ASCII-8BIT to UTF-8 for lib/active_support/values/unicode_tables.dat, skipping
Installing ri documentation for activesupport-4.2.6
Parsing documentation for builder-3.2.2
Installing ri documentation for builder-3.2.2
Parsing documentation for activemodel-4.2.6
Installing ri documentation for activemodel-4.2.6
Parsing documentation for arel-6.0.3
Installing ri documentation for arel-6.0.3
Parsing documentation for activerecord-4.2.6
Installing ri documentation for activerecord-4.2.6
9 gems installed
bash-3.2#
```

#gemでsqliteを<del>ちゃちゃっと</del>インストールする

```
bash-3.2# gem install sqlite3
Fetching: sqlite3-1.3.11.gem (100%)
Building native extensions.  This could take a while...
ERROR:  Error installing sqlite3:
	ERROR: Failed to build gem native extension.

    /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby extconf.rb
checking for sqlite3.h... *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
	--with-opt-dir
	--without-opt-dir
	--with-opt-include
	--without-opt-include=${opt-dir}/include
	--with-opt-lib
	--without-opt-lib=${opt-dir}/lib
	--with-make-prog
	--without-make-prog
	--srcdir=.
	--curdir
	--ruby=/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby
	--with-sqlite3-dir
	--without-sqlite3-dir
	--with-sqlite3-include
	--without-sqlite3-include=${sqlite3-dir}/include
	--with-sqlite3-lib
	--without-sqlite3-lib=${sqlite3-dir}/
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/mkmf.rb:434:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
	from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/mkmf.rb:549:in `block in try_compile'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/mkmf.rb:502:in `with_werror'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/mkmf.rb:549:in `try_compile'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/mkmf.rb:1091:in `block in find_header'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/mkmf.rb:889:in `block in checking_for'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/mkmf.rb:340:in `block (2 levels) in postpone'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/mkmf.rb:310:in `open'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/mkmf.rb:340:in `block in postpone'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/mkmf.rb:310:in `open'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/mkmf.rb:336:in `postpone'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/mkmf.rb:888:in `checking_for'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/mkmf.rb:1090:in `find_header'
	from extconf.rb:30:in `<main>'


Gem files will remain installed in /Library/Ruby/Gems/2.0.0/gems/sqlite3-1.3.11 for inspection.
Results logged to /Library/Ruby/Gems/2.0.0/gems/sqlite3-1.3.11/ext/sqlite3/gem_make.out
bash-3.2#
```

ん？

>You have to install development tools first.

あーはいはい。have to ね。mustね。
で、デベロップメントツールってなんだよと思い、調べたら[ここ](http://stackoverflow.com/questions/18777079/cant-install-atomic-ruby-atomic-gem-in-rails-4-0)にうまこと書かれていた。

gem install atomic -V

で、結局、doesn't work....................(#ﾟДﾟ)y-~~ｲﾗｲﾗ

その後、ちゃちゃっと調べて、sqlite3-ruby なら行けるらしいので、以下を実行。

```
bash-3.2# gem install sqlite3-ruby
Fetching: sqlite3-ruby-1.3.3.gem (100%)

#######################################################

Hello! The sqlite3-ruby gem has changed it's name to just sqlite3.  Rather than
installing `sqlite3-ruby`, you should install `sqlite3`.  Please update your
dependencies accordingly.

Thanks from the Ruby sqlite3 team!

<3 <3 <3 <3

#######################################################

Successfully installed sqlite3-ruby-1.3.3
Parsing documentation for sqlite3-ruby-1.3.3
Installing ri documentation for sqlite3-ruby-1.3.3
1 gem installed
bash-3.2#
```

#ちゃちゃっとrbファイルを作って、Webサーバを起動する

```test.rb
require 'sinatra'
require 'sinatra/reloader'

get '/test/:parameter' do
  "Parameter is #{params['parameter']}."
end
```

```
bash-3.2# ruby test.rb 
[2016-03-30 12:51:06] INFO  WEBrick 1.3.1
[2016-03-30 12:51:06] INFO  ruby 2.0.0 (2015-04-13) [universal.x86_64-darwin15]
== Sinatra (v1.4.7) has taken the stage on 4567 for development with backup from WEBrick
[2016-03-30 12:51:06] INFO  WEBrick::HTTPServer#start: pid=26244 port=4567
```

![Capture 2016-03-30 12.53.59.png](https://qiita-image-store.s3.amazonaws.com/0/65994/9078485f-f217-e842-404e-dde6fb9f01d5.png)
表示できましたね( ´∀｀)bｸﾞｯ!

ちなみに、Webサーバを止める際は、Ctrl + Cでイケるらしいですが、
ターミナル落としちゃった場合は、lsofのポート指定でプロセスをkillすればよい。
(もっといい方法あるハズ...)

```
bash-3.2# lsof -i :4567
COMMAND  PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
ruby    9863 root    9u  IPv6 0xb1518348b52422a3      0t0  TCP localhost:tram (LISTEN)
ruby    9863 root   10u  IPv4 0xb1518348c1306b83      0t0  TCP localhost:tram (LISTEN)
bash-3.2# kill 9863
bash-3.2# kill 9863
bash: kill: (9863) - No such process
bash-3.2#
```

#まとめ
Sinatra環境を構築しました。sqliteのインストールに苦戦したくせに使ってねーじゃんか！
とか思いつつも今後使って行くことにします。