[Ruby] 日本語表示対応
日本語を含むファイルを実行すると、
% ruby xxx.rb invalid multibyte char (US-ASCII)
そこで
xxx.rbファイルの最初に以下を記述。
# encoding: utf-8
日本語を含むファイルを実行すると、
% ruby xxx.rb invalid multibyte char (US-ASCII)
そこで
xxx.rbファイルの最初に以下を記述。
# encoding: utf-8
まずインストール
% brew install mysql
終わったら設定する。mysqlがインストールされたディレクトリに移動。
% cd /usr/local/Cellar/mysql/5.5.20
実行
% mysql_install_db Installing MySQL system tables... OK Filling help tables... OK To start mysqld at boot time you have to copy support-files/mysql.server to the right place for your system PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER ! To do so, start the server, then issue the following commands: ./bin/mysqladmin -u root password 'new-password' ./bin/mysqladmin -u root -h cl1018 password 'new-password' Alternatively you can run: ./bin/mysql_secure_installation which will also give you the option of removing the test databases and anonymous user created by default. This is strongly recommended for production servers. See the manual for more instructions. You can start the MySQL daemon with: cd . ; ./bin/mysqld_safe & You can test the MySQL daemon with mysql-test-run.pl cd ./mysql-test ; perl mysql-test-run.pl Please report any problems with the ./bin/mysqlbug script!
起動設定
インストールディレクトリに生成されるplistを「~/Library/LaunchAgents/」以下にコピー
% cp /usr/local/Cellar/mysql/5.5.20/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/
パスワード設定
mysqladminコマンドで設定
% mysqladmin -u root -h 127.0.0.1 password 'パスワード'
ログインしてみる
% mysql -u root -h 127.0.0.1 -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.5.20 Source distribution Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
my.conf作成
vi /etc/my.cnf [mysqld] max_allowed_packet=64M character-set-server = utf8 innodb_file_per_table [mysql] default-character-set = utf8
プロセス確認するときは、psコマンド
% ps ax | grep mysql
ソケットの確認は
% mysql_config --socket
起動コマンド
% mysql.server start
終了コマンド
% mysql.server stop
必要そうなもの
# yum -y install git # yum -y install make # yum -y install gcc # yum -y install gcc-c++ # yum -y install zlib-devel # yum -y install openssl-devel # yum -y install httpd-devel # yum -y install curl # yum -y install curl-devel # yum -y install readline-devel # yum -y install tk-devel # Database # yum install sqlite-devel # yum install mysql-devel # yum --enablerepo=epel -y install libyaml-devel
# bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer )
# source /etc/profile
[Ruby]
# rvm install 1.9.3 # rvm use 1.9.3 --default
[Rails]
# gem install rails
[mysql]
# gem install mysql -- --with-mysql-config=/usr/bin/mysql_config # gem install mysql2 -v '0.3.11'
プロジェクトをつくってみる
# rails new hoge -d mysql
create
create README.rdoc
create Rakefile
create config.ru
create .gitignore
create Gemfile
.......
create vendor/plugins/.gitkeep
run bundle install
Fetching gem metadata from https://rubygems.org/.........
Using rake (0.9.2.2)
.....
Using
Installing uglifier (1.2.4)
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
iMacが新しくなったので再度インストール。(Lion環境)
% brew install zsh
/etc/shellsにインストールしたzshを追記
% sudo sh -c "echo '/usr/local/bin/zsh' >> /etc/shells"
既にあれば行わない
% touch ~/.zshrc
パスを設定。
これを行わないとデフォルトの/etc/zshのまま。
% echo 'export PATH=/usr/local/bin:$PATH' >> ~/.zshrc
再読み込み
% source .zshrc
sinatraをHeorkuで動作させたい。

とりあえず動作優先、app.rbつくる。
require 'sinatra' get '/' do "Hello, Heroku" end
Gemfileをつくる
% vim Gemfile source :rubyforge gem 'sinatra', '1.3.2' gem 'thin', '1.3.1'
bundleコマンドでGemfileのライブラリをインストールする。
% bundle install
Herokuの起動ファイル作成する
% vim Procfile web: bundle exec ruby app.rb -p $PORT
この状態でローカルでの確認する。
Procfileを読み込むためにforemanというgemを入れ、アプリをローカルで起動
% gem install foreman % foreman start
http://localhost:5000/で表示されればOK
Herokuにアップする準備
% git init % git add . % git commit -m "first commit"
herokuにプロジェクトを作成する。
(–stackオプションでcedarスタックを選択して、アプリ名にmyapp-testyとする)
% heroku create --stack cedar myapp-testy Creating myapp-testy... done, stack is cedar http://myapp-testy.herokuapp.com/ | git@heroku.com:myapp-testy.git Git remote heroku added
herokuのレポジトリにコミットする
% git push heroku master
Counting objects: 6, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (6/6), 696 bytes, done.
Total 6 (delta 0), reused 0 (delta 0)
-----> Heroku receiving push
-----> Ruby app detected
-----> Installing dependencies using Bundler version 1.1.2
Running: bundle install --without development:test --path vendor/bundle --binstubs bin/ --deployment
Fetching gem metadata from http://rubygems.org/.....
Installing daemons (1.1.8)
Installing eventmachine (0.12.10) with native extensions
Installing rack (1.4.1)
Installing rack-protection (1.2.0)
Installing tilt (1.3.3)
Installing sinatra (1.3.2)
Installing thin (1.3.1) with native extensions
Using bundler (1.1.2)
Your bundle is complete! It was installed into ./vendor/bundle
Cleaning up the bundler cache.
-----> Discovering process types
Procfile declares types -> web
Default types for Ruby -> console, rake
-----> Compiled slug size is 3.1MB
-----> Launching... done, v3
http://myapp-testy.herokuapp.com deployed to Heroku
To git@heroku.com:myapp-testy.git
* [new branch] master -> master
http://myapp-testy.herokuapp.com/
こんなもん?
Heroku Toolbeltをインストール。
インストールした後は、アプリを公開出来るように設定。
自Macで
% gem install heroku
アプリのディレクトリでgitを初期化
% cd /Users/xxx/app % git init Initialized empty Git repository in /Users/xxx/app/.git/
アプリのディレクトリでherokuコマンドで以下を実行
% heroku create Creating radiant-summer-6462...... done, stack is bamboo-mri-1.9.2 http://radiant-summer-6462.heroku.com/ | git@heroku.com:radiant-summer-6462.git Git remote heroku added
公開鍵を変更したいとき
% heroku keys:add ~/.ssh/id_rsa.pub