技術メモ

主にRuby関連

Shinjuku.rb #2に参加してきました #shinjukurb

Ruby関連の勉強会初参加です。
沢山の情報を得たので忘れないようにメモとちょい足し。

Rails3レシピブック読書会

[001] Railsをインストールする

◆ Windows環境へのインストール

 WindowsでRailsを動かそうとしている人には絶望、WindowsでRailsアプリを
 運用したことがある人(私)には同意させる内容が書いてありますw
 < 以下は私のちょい足し >
 それでもWindowsで運用する必要がある人は本に書いてある通りRailsInstallerを使う
 のが無難だと思います。
 現時点では、ver.2.0.1で以下の構成になってます。
  - Ruby 1.9.2-p290
  - Rails 3.1.1
  - Bundler 1.0.18
  - Git 1.7.6
  - Sqlite 3.7.3
  - TinyTDS 0.4.5
  - SQL Server support 3.3.3
  - DevKit

 でもDevKitの導入方法が分かる人はRubyInstaller + DevKitRubyRails
 最新版等、好きな環境を整えられるのでコチラをオススメします。
 <手前ミソですがWindows環境でRubyRailsを使う時に便利だったものを
  Qiitaに載せてあるので興味があればどうぞ>

 但し!本の最後にもありますがDevKit導入だけでは解決できない場合があります。
 代表例としてはWebサーバです。結論から言えばThinやMogrelは上記環境で動作
 しますが、ある特定なバージョンやプラットフォームを指定しないと動作しない
 のでgemをインストールする際にはinstallオプションが必要になります。
 それでも動かない場合はrake-compilerを使ったりします。

 そういえば、仕事でWindows環境でのRails開発経験がある人?って聞かれ、
 自分の後ろは確認してないけど自分以外に手を挙げた人はいたのかな?

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

rails new コマンド

 rails new コマンドでアプリケーション名をスネークケースにするとどうなるか?

$ bundle exec rails new _a_b_c_

アプリケーションディレクトリ名: _a_b_c_
アプリケーションモジュール名: ABC

 rails new コマンドのオプションは沢山あるよね?という話に発展

$ bundle exec rails new --help

Usage:
  rails new APP_PATH [options]

Options:
  -r, [--ruby=PATH]              # Path to the Ruby binary of your choice
                                 # Default: /Users/masassiez/.rbenv/versions/1.9.3-p125/bin/ruby
  -b, [--builder=BUILDER]        # Path to a application builder (can be a filesystem path or URL)
  -m, [--template=TEMPLATE]      # Path to an application template (can be a filesystem path or URL)
      [--skip-gemfile]           # Don't create a Gemfile
      [--skip-bundle]            # Don't run bundle install
  -G, [--skip-git]               # Skip Git ignores and keeps
  -O, [--skip-active-record]     # Skip Active Record files
  -S, [--skip-sprockets]         # Skip Sprockets files
  -d, [--database=DATABASE]      # Preconfigure for selected database (options: mysql/oracle/postgresql/sqlite3/frontbase/ibm_db/sqlserver/jdbcmysql/jdbcsqlite3/jdbcpostgresql/jdbc)
                                 # Default: sqlite3
  -j, [--javascript=JAVASCRIPT]  # Preconfigure for selected JavaScript library
                                 # Default: jquery
  -J, [--skip-javascript]        # Skip JavaScript files
      [--dev]                    # Setup the application with Gemfile pointing to your Rails checkout
      [--edge]                   # Setup the application with Gemfile pointing to Rails repository
  -T, [--skip-test-unit]         # Skip Test::Unit files
      [--old-style-hash]         # Force using old style hash (:foo => 'bar') on Ruby >= 1.9

Runtime options:
  -f, [--force]    # Overwrite files that already exist
  -p, [--pretend]  # Run but do not make any changes
  -q, [--quiet]    # Supress status output
  -s, [--skip]     # Skip files that already exist

Rails options:
  -h, [--help]     # Show this help message and quit
  -v, [--version]  # Show Rails version number and quit

Description:
    The 'rails new' command creates a new Rails application with a default
    directory structure and configuration at the path you specify.

    You can specify extra command-line arguments to be used every time
    'rails new' runs in the .railsrc configuration file in your home directory.

    Note that the arguments specified in the .railsrc file don't affect the
    defaults values shown above in this help message.

Example:
    rails new ~/Code/Ruby/weblog

    This generates a skeletal Rails installation in ~/Code/Ruby/weblog.
    See the README in the newly created application to get going.

 - -Tオプション は良く使うね
 - --skip-bundleオプション で自動的な bundle install を取り消せるよ
 - -mオプション も便利
 そういえば -mオプション の話の中で、RailsWizardに意外に反応があってビックリ。
 結構メジャーなのかと思って言ってみたんだけど、マイナーだったらしいw

◆ rails install コマンド

 - --pathオプション でgemをアプリ毎に指定のディレクトリに置けるよ
 個人的には --binstubsオプションも覚えておいた方が良いと思う。
 rake等の実行で bundle exec を付けずに rake を実行できます。

[003] Railsアプリケーションのファイル構造を理解する

◆ Gemfile

● sourceについて

Defaultは以下の3組

source :rubygems
source "http://rubygems.org"
source :rubyforge
source "http://gems.rubyforge.org"
source :gemcutter
source "http://gemcutter.org"

それ以外のgemサーバも可能か? => 多分、 gem server で可能

$ gem server --help

Usage: gem server [options]

  Options:
    -p, --port=PORT                  port to listen on
    -d, --dir=GEMDIR                 directories from which to serve gems
                                     multiple directories may be provided
        --[no-]daemon                run as a daemon
    -b, --bind=HOST,HOST             addresses to bind
    -l, --launch[=COMMAND]           launches a browser window
                                     COMMAND defaults to 'start' on Windows
                                     and 'open' on all other platforms


  Common Options:
    -h, --help                       Get help on this command
    -V, --[no-]verbose               Set the verbose level of output
    -q, --quiet                      Silence commands
        --config-file FILE           Use this config file instead of default
        --backtrace                  Show stack backtrace on errors
        --debug                      Turn on Ruby debugging


  Summary:
    Documentation and gem repository HTTP server

  Description:
    The server command starts up a web server that hosts the RDoc for your
    installed gems and can operate as a server for installation of gems on other
    machines.
    
    The cache files for installed gems must exist to use the server as a source
    for gem installation.
    
    To install gems from a running server, use `gem install GEMNAME --source
    http://gem_server_host:8808`
    
    You can set up a shortcut to gem server documentation using the URL:
    
      http://localhost:8808/rdoc?q=%s - Firefox
      http://localhost:8808/rdoc?q=* - LaunchBar

  Defaults:
    --port 8808 --dir /Users/masassiez/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1 --no-daemon
◆ config.ru

 rails s コマンドで読み込まれる

◆ 各assetsディレクトリ

3つあるね

  • app/assets : アプリ固有
  • lib/assets : 汎用(管理は自分を含む) 例:社内汎用など
  • vendor/assets : 汎用(管理は他人) 例:JQuery Plug-inなど

各assetsディレクトリ以下は全てアクセス可能。じゃあ、名前が同じ時の優先順位は?
Rails.application.config.assets.paths順に優先度が高くなる

mkdir lib/assets/javascripts
y Rails.application.config.assets.paths

---
- /Users/masassiez/Dropbox/project/rails/_a_b_c_/app/assets/images
- /Users/masassiez/Dropbox/project/rails/_a_b_c_/app/assets/javascripts
- /Users/masassiez/Dropbox/project/rails/_a_b_c_/app/assets/stylesheets
- /Users/masassiez/Dropbox/project/rails/_a_b_c_/lib/assets/javascripts
- /Users/masassiez/Dropbox/project/rails/_a_b_c_/vendor/assets/javascripts
- /Users/masassiez/Dropbox/project/rails/_a_b_c_/vendor/assets/stylesheets
- /Users/masassiez/Dropbox/project/rails/_a_b_c_/vendor/bundle/ruby/1.9.1/gems/jquery-rails-2.0.0/vendor/assets/javascripts
- /Users/masassiez/Dropbox/project/rails/_a_b_c_/vendor/bundle/ruby/1.9.1/gems/coffee-rails-3.2.2/lib/assets/javascripts

config/application.rbに config.assets.paths << Rails.root.join("app", "assets", "flash") を追加するとどうなる?

# config/application.rbに config.assets.paths << Rails.root.join("app", "assets", "flash") を追加
y Rails.application.config.assets.paths

---
- /Users/masassiez/Dropbox/project/rails/_a_b_c_/app/assets/images
- /Users/masassiez/Dropbox/project/rails/_a_b_c_/app/assets/javascripts
- /Users/masassiez/Dropbox/project/rails/_a_b_c_/app/assets/stylesheets
- /Users/masassiez/Dropbox/project/rails/_a_b_c_/lib/assets/javascripts
- /Users/masassiez/Dropbox/project/rails/_a_b_c_/vendor/assets/javascripts
- /Users/masassiez/Dropbox/project/rails/_a_b_c_/vendor/assets/stylesheets
- /Users/masassiez/Dropbox/project/rails/_a_b_c_/vendor/bundle/ruby/1.9.1/gems/jquery-rails-2.0.0/vendor/assets/javascripts
- /Users/masassiez/Dropbox/project/rails/_a_b_c_/vendor/bundle/ruby/1.9.1/gems/coffee-rails-3.2.2/lib/assets/javascripts
- !ruby/object:Pathname
  path: !binary |-
    L1VzZXJzL21hc2Fzc2llei9Ecm9wYm94L3Byb2plY3QvcmFpbHMvX2FfYl9j
    Xy9hcHAvYXNzZXRzL2ZsYXNo
◆ tmp/pidsディレクトリ

なんぞ?
Webサーバ起動すると起動中 server.pid が現れます。

etc.

bundle

bundle1.1

早いよ

git

ロカールリポジトリからリモートリポジトリへのignore

.git/info/excludeファイルにignoreパターンを書く事でローカルでは管理対象だがリモートでは管理対象外にできる。(あってるかな?)

mxcl/homebrew - GitHub

homebrewいいよw

fomula分かり易くて書き易いよ。

puma

pumaもあるよ