2012年9月26日水曜日

[Rails]ルーティングについて-初級編-

Clip to Evernote
今回はルーティングの話。初級編ですー

Railsでは、URLから特定のコントローラとアクションを選ぶことを『ルーティング』と呼びます。
ルーティングの設定は、 /config/routes.rb で指定します。
こんな感じですね↓

アプリケーション名::Application.routes.draw do   ルーティングを記述する
end
実際に、アプリケーションに新しいactionを追加して、ルーティングの設定をしてみます。

routes.rb に以下を追加

この設定により、『/about』というパスでアクセスが来た時に、TopControllerのaboutアクションを呼び出すようになります。
Study::Application.routes.draw do
root to: "top#index"
get "about" => "top#about", as: "about" # この行を追加
end
view raw routes.rb hosted with ❤ by GitHub


TopControllerにaboutアクションを追加

aboutアクションは特に何もしません。
# coding: utf-8
class TopController < ApplicationController
def index
@message = "こんにちは!"
end
def about # このアクションを追加
end
end

viewを作成

about.html.erbを作成します。ついでに、ちょっとインスタンス変数も使ってます。
<% @page_title = "このサイトについて" %>
<h1><%= @page_title %></h1>
<p>このサイトはRailsの勉強をするサイトです。</p>
view raw about.html.erb hosted with ❤ by GitHub

サーバを起動

サーバを起動します。
rails server
ブラウザから、『http://localhost:3000/about』にアクセスしてみましょう。
こんな表示になればOKです。


[Ruby]日本語使用時の注意

Clip to Evernote
今回は文字コードの話が出たのでメモ

ソースコードに日本語を使用する場合の注意点

ソースコードを保存する際は、『UTF-8』が推奨されていますが、
ソースコード内に、コメント以外の定数や出力などで、日本語などのマルチバイトを使用する場合は、マジックコメントが必要だそうです。

ソースを書いて実際にやってみましょう。
単純に標準出力に日本語を表示するだけのソースです。
# coding: utf-8 # ←これがマジックコメント
puts "はじめまして。"
実行するとこんな感じで表示されます。

$ ruby magic_comments.rb
はじめまして。
このマジックコメントで、ちゃんと表示されるわけですね。

Rubyの文字コードの解釈について

Rubyのデフォルトの文字コードは『US-ASCII』で、マジックコメントがないと、エラーになるんですね。
ソース内に日本語がある場合は注意が必要です。
ちなみに、文字コードがShift-JISの場合は、
# coding: Shift_JIS
と記述すると良いようです。

2012年9月25日火曜日

[Rails][Mac]アプリケーションを作成する

Clip to Evernote
今回はローカルで、アプリケーションを作成する手順のまとめです。
※rubyのインストール等は終わってる前提です。インストール手順などの詳細はこちらを参考にしてください。

まずはRailsのインストール

適当なディレクトリを作成します。
$ mkdir rails
で、作成したディレクトリに移動します。
$ cd rails
そこにrailsをインストールします。
$ gem install rails
以下のようにいろいろ必要なものをインストールしてくれます。

gem install rails
Fetching: i18n-0.6.1.gem (100%)
Fetching: activesupport-3.2.8.gem (100%)
Fetching: builder-3.0.3.gem (100%)
Fetching: activemodel-3.2.8.gem (100%)
Fetching: rack-1.4.1.gem (100%)
Fetching: rack-cache-1.2.gem (100%)
Fetching: rack-test-0.6.1.gem (100%)
Fetching: journey-1.0.4.gem (100%)
Fetching: hike-1.2.1.gem (100%)
Fetching: tilt-1.3.3.gem (100%)
Fetching: sprockets-2.1.3.gem (100%)
Fetching: erubis-2.7.0.gem (100%)
Fetching: actionpack-3.2.8.gem (100%)
Fetching: arel-3.0.2.gem (100%)
Fetching: tzinfo-0.3.33.gem (100%)
Fetching: activerecord-3.2.8.gem (100%)
Fetching: activeresource-3.2.8.gem (100%)
Fetching: polyglot-0.3.3.gem (100%)
Fetching: treetop-1.4.10.gem (100%)
Fetching: mail-2.4.4.gem (100%)
Fetching: actionmailer-3.2.8.gem (100%)
Fetching: rack-ssl-1.3.2.gem (100%)
Fetching: thor-0.16.0.gem (100%)
Fetching: json-1.7.5.gem (100%)
Building native extensions.  This could take a while...
Fetching: rdoc-3.12.gem (100%)
Depending on your version of ruby, you may need to install ruby rdoc/ri data:
<= 1.8.6 : unsupported
 = 1.8.7 : gem install rdoc-data; rdoc-data --install
 = 1.9.1 : gem install rdoc-data; rdoc-data --install
>= 1.9.2 : nothing to do! Yay!
Fetching: railties-3.2.8.gem (100%)
Fetching: rails-3.2.8.gem (100%)
Successfully installed i18n-0.6.1
Successfully installed activesupport-3.2.8
Successfully installed builder-3.0.3
Successfully installed activemodel-3.2.8
Successfully installed rack-1.4.1
Successfully installed rack-cache-1.2
Successfully installed rack-test-0.6.1
Successfully installed journey-1.0.4
Successfully installed hike-1.2.1
Successfully installed tilt-1.3.3
Successfully installed sprockets-2.1.3
Successfully installed erubis-2.7.0
Successfully installed actionpack-3.2.8
Successfully installed arel-3.0.2
Successfully installed tzinfo-0.3.33
Successfully installed activerecord-3.2.8
Successfully installed activeresource-3.2.8
Successfully installed polyglot-0.3.3
Successfully installed treetop-1.4.10
Successfully installed mail-2.4.4
Successfully installed actionmailer-3.2.8
Successfully installed rack-ssl-1.3.2
Successfully installed thor-0.16.0
Successfully installed json-1.7.5
Successfully installed rdoc-3.12
Successfully installed railties-3.2.8
Successfully installed rails-3.2.8
27 gems installed
Installing ri documentation for i18n-0.6.1...
Installing ri documentation for activesupport-3.2.8...
Installing ri documentation for builder-3.0.3...
Installing ri documentation for activemodel-3.2.8...
Installing ri documentation for rack-1.4.1...
Installing ri documentation for rack-cache-1.2...
Installing ri documentation for rack-test-0.6.1...
Installing ri documentation for journey-1.0.4...
Installing ri documentation for hike-1.2.1...
Installing ri documentation for tilt-1.3.3...
Installing ri documentation for sprockets-2.1.3...
Installing ri documentation for erubis-2.7.0...
Installing ri documentation for actionpack-3.2.8...
Installing ri documentation for arel-3.0.2...
Installing ri documentation for tzinfo-0.3.33...
Installing ri documentation for activerecord-3.2.8...
Installing ri documentation for activeresource-3.2.8...
Installing ri documentation for polyglot-0.3.3...
Installing ri documentation for treetop-1.4.10...
Installing ri documentation for mail-2.4.4...
Installing ri documentation for actionmailer-3.2.8...
Installing ri documentation for rack-ssl-1.3.2...
Installing ri documentation for thor-0.16.0...
Installing ri documentation for json-1.7.5...
Installing ri documentation for rdoc-3.12...
Installing ri documentation for railties-3.2.8...
Installing ri documentation for rails-3.2.8...
Installing RDoc documentation for i18n-0.6.1...
Installing RDoc documentation for activesupport-3.2.8...
Installing RDoc documentation for builder-3.0.3...
Installing RDoc documentation for activemodel-3.2.8...
Installing RDoc documentation for rack-1.4.1...
Installing RDoc documentation for rack-cache-1.2...
Installing RDoc documentation for rack-test-0.6.1...
Installing RDoc documentation for journey-1.0.4...
Installing RDoc documentation for hike-1.2.1...
Installing RDoc documentation for tilt-1.3.3...
Installing RDoc documentation for sprockets-2.1.3...
Installing RDoc documentation for erubis-2.7.0...
Installing RDoc documentation for actionpack-3.2.8...
Installing RDoc documentation for arel-3.0.2...
Installing RDoc documentation for tzinfo-0.3.33...
Installing RDoc documentation for activerecord-3.2.8...
Installing RDoc documentation for activeresource-3.2.8...
Installing RDoc documentation for polyglot-0.3.3...
Installing RDoc documentation for treetop-1.4.10...
Installing RDoc documentation for mail-2.4.4...
Installing RDoc documentation for actionmailer-3.2.8...
Installing RDoc documentation for rack-ssl-1.3.2...
Installing RDoc documentation for thor-0.16.0...
Installing RDoc documentation for json-1.7.5...
Installing RDoc documentation for rdoc-3.12...
Installing RDoc documentation for railties-3.2.8...
Installing RDoc documentation for rails-3.2.8...

これでインストールは完了。
※インストールするrailsのバージョンを指定したい場合は、
gem install rails --version "~> 3.2.1" --no-rdoc --no-ri
のコマンドを叩くと良いです。

アプリケーションを作成する

アプリケーションを作成します。
$ rails new study --skip-bundle
rails newコマンドで、『study』ディレクトリを作成して、Railsアプリケーションに必要なフォルダやファイルを作ってくれます。

      create
      create  README.rdoc
      create  Rakefile
      create  config.ru
      create  .gitignore
      create  Gemfile
      create  app
      create  app/assets/images/rails.png
      create  app/assets/javascripts/application.js
      create  app/assets/stylesheets/application.css
      create  app/controllers/application_controller.rb
      create  app/helpers/application_helper.rb
      create  app/mailers
      create  app/models
      create  app/views/layouts/application.html.erb
      create  app/mailers/.gitkeep
      create  app/models/.gitkeep
      create  config
      create  config/routes.rb
      create  config/application.rb
      create  config/environment.rb
      create  config/environments
      create  config/environments/development.rb
      create  config/environments/production.rb
      create  config/environments/test.rb
      create  config/initializers
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/secret_token.rb
      create  config/initializers/session_store.rb
      create  config/initializers/wrap_parameters.rb
      create  config/locales
      create  config/locales/en.yml
      create  config/boot.rb
      create  config/database.yml
      create  db
      create  db/seeds.rb
      create  doc
      create  doc/README_FOR_APP
      create  lib
      create  lib/tasks
      create  lib/tasks/.gitkeep
      create  lib/assets
      create  lib/assets/.gitkeep
      create  log
      create  log/.gitkeep
      create  public
      create  public/404.html
      create  public/422.html
      create  public/500.html
      create  public/favicon.ico
      create  public/index.html
      create  public/robots.txt
      create  script
      create  script/rails
      create  test/fixtures
      create  test/fixtures/.gitkeep
      create  test/functional
      create  test/functional/.gitkeep
      create  test/integration
      create  test/integration/.gitkeep
      create  test/unit
      create  test/unit/.gitkeep
      create  test/performance/browsing_test.rb
      create  test/test_helper.rb
      create  tmp/cache
      create  tmp/cache/assets
      create  vendor/assets/javascripts
      create  vendor/assets/javascripts/.gitkeep
      create  vendor/assets/stylesheets
      create  vendor/assets/stylesheets/.gitkeep
      create  vendor/plugins
      create  vendor/plugins/.gitkeep
『--skip-bundle』オプションをつけているのは、『bundle install』の実行を省略するためです。オプションを付けないと、『bundle install』が自動実行されます。

次にBundlerでGemパッケージを追加します。
$ cd study
$ bundle install
Fetching gem metadata from https://rubygems.org/.........
Using rake (0.9.2.2)
Using i18n (0.6.1)
Using multi_json (1.3.6)
Using activesupport (3.2.8)
Using builder (3.0.3)
Using activemodel (3.2.8)
Using erubis (2.7.0)
Using journey (1.0.4)
Using rack (1.4.1)
Using rack-cache (1.2)
Using rack-test (0.6.1)
Using hike (1.2.1)
Using tilt (1.3.3)
Using sprockets (2.1.3)
Using actionpack (3.2.8)
Using mime-types (1.19)
Using polyglot (0.3.3)
Using treetop (1.4.10)
Using mail (2.4.4)
Using actionmailer (3.2.8)
Using arel (3.0.2)
Using tzinfo (0.3.33)
Using activerecord (3.2.8)
Using activeresource (3.2.8)
Using bundler (1.2.0)
Installing coffee-script-source (1.3.3)
Installing execjs (1.4.0)
Installing coffee-script (2.2.0)
Using rack-ssl (1.3.2)
Using json (1.7.5)
Using rdoc (3.12)
Using thor (0.16.0)
Using railties (3.2.8)
Installing coffee-rails (3.2.2)
Installing jquery-rails (2.1.2)
Using rails (3.2.8)
Installing sass (3.2.1)
Installing sass-rails (3.2.5)
Installing sqlite3 (1.3.6) with native extensions
Installing uglifier (1.3.0)
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed. 
これでインストール完了。
SQLite3とかいろいろ必要なパッケージがインストールされました。

アプリケーションを起動する

先ほど作ったアプリケーションのフォルダに移動して、サーバを起動します。
Rubyに付属されているWEBrickというウェブサーバが起動されます。
$ rails server
=> Booting WEBrick
=> Rails 3.2.8 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2012-09-24 07:52:41] INFO  WEBrick 1.3.1
[2012-09-24 07:52:41] INFO  ruby 1.9.3 (2012-04-20) [x86_64-darwin11.4.0]
[2012-09-24 07:52:41] INFO  WEBrick::HTTPServer#start: pid=57972 port=3000

Started GET "/assets/rails.png" for 127.0.0.1 at 2012-09-24 07:52:54 +0900
Connecting to database specified by database.yml
Served asset /rails.png - 200 OK (9ms) 
これで起動しました。
ブラウザを上げて、『http://localhost:3000』にアクセスしてみましょう。
以下のような画面が出ればOK
終了する場合は、『control』+『c』で終わります。

アプリケーションの構成を見てみる

Macでアプリケーションを作成すると、こんな感じ。

いろいろとフォルダができますが、各フォルダの役割は簡単に言うとこんな感じ。
app モデル、ビュー、コントローラのコードを置く
config ルーティングやデータベースなどの設定ファイルを置く
db マイグレーションスクリプトやシードデータを置く
doc 開発者向けのドキュメントを置く
lib 自作ライブラリやrakeファイルを置く
log ログが出力される
public アプリケーションを介さずに、静的ファイルを置く
script スクリプトファイルを置く
test テストケースを書いたテストファイルを置く
tmp キャッシュなどのテンポラリファイル
vender プラグインなどは配置する

appフォルダの下にもフォルダが分かれており、それぞれに役割があります。
appの下にはファイル名規約があるので、次のサイトを参考にしましょう。
(参考:Railsドキュメント Railsの基礎知識)

コントローラとアクションの作成

ようやくコーディングっぽいところに到達(笑)
Railsの初期画面だと味気ないので、コントローラを作成します。
コンソールに戻って、『rails generate コントローラ名 アクション名』コマンドを実行します。
generateは省略もできて、『rails g 〜』とも書けるようです。
まあ、ともかくコマンド実行しましょう。
$ rails g controller top index
      create  app/controllers/top_controller.rb
       route  get "top/index"
      invoke  erb
      create    app/views/top
      create    app/views/top/index.html.erb
      invoke  test_unit
      create    test/functional/top_controller_test.rb
      invoke  helper
      create    app/helpers/top_helper.rb
      invoke    test_unit
      create      test/unit/helpers/top_helper_test.rb
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/top.js.coffee
      invoke    scss
      create      app/assets/stylesheets/top.css.scss
TopControllerとindexアクションがこれで作成されました。
実際にファイルを見てみましょうー。
app/controllers/top_controller.rb
class TopController < ApplicationController
def index
end
end
TopControllerクラスの中に、indexメソッドができてます。
これが、トップページを表示するアクションになります。

次に、ルーティングの設定をします。
config/route.rb を開いて、以下のように書き換えます。
class TopController < ApplicationController
def index
end
end

書き換えたら、public/index.htmlファイルを削除します。
これを消さないと、また同じ画面が表示されることに!!

最後に、ローカルサーバを起動します。
rails server 
以下のように表示されればOK!!

ビューを作成する

次に、indexアクションに対応するテンプレートを編集します。
先ほどの画面を表示していたHTMLですね。
Railsのビューのためのテンプレートは、『アクション名』+『.html』+『.erb』というファイル名になります。(HTMLの場合は)
/app/view/top/index.html.erb を開いて、以下のように修正します。
<h1>Ruby on Rails</h1>
<p>Hello Wolrd!</p>
view raw index.html.erb hosted with ❤ by GitHub

ブラウザを再読み込みすると、表示が変わってますね。

変数を表示する

ベタ書きだとつまらんので、変数を使ってみます。
/app/controller/top_controller.rb
@messageを定義して、文字を代入します。
# coding: utf-8
class TopController < ApplicationController
def index
@message = "こんにちは!"
end
end
テンプレートの方もmessageを読み込むように修正します。変数をテンプレートで読み込む場合は、<%=  %>で変数を囲むんですな。
<h1>Ruby on Rails</h1>
<p><%= @message %></p>
view raw index2.html.erb hosted with ❤ by GitHub
ブラウザをリロードするとこんな感じ。
以上、アプリケーション作成入門でした!

2012年9月17日月曜日

[Mac][Heroku]MacでHeroku環境構築

Clip to Evernote
前回のMacでRuby環境構築でRubyの環境構築までできたので、
今回はHerokuを使ってみようの会ですw

今回の参考先もmacを買って、今すぐherokuでruby1.9.3 + rails3.2しよう!ですww
ありがとうございますm(__)m

Herokuの登録は
macを買って、今すぐherokuでruby1.9.3 + rails3.2しよう!
の『herokuアカウントを作成』から読んでね。

アカウントが作成出来たら、参考先をそのまま続けてやってきましょうー。


ssh公開鍵を作成する


以下コマンドを打って、Enterを押すと出来上がり。
$ ssh-keygen -b 2048
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/xxxx/.ssh/id_rsa):   ←Enter押す
Created directory '/Users/xxxx/.ssh'.
Enter passphrase (empty for no passphrase):    ←入力せずにEnter押す
Enter same passphrase again:    ←入力せずEnter押す
Your identification has been saved in /Users/xxxx/.ssh/id_rsa.
Your public key has been saved in /Users/xxxx/.ssh/id_rsa.pub.
fingerprintとか表示されるので、眺めて終了。
作った公開鍵を登録
heroku keys:add

rvm gemsetを作成する

rvm gemsetってなんじゃらホイと思ってぐぐってみました。
RVM gemset とは?によると、rubyのバージョンと使用用途によってgemの組み合わせやバージョンを管理できるようになる仕組みのようです。
開発環境によって、組み合わせが変わったりできるとか。
次のサイトで、賢い使い方がわかりやすく載ってたので、使う場合は参考にすると良いかと。(参考サイト:rvm のgemsetをおさらい(Mac でRuby))
とりあえず、heroku用のgemsetを作って見ましょうー。
以下のコマンドでOK。
rvm --create 1.9.3-p194@heroku-sample
 gemでherokuをインストールもしちゃいます。

$ gem install heroku --no-ri --no-rdoc
Fetching: excon-0.16.2.gem (100%)
Fetching: heroku-api-0.3.5.gem (100%)
Fetching: netrc-0.7.7.gem (100%)
Fetching: mime-types-1.19.gem (100%)
Fetching: rest-client-1.6.7.gem (100%)
Fetching: addressable-2.3.2.gem (100%)
Fetching: launchy-2.1.2.gem (100%)
Fetching: rubyzip-0.9.9.gem (100%)
Fetching: heroku-2.31.2.gem (100%)
 !    Heroku recommends using the Heroku Toolbelt to install the CLI.
 !    Download it from: https://toolbelt.heroku.com
Successfully installed excon-0.16.2
Successfully installed heroku-api-0.3.5
Successfully installed netrc-0.7.7
Successfully installed mime-types-1.19
Successfully installed rest-client-1.6.7
Successfully installed addressable-2.3.2
Successfully installed launchy-2.1.2
Successfully installed rubyzip-0.9.9
Successfully installed heroku-2.31.2
9 gems installed

Herokuにログインする

 herokuにログインします。
heroku login

herokuアカウントのメアドとパスワードを入力して
Authentication successful. って言われたらOK。

rails3.2.2をインストール

railsをインストールします。
2012/09/17時点で、railsの最新が3.2.8だったので、それを入れてみます。
gem install rails --version 3.2.8 --no-ri --no-rdoc
gem install rails --version 3.2.8 --no-ri --no-rdoc
Fetching: i18n-0.6.1.gem (100%)
Fetching: multi_json-1.3.6.gem (100%)
Fetching: activesupport-3.2.8.gem (100%)
Fetching: builder-3.0.3.gem (100%)
Fetching: activemodel-3.2.8.gem (100%)
Fetching: rack-1.4.1.gem (100%)
Fetching: rack-cache-1.2.gem (100%)
Fetching: rack-test-0.6.1.gem (100%)
Fetching: journey-1.0.4.gem (100%)
Fetching: hike-1.2.1.gem (100%)
Fetching: tilt-1.3.3.gem (100%)
Fetching: sprockets-2.1.3.gem (100%)
Fetching: erubis-2.7.0.gem (100%)
Fetching: actionpack-3.2.8.gem (100%)
Fetching: arel-3.0.2.gem (100%)
Fetching: tzinfo-0.3.33.gem (100%)
Fetching: activerecord-3.2.8.gem (100%)
Fetching: activeresource-3.2.8.gem (100%)
Fetching: polyglot-0.3.3.gem (100%)
Fetching: treetop-1.4.10.gem (100%)
Fetching: mail-2.4.4.gem (100%)
Fetching: actionmailer-3.2.8.gem (100%)
Fetching: rack-ssl-1.3.2.gem (100%)
Fetching: thor-0.16.0.gem (100%)
Fetching: json-1.7.5.gem (100%)
Building native extensions. This could take a while...
Fetching: rdoc-3.12.gem (100%)
Depending on your version of ruby, you may need to install ruby rdoc/ri data:
<= 1.8.6 : unsupported
= 1.8.7 : gem install rdoc-data; rdoc-data --install
= 1.9.1 : gem install rdoc-data; rdoc-data --install
>= 1.9.2 : nothing to do! Yay!
Fetching: railties-3.2.8.gem (100%)
Fetching: rails-3.2.8.gem (100%)
Successfully installed i18n-0.6.1
Successfully installed multi_json-1.3.6
Successfully installed activesupport-3.2.8
Successfully installed builder-3.0.3
Successfully installed activemodel-3.2.8
Successfully installed rack-1.4.1
Successfully installed rack-cache-1.2
Successfully installed rack-test-0.6.1
Successfully installed journey-1.0.4
Successfully installed hike-1.2.1
Successfully installed tilt-1.3.3
Successfully installed sprockets-2.1.3
Successfully installed erubis-2.7.0
Successfully installed actionpack-3.2.8
Successfully installed arel-3.0.2
Successfully installed tzinfo-0.3.33
Successfully installed activerecord-3.2.8
Successfully installed activeresource-3.2.8
Successfully installed polyglot-0.3.3
Successfully installed treetop-1.4.10
Successfully installed mail-2.4.4
Successfully installed actionmailer-3.2.8
Successfully installed rack-ssl-1.3.2
Successfully installed thor-0.16.0
Successfully installed json-1.7.5
Successfully installed rdoc-3.12
Successfully installed railties-3.2.8
Successfully installed rails-3.2.8
28 gems installed
こんなかんじでインストールされて完了。

アプリを作成する

railsアプリをまず作ります。
rails new heroku-sample
$ rails new heroku-sample
create
create README.rdoc
create Rakefile
create config.ru
create .gitignore
create Gemfile
create app
create app/assets/images/rails.png
create app/assets/javascripts/application.js
create app/assets/stylesheets/application.css
create app/controllers/application_controller.rb
create app/helpers/application_helper.rb
create app/mailers
create app/models
create app/views/layouts/application.html.erb
create app/mailers/.gitkeep
create app/models/.gitkeep
create config
create config/routes.rb
create config/application.rb
create config/environment.rb
create config/environments
create config/environments/development.rb
create config/environments/production.rb
create config/environments/test.rb
create config/initializers
create config/initializers/backtrace_silencers.rb
create config/initializers/inflections.rb
create config/initializers/mime_types.rb
create config/initializers/secret_token.rb
create config/initializers/session_store.rb
create config/initializers/wrap_parameters.rb
create config/locales
create config/locales/en.yml
create config/boot.rb
create config/database.yml
create db
create db/seeds.rb
create doc
create doc/README_FOR_APP
create lib
create lib/tasks
create lib/tasks/.gitkeep
create lib/assets
create lib/assets/.gitkeep
create log
create log/.gitkeep
create public
create public/404.html
create public/422.html
create public/500.html
create public/favicon.ico
create public/index.html
create public/robots.txt
create script
create script/rails
create test/fixtures
create test/fixtures/.gitkeep
create test/functional
create test/functional/.gitkeep
create test/integration
create test/integration/.gitkeep
create test/unit
create test/unit/.gitkeep
create test/performance/browsing_test.rb
create test/test_helper.rb
create tmp/cache
create tmp/cache/assets
create vendor/assets/javascripts
create vendor/assets/javascripts/.gitkeep
create vendor/assets/stylesheets
create vendor/assets/stylesheets/.gitkeep
create vendor/plugins
create vendor/plugins/.gitkeep
run bundle install
Fetching gem metadata from https://rubygems.org/.........
Using rake (0.9.2.2)
Using i18n (0.6.1)
Using multi_json (1.3.6)
Using activesupport (3.2.8)
Using builder (3.0.3)
Using activemodel (3.2.8)
Using erubis (2.7.0)
Using journey (1.0.4)
Using rack (1.4.1)
Using rack-cache (1.2)
Using rack-test (0.6.1)
Using hike (1.2.1)
Using tilt (1.3.3)
Using sprockets (2.1.3)
Using actionpack (3.2.8)
Using mime-types (1.19)
Using polyglot (0.3.3)
Using treetop (1.4.10)
Using mail (2.4.4)
Using actionmailer (3.2.8)
Using arel (3.0.2)
Using tzinfo (0.3.33)
Using activerecord (3.2.8)
Using activeresource (3.2.8)
Using bundler (1.2.0)
Installing coffee-script-source (1.3.3)
Installing execjs (1.4.0)
Installing coffee-script (2.2.0)
Using rack-ssl (1.3.2)
Using json (1.7.5)
Using rdoc (3.12)
Using thor (0.16.0)
Using railties (3.2.8)
Installing coffee-rails (3.2.2)
Installing jquery-rails (2.1.2)
Using rails (3.2.8)
Installing sass (3.2.1)
Installing sass-rails (3.2.5)
Installing sqlite3 (1.3.6) with native extensions
Installing uglifier (1.3.0)
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
mysqlとか使いたかったら、-d mysqlオプションとか入れるといいらしいです。
が、ここではデフォルトでいきますw
次に、サンプルアプリの中のディレクトリに移動します。
cd heroku-sample
gitを初期化します。
$ git init
Initialized empty Git repository in /Users/yukiko/Documents/heroku-sample/.git/
$ git add -A
$ git commit -m'init'
[master (root-commit) 1a20ea1] init
37 files changed, 1204 insertions(+)
create mode 100644 .gitignore
create mode 100644 Gemfile
create mode 100644 Gemfile.lock
create mode 100644 README.rdoc
create mode 100644 Rakefile
create mode 100644 app/assets/images/rails.png
create mode 100644 app/assets/javascripts/application.js
create mode 100644 app/assets/stylesheets/application.css
create mode 100644 app/controllers/application_controller.rb
create mode 100644 app/helpers/application_helper.rb
create mode 100644 app/mailers/.gitkeep
create mode 100644 app/models/.gitkeep
create mode 100644 app/views/layouts/application.html.erb
create mode 100644 config.ru
create mode 100644 config/application.rb
create mode 100644 config/boot.rb
create mode 100644 config/database.yml
create mode 100644 config/environment.rb
create mode 100644 config/environments/development.rb
create mode 100644 config/environments/production.rb
create mode 100644 config/environments/test.rb
create mode 100644 config/initializers/backtrace_silencers.rb
create mode 100644 config/initializers/inflections.rb
create mode 100644 config/initializers/mime_types.rb
create mode 100644 config/initializers/secret_token.rb
create mode 100644 config/initializers/session_store.rb
create mode 100644 config/initializers/wrap_parameters.rb
create mode 100644 config/locales/en.yml
create mode 100644 config/routes.rb
create mode 100644 db/seeds.rb
create mode 100644 doc/README_FOR_APP
create mode 100644 lib/assets/.gitkeep
create mode 100644 lib/tasks/.gitkeep
create mode 100644 log/.gitkeep
create mode 100644 public/404.html
create mode 100644 public/422.html
create mode 100644 public/500.html
create mode 100644 public/favicon.ico
create mode 100644 public/index.html
create mode 100644 public/robots.txt
create mode 100755 script/rails
create mode 100644 test/fixtures/.gitkeep
create mode 100644 test/functional/.gitkeep
create mode 100644 test/integration/.gitkeep
create mode 100644 test/performance/browsing_test.rb
create mode 100644 test/test_helper.rb
create mode 100644 test/unit/.gitkeep
create mode 100644 vendor/assets/javascripts/.gitkeep
create mode 100644 vendor/assets/stylesheets/.gitkeep
create mode 100644 vendor/plugins/.gitkeep
view raw git-init.txt hosted with ❤ by GitHub
gitの使い方は、Git入門で。
次に、最低限のgemを記述します。(といってもコンソールでコマンド叩くだけ)
$ curl https://raw.github.com/gist/1970532/376b6a26ed936ac43cad0b17a64512f5c0216a50/Gemifile > Gemfile
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 302 100 302 0 0 83 0 0:00:03 0:00:03 --:--:-- 109
$ rm -f Gemfile.lock
$ bundle install --without production
Fetching gem metadata from https://rubygems.org/.........
Using rake (0.9.2.2)
Using i18n (0.6.1)
Using multi_json (1.3.6)
Installing activesupport (3.2.2)
Using builder (3.0.3)
Installing activemodel (3.2.2)
Using erubis (2.7.0)
Using journey (1.0.4)
Using rack (1.4.1)
Using rack-cache (1.2)
Using rack-test (0.6.1)
Using hike (1.2.1)
Using tilt (1.3.3)
Using sprockets (2.1.3)
Installing actionpack (3.2.2)
Using mime-types (1.19)
Using polyglot (0.3.3)
Using treetop (1.4.10)
Using mail (2.4.4)
Installing actionmailer (3.2.2)
Using arel (3.0.2)
Using tzinfo (0.3.33)
Installing activerecord (3.2.2)
Installing activeresource (3.2.2)
Using addressable (2.3.2)
Using bundler (1.2.0)
Using coffee-script-source (1.3.3)
Using execjs (1.4.0)
Using coffee-script (2.2.0)
Using rack-ssl (1.3.2)
Using json (1.7.5)
Using rdoc (3.12)
Installing thor (0.14.6)
Installing railties (3.2.2)
Using coffee-rails (3.2.2)
Using excon (0.16.2)
Using heroku-api (0.3.5)
Using launchy (2.1.2)
Using netrc (0.7.7)
Using rest-client (1.6.7)
Using rubyzip (0.9.9)
Using heroku (2.31.2)
Using jquery-rails (2.1.2)
Installing rails (3.2.2)
Using sass (3.2.1)
Using sass-rails (3.2.5)
Using sqlite3 (1.3.6)
Using uglifier (1.3.0)
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
$ git add -A
$ git commit -m 'Update Gemtile'
[master 546179e] Update Gemtile
2 files changed, 77 insertions(+), 69 deletions(-)
rewrite Gemfile (77%)
view raw gem_write.txt hosted with ❤ by GitHub
Gemfileの中を見てみましょう。
vi Gemfile
source 'https://rubygems.org'
gem 'rails', '3.2.2'
gem 'jquery-rails'
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
end
group :test, :development do
gem 'sqlite3'
gem 'heroku'
end
group :production do
gem 'pg'
gem 'thin'
end
view raw Gemfile hosted with ❤ by GitHub
開発では、sqlite3で、本番だとpostgreSQLだったりするんですな。ふむふむ。

herokuにアプリを作成する

以下コマンドを実行
$ heroku create --stack cedar
Creating sultry-cliffs-6716... done, stack is cedar
http://sultry-cliffs-6716.herokuapp.com/ | git@heroku.com:sultry-cliffs-6716.git
Git remote heroku added
$ heroku labs:enable user_env_compile
$ heroku config:add RUBY_VERSION=ruby-1.9.3 # ←ruby-1.9.3-p194はherokuでまだ使えないみたいなので、バージョン騙すw
view raw heroku_create hosted with ❤ by GitHub
herokuにdeployする
git push heroku master
Counting objects: 67, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (53/53), done.
Writing objects: 100% (67/67), 26.85 KiB, done.
Total 67 (delta 5), reused 0 (delta 0)
-----> Heroku receiving push
-----> Ruby/Rails app detected
-----> Using RUBY_VERSION: ruby-1.9.3
WARNING: RUBY_VERSION support has been deprecated and will be removed entirely on August 1, 2012.
See https://devcenter.heroku.com/articles/ruby-versions#selecting_a_version_of_ruby for more information.
-----> Installing dependencies using Bundler version 1.2.0
Running: bundle install --without development:test --path vendor/bundle --binstubs bin/ --deployment
Fetching gem metadata from https://rubygems.org/.......
Installing rake (0.9.2.2)
Installing i18n (0.6.1)
Installing multi_json (1.3.6)
Installing activesupport (3.2.2)
Installing builder (3.0.3)
Installing activemodel (3.2.2)
Installing erubis (2.7.0)
Installing journey (1.0.4)
Installing rack (1.4.1)
Installing rack-cache (1.2)
Installing rack-test (0.6.1)
Installing hike (1.2.1)
Installing tilt (1.3.3)
Installing sprockets (2.1.3)
Installing actionpack (3.2.2)
Installing mime-types (1.19)
Installing polyglot (0.3.3)
Installing treetop (1.4.10)
Installing mail (2.4.4)
Installing actionmailer (3.2.2)
Installing arel (3.0.2)
Installing tzinfo (0.3.33)
Installing activerecord (3.2.2)
Installing activeresource (3.2.2)
Installing coffee-script-source (1.3.3)
Installing execjs (1.4.0)
Installing coffee-script (2.2.0)
Installing rack-ssl (1.3.2)
Installing json (1.7.5) with native extensions
Installing rdoc (3.12)
Installing thor (0.14.6)
Installing railties (3.2.2)
Installing coffee-rails (3.2.2)
Installing daemons (1.1.9)
Installing eventmachine (1.0.0) with native extensions
Installing jquery-rails (2.1.2)
Installing pg (0.14.1) with native extensions
Using bundler (1.2.0)
Installing rails (3.2.2)
Installing sass (3.2.1)
Installing sass-rails (3.2.5)
Installing thin (1.4.1) with native extensions
Installing uglifier (1.3.0)
Your bundle is complete! It was installed into ./vendor/bundle
Post-install message from rdoc:
Depending on your version of ruby, you may need to install ruby rdoc/ri data:
<= 1.8.6 : unsupported
= 1.8.7 : gem install rdoc-data; rdoc-data --install
= 1.9.1 : gem install rdoc-data; rdoc-data --install
>= 1.9.2 : nothing to do! Yay!
Cleaning up the bundler cache.
-----> Writing config/database.yml to read from DATABASE_URL
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
Asset precompilation completed (9.97s)
-----> Rails plugin injection
Injecting rails_log_stdout
Injecting rails3_serve_static_assets
-----> Discovering process types
Procfile declares types -> (none)
Default types for Ruby/Rails -> console, rake, web, worker
-----> Compiled slug size is 20.3MB
-----> Launching... done, v7
http://sultry-cliffs-6716.herokuapp.com deployed to Heroku
To git@heroku.com:sultry-cliffs-6716.git
* [new branch] master -> master
view raw heroku_deploy hosted with ❤ by GitHub
どうやらうまくいったみたい。
作成したアプリを見てみましょう。
以下、コマンドを打つと、safariが起動します。
heroku open
ようやく完了!

scaffoldでアプリを作る

まずは、デフォルト表示されるindex.htmlを消します。
$ rm -f public/index.html
$ git add -A
$ git commit -m'Delete default index.html'
[master 0aff2b8] Delete default index.html
1 file changed, 241 deletions(-)
delete mode 100644 public/index.html
scaffoldでブックマークアプリを作ります。
$ rails g scaffold bookmark name:string url:string
invoke active_record
create db/migrate/20120917103255_create_bookmarks.rb
create app/models/bookmark.rb
invoke test_unit
create test/unit/bookmark_test.rb
create test/fixtures/bookmarks.yml
route resources :bookmarks
invoke scaffold_controller
create app/controllers/bookmarks_controller.rb
invoke erb
create app/views/bookmarks
create app/views/bookmarks/index.html.erb
create app/views/bookmarks/edit.html.erb
create app/views/bookmarks/show.html.erb
create app/views/bookmarks/new.html.erb
create app/views/bookmarks/_form.html.erb
invoke test_unit
create test/functional/bookmarks_controller_test.rb
invoke helper
create app/helpers/bookmarks_helper.rb
invoke test_unit
create test/unit/helpers/bookmarks_helper_test.rb
invoke assets
invoke coffee
create app/assets/javascripts/bookmarks.js.coffee
invoke scss
create app/assets/stylesheets/bookmarks.css.scss
invoke scss
create app/assets/stylesheets/scaffolds.css.scss
$ rake db:migrate
== CreateBookmarks: migrating ================================================
-- create_table(:bookmarks)
-> 0.0012s
== CreateBookmarks: migrated (0.0013s) =======================================
$ sed -i -e "s/ # root:to => 'welcome#index'/ root:to => 'bookmarks#index'/" config/routes.rb
$ git add -A
$ git commit -m 'rails g scaffold bookmark'
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: config/routes.rb
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# app/assets/javascripts/bookmarks.js.coffee
# app/assets/stylesheets/bookmarks.css.scss
# app/assets/stylesheets/scaffolds.css.scss
# app/controllers/bookmarks_controller.rb
# app/helpers/bookmarks_helper.rb
# app/models/bookmark.rb
# app/views/bookmarks/
# config/routes.rb-e
# db/migrate/
# db/schema.rb
# test/fixtures/bookmarks.yml
# test/functional/bookmarks_controller_test.rb
# test/unit/bookmark_test.rb
# test/unit/helpers/
no changes added to commit (use "git add" and/or "git commit -a")
migrationするので、一旦メンテナンスモードにします。
migrationとは、Rubyスクリプトによって、テーブルの作成やカラムの構成変更を行う機能のこと。
詳しくは、次のサイトを参考にしてください。
(参考サイト:Ruby on Rails : migration 機能でデータベーススキーマを変更する)
それでは、メンテナンスモードに切り替えます。メンテナンスモードなので、エラーが表示されなくなります。
heroku maintenance:on
herokuにdeployします。
git push heroku master
heroku側もmigrateします。
heroku run rake db:migrate
メンテナンスモードを解除します。
heroku maintenance:off
herokuを開いて確認
heroku open
こんな画面が開いたらOK
herokuでアプリ公開まで出来ました!

2012年9月13日木曜日

[Mac][Ruby]MacでRuby環境構築

Clip to Evernote
Windowsでの構築に挫折した(笑)ので、Macでの環境構築にトライ\(^o^)/

参考:
macを買って、今すぐherokuでruby1.9.3 + rails3.2しよう!

item2を入れる

どうやらターミナルらしいです。おすすめされたので入れてみます。
以下URLにアクセス
http://code.google.com/p/iterm2/
ダウンロードタブを選択します。
 currentって書いてる方をダウンロード
zip展開したら終わり

command line tools for xcodeを入れる または Xcodeを入れる

XcodeからCommand Line Tools for Xcodeに切り替えたらHDD使用容量が7GB減ったを参考にがしがし入れます。
っていうか、Xcode入ってるんだけどねw
Xcodeのインストールはこちらを参考にどうぞ。

homebrewを入れる

参考サイトのコマンドを叩いたら、404エラーと言われたので、本家で確認。
本家サイト:homebrew
先ほど入れたitem2を立ちあげて、以下のコマンドを叩きましょう。
/usr/bin/ruby -e "$(/usr/bin/curl -fsSL https://raw.github.com/mxcl/homebrew/go)"
こんな感じ。
$ /usr/bin/ruby -e "$(/usr/bin/curl -fsSL https://raw.github.com/mxcl/homebrew/go)"
==> This script will install:
/usr/local/bin/brew
/usr/local/Library/...
/usr/local/share/man/man1/brew.1
==> The following directories will be made group writable:
/usr/local/.
==> The following directories will have their group set to admin:
/usr/local/.
Press enter to continue
==> /usr/bin/sudo /bin/chmod g+rwx /usr/local/.
==> /usr/bin/sudo /usr/bin/chgrp admin /usr/local/.
==> Downloading and Installing Homebrew...
remote: Counting objects: 82693, done.
remote: Compressing objects: 100% (39044/39044), done.
remote: Total 82693 (delta 57065), reused 65858 (delta 42824)
Receiving objects: 100% (82693/82693), 11.54 MiB | 1.59 MiB/s, done.
Resolving deltas: 100% (57065/57065), done.
From https://github.com/mxcl/homebrew
* [new branch] master -> origin/master
HEAD is now at 05eceb8 tmux: Correct japanese sonant mark display
Warning: Install the "Command Line Tools for Xcode": http://connect.apple.com
==> Installation successful!
You should run `brew doctor' *before* you install anything.
Now type: brew help
view raw homebrew.txt hosted with ❤ by GitHub
途中で、EnterKeyを押せーと行ってくるので、Enterを押しましょう。
homebrewが何者かというと、MacのUNIXツールをインストールするためのパッケージ管理システムだそうです。便利くんってことですね。へー。
詳しくはこちらを参照に。

zshを入れる

好みって言われても使ったことないので、とりま入れるww
bashの拡張ならまだ書ける気がする。。
item2で以下のコマンドを実行する。
brewhome install zsh
$ brew install zsh
==> Installing zsh dependency: gdbm
==> Downloading http://ftpmirror.gnu.org/gdbm/gdbm-1.10.tar.gz
######################################################################## 100.0%
==> ./configure --prefix=/usr/local/Cellar/gdbm/1.10 --mandir=/usr/local/Cellar/
==> make install
/usr/local/Cellar/gdbm/1.10: 10 files, 228K, built in 15 seconds
==> Installing zsh dependency: pcre
==> Downloading ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.31.
######################################################################## 100.0%
######################################################################## 100.0%==> ./configure --prefix=/usr/local/Cellar/pcre/8.31 --enable-utf8 --enable-unic
==> make test
==> make install
/usr/local/Cellar/pcre/8.31: 130 files, 3.2M, built in 50 seconds
==> Installing zsh
==> Downloading http://www.zsh.org/pub/zsh-5.0.0.tar.bz2
######################################################################## 100.0%
==> ./configure --prefix=/usr/local/Cellar/zsh/5.0.0 --enable-fndir=/usr/local/C
==> make install
Warning: skip_clean :all is deprecated
Skip clean was commonly used to prevent brew from stripping binaries.
brew no longer strips binaries, if skip_clean is required to prevent
brew from removing empty directories, you should specify exact paths
in the formula.
==> Caveats
To use this build of Zsh as your login shell, add it to /etc/shells.
If you have administrator privileges, you must fix an Apple miss
configuration in Mac OS X 10.7 Lion by renaming /etc/zshenv to
/etc/zprofile, or Zsh will have the wrong PATH when executed
non-interactively by scripts.
Alternatively, install Zsh with /etc disabled:
brew install --disable-etcdir zsh
==> Summary
/usr/local/Cellar/zsh/5.0.0: 956 files, 8.5M, built in 105 seconds
view raw install_zsh hosted with ❤ by GitHub
次にこのコマンドを実行
curl https://raw.github.com/gist/1970184/d7adf527117edd583de7b55141f8d7377a3b6cf6/.zshrc > ~/.zshrc
$ curl https://raw.github.com/gist/1970184/d7adf527117edd583de7b55141f8d7377a3b6cf6/.zshrc > ~/.zshrc
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 84 100 84 0 0 26 0 0:00:03 0:00:03 --:--:-- 35
view raw curl_zshrc hosted with ❤ by GitHub

中身を見る。
$ cat ~/.zshrc
export LANG=ja_JP.UTF-8
# homebrewを優先
export PATH=~/bin:/usr/local/bin:$PATH
view raw cat_zshrc hosted with ❤ by GitHub

設定を反映するため、以下のコマンド実行
source ~/.zshrc
シェルを変えるため、以下のコマンドも実行する

sudo sh -c "echo /usr/local/bin/zsh >> /etc/shells"
chsh -s /usr/local/bin/zsh

gitを入れる

gitをインストール。以下のコマンド実行
$ brew install git
==> Downloading http://git-core.googlecode.com/files/git-1.7.12.tar.gz
######################################################################## 100.0%
==> make prefix=/usr/local/Cellar/git/1.7.12 CC=cc CFLAGS= LDFLAGS= install
==> make CC=cc CFLAGS= LDFLAGS=
==> make clean
==> Downloading http://git-core.googlecode.com/files/git-manpages-1.7.12.tar.gz
######################################################################## 100.0%
==> Downloading http://git-core.googlecode.com/files/git-htmldocs-1.7.12.tar.gz
######################################################################## 100.0%
==> Caveats
The OS X keychain credential helper has been installed to:
/usr/local/bin/git-credential-osxkeychain
The 'contrib' directory has been installed to:
/usr/local/share/git-core/contrib
==> Caveats
Bash completion has been installed to:
/usr/local/etc/bash_completion.d
==> Summary
/usr/local/Cellar/git/1.7.12: 1235 files, 24M, built in 42 seconds
view raw install_git_log hosted with ❤ by GitHub

config設定をする。gitのアカウントを持っていたら、その情報と連携させた方がいいんだろうな。。
git config --global user.name "your name"
git config --global user.email "youraddress@example.com"

automakeを入れる

簡単な記述でmakeをしてくれるやつらしい。詳しくはこちら
brewhome install automake
実行結果はこんな感じ
$ brew install automake
==> Installing automake dependency: autoconf
==> Downloading http://ftpmirror.gnu.org/autoconf/autoconf-2.69.tar.gz
######################################################################## 100.0%
==> Patching
patching file bin/autoreconf.in
==> ./configure --prefix=/usr/local/Cellar/autoconf/2.69
==> make install
/usr/local/Cellar/autoconf/2.69: 67 files, 1.9M, built in 10 seconds
==> Installing automake
==> Downloading http://ftpmirror.gnu.org/automake/automake-1.12.3.tar.gz
######################################################################## 100.0%
==> ./configure --prefix=/usr/local/Cellar/automake/1.12.3
==> make install
/usr/local/Cellar/automake/1.12.3: 129 files, 2.0M, built in 8 seconds

rvmを入れる

複数のRubyを管理するツール。
bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
パスを通すようにzshrcのファイルを開く
vim ~/.zshrc
開いたら、以下の記述を追記する。

# rvm
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"

ruby1.9.3を入れる

以下のコマンドを実行する。
rvm install 1.9.3 --with-gcc=clang
するとこんな感じで怒られたorz
You requested building with 'clang' but it is not in your path.
どうやら、Xcodeの中からgccがなくなったのが原因っぽい。(参考:rvmによるrubyのアップデートでハマる)
Xcodeからインストールできるみたいなので、commandLineをインストールする。
参考:Mac OS X Lionにしたらgccがなくてnode.jsがコンパイルできなくなった件
インストールが終わったら、gccが入ったことを確認
gcc -v
Using built-in specs.
Target: i686-apple-darwin11
Configured with: /private/var/tmp/llvmgcc42/llvmgcc42-2336.11~28/src/configure --disable-checking --enable-werror --prefix=/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2 --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-prefix=llvm- --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin11 --enable-llvm=/private/var/tmp/llvmgcc42/llvmgcc42-2336.11~28/dst-llvmCore/Developer/usr/local --program-prefix=i686-apple-darwin11- --host=x86_64-apple-darwin11 --target=i686-apple-darwin11 --with-gxx-include-dir=/usr/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
こんなん出てればOK
ようやく戻って、もう一度Rubyをインストール

rvm install 1.9.3 --with-gcc=clang
Rubyのバージョンを指定します。
rvm use 1.9.3 
Rubyのバージョンを確認
ruby -v
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.4.0]

次はHerokuの環境構築しまーす。
今日はここまで。

2012年9月12日水曜日

[Ruby]1-2 変数とは-グローバル変数編-

Clip to Evernote
参考:
変数と定数

■グローバル変数とは

'$'で始まる変数。
プログラムのどこからでも参照が可能な変数のこと。
グローバル変数には、宣言が必要ない!
初期化されていない場合のグローバル変数の初期値は「nil」になる。

では振る舞いを確認

# グローバル変数
$hoge = 1
# メソッド定義
def foo
$hoge # グローバル変数なので、メソッド内からでもグローバル変数を参照できる
end
# メソッド呼び出し
foo #=> 1: メソッドfooは正常に終了。$hogeの値を参照できている
$hogehoge #=> nil: 初期化していないグローバル変数の初期値はnilになる
グローバル変数は、どこからでも参照できる一方で、どこからでも変更が可能なリスクもある。
そのため、必要がなければ、ローカル変数を極力使う方がいい。

目次に戻る

[Ruby]初心者のためのRubyまとめ

Clip to Evernote
Rubyの勉強がてらにまとめてみます。
初心者とは言いつつ、ある程度用語が出てくるかもしれないので、そのへんは許して下さいw
なるべくわかりやすいようにまとめるつもりです。
順次更新予定。

参考図書はこちら

  1. 記述の仕方
  2. ソースコードについて

[Ruby]1-1 変数とは-ローカル変数編-

Clip to Evernote
参考:

■変数

変数とは、他のプログラミング言語だと『物を入れる箱』のイメージのものだが、Rubyは『箱(入れ物)』ではなく、『名札』のイメージ。
変数自体にオブジェクトをコピーして格納するのではなく、どこかに存在するオブジェクトの名札にすぎない。なので、名札自体にデータ型とかは存在しない、ということ。
詳細は上記のリンクで解説されていますのでチラ見しましょうw

Rubyでは、変数はローカル変数、 インスタンス変数、 クラス変数、 グローバル変数、 定数に区別されます。名前によって、その区分が分かれるので、まずはローカル変数から見てきましょう。

■ローカル変数とは

ローカル変数は、メソッド内などの特定の範囲内で使用出来る変数で、次に出てくる識別子で始まります。

■ローカル変数の識別子

ローカル変数で使用出来るのは、以下の文字列。
『_』アンダースコア
英数字

※先頭に数字は使用できないので注意

こんな感じ
○ abc
× 1_to_100

予約語
rubyで予約されている文字列(ローカル変数として使用できない文字列)は以下
nil
true
false
not
or
and
BEGINE
END
begin
end
do
then
yield
rescue
ensure
class
module
def
defined?
alias
undef
super
self
return
while
until
for
in
break
next
redo
retry
case
when
if
unless
else
elsif
__LINE__
__FILE__
__ENCODING__ ※Ruby1.9系のみ

いっぱいあるけど、そのうち出てくるので、覚えなくともなんとなくわかってくるはず!(笑)

■スコープについて
最初に代入式が使用された位置から、その代入を含むブロックまたはメソッド定義の終わりまで。要は、メソッド内でしか動作しない変数です。
メソッドの外から、メソッド内で使用されているローカル変数は参照出来ない。
最初はよくわからないかもしれないので、動作を実際に見てみる方が早いですね。
以下で振る舞いの確認。
# これがローカル変数
hoge = 1
# メソッド定義始まり
def foo
hoge # メソッドから外側のローカル変数は参照出来ないので、メソッド呼び出しとみなされる
end
# メソッド定義終わり
foo #=> NameError:barメソッドのローカル変数hogeは宣言されていないのでエラーになる
hogehoge #=> NameError: ローカル変数hogehogeに代入がされていないため

今回はここまで!

目次に戻る

2012年9月5日水曜日

[Heroku][windows]Herokuを使い始めてみた

Clip to Evernote
全然触ったことなかったので、
使ってみようかと。
というか、サーバ構築があんまりうまくいってないので、先に開発環境だけでも作りたい。。
というわけで、Windowsでいろいろやってみようー

■Heroku にアカウント作成

まずはHerokuにアクセス。
http://www.heroku.com/

アカウントを作りましょう。
で、ログインをする。


■Toolbeltをインストール

Herokuにログインすると、クイックスタートのリンクがあるので、ポチる。
ステップ1はログインなので、無視して、
ステップ2のToolbeltのリンクをポチる。
言われるがままにToolbeltをインストールする。


1.toolbeltのリンクから以下の画面に遷移し、ダウンロードボタンをポチる。



2.ダウンロードが完了したら、『heroku-toolbelt.exe』をクリック。
するとインストール画面が起動する。『Next』ボタンをポチる
3.黙って『Next』

4.なんかFullでインストールすると言うので、やっぱり黙って『Next』ポチる


5.こんだけインストールするよ、と言ってるのを確認して、『Install』ボタンをポチる。
Gitも入るのね。ラッキー

6.ガリガリインストールするのを黙って放置して、終わったらFinish。



■Herokuを起動する

https://toolbelt.heroku.com/
上記の『Getting Start』にある通りにコマンドプロンプトからコマンドを打ち込む

○SSHのKeyを作る
メアドとか入力してYesして作られるのを待つ

○適当なディレクトリを作って、その下にプロジェクトを作成する

なんか出来たっぽいが、操作がわからないので、また次回