2012年9月26日水曜日

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

このエントリーをはてなブックマークに追加 Clip to Evernote
今回はルーティングの話。初級編ですー

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

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

routes.rb に以下を追加

この設定により、『/about』というパスでアクセスが来た時に、TopControllerのaboutアクションを呼び出すようになります。


TopControllerにaboutアクションを追加

aboutアクションは特に何もしません。

viewを作成

about.html.erbを作成します。ついでに、ちょっとインスタンス変数も使ってます。

サーバを起動

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


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

このエントリーをはてなブックマークに追加 Clip to Evernote
今回は文字コードの話が出たのでメモ

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

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

ソースを書いて実際にやってみましょう。
単純に標準出力に日本語を表示するだけのソースです。
実行するとこんな感じで表示されます。

$ 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
TopControllerクラスの中に、indexメソッドができてます。
これが、トップページを表示するアクションになります。

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

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

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

ビューを作成する

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

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

変数を表示する

ベタ書きだとつまらんので、変数を使ってみます。
/app/controller/top_controller.rb
@messageを定義して、文字を代入します。
テンプレートの方もmessageを読み込むように修正します。変数をテンプレートで読み込む場合は、<%=  %>で変数を囲むんですな。
ブラウザをリロードするとこんな感じ。
以上、アプリケーション作成入門でした!

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
こんなかんじでインストールされて完了。

アプリを作成する

railsアプリをまず作ります。
rails new heroku-sample
mysqlとか使いたかったら、-d mysqlオプションとか入れるといいらしいです。
が、ここではデフォルトでいきますw
次に、サンプルアプリの中のディレクトリに移動します。
cd heroku-sample
gitを初期化します。
gitの使い方は、Git入門で。
次に、最低限のgemを記述します。(といってもコンソールでコマンド叩くだけ)
Gemfileの中を見てみましょう。
vi Gemfile
開発では、sqlite3で、本番だとpostgreSQLだったりするんですな。ふむふむ。

herokuにアプリを作成する

以下コマンドを実行
herokuにdeployする
どうやらうまくいったみたい。
作成したアプリを見てみましょう。
以下、コマンドを打つと、safariが起動します。
heroku open
ようやく完了!

scaffoldでアプリを作る

まずは、デフォルト表示されるindex.htmlを消します。
scaffoldでブックマークアプリを作ります。
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)"
こんな感じ。
途中で、EnterKeyを押せーと行ってくるので、Enterを押しましょう。
homebrewが何者かというと、MacのUNIXツールをインストールするためのパッケージ管理システムだそうです。便利くんってことですね。へー。
詳しくはこちらを参照に。

zshを入れる

好みって言われても使ったことないので、とりま入れるww
bashの拡張ならまだ書ける気がする。。
item2で以下のコマンドを実行する。
brewhome install zsh
次にこのコマンドを実行
curl https://raw.github.com/gist/1970184/d7adf527117edd583de7b55141f8d7377a3b6cf6/.zshrc > ~/.zshrc

中身を見る。

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

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

gitを入れる

gitをインストール。以下のコマンド実行

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

automakeを入れる

簡単な記述でmakeをしてくれるやつらしい。詳しくはこちら
brewhome install automake
実行結果はこんな感じ

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」になる。

では振る舞いを確認

グローバル変数は、どこからでも参照できる一方で、どこからでも変更が可能なリスクもある。
そのため、必要がなければ、ローカル変数を極力使う方がいい。

目次に戻る

[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系のみ

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

■スコープについて
最初に代入式が使用された位置から、その代入を含むブロックまたはメソッド定義の終わりまで。要は、メソッド内でしか動作しない変数です。
メソッドの外から、メソッド内で使用されているローカル変数は参照出来ない。
最初はよくわからないかもしれないので、動作を実際に見てみる方が早いですね。
以下で振る舞いの確認。

今回はここまで!

目次に戻る

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して作られるのを待つ

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

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