macをmarvericksにしたら、rails server動かなくなった

色々と必要ないものとかもありそうだけど、やったこと全部書く

なにはともかく
xcodeのcommand line toolの入れなおし

1
xcode-select --install

んで、開発中のrailsディレクトリでrails serverしようとしたら動かない。
とりあえず再bundle installしなおしてみる。

nokogiriのインストールでlibxmlがどうこうってエラーでたから

1
2
3
~/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/nokogiri-1.6.0/ext/nokogiri/tmp/i686-apple-darwin11/ports/libxml2/2.8.0/libxml2-2.8.0
sudo make clean
sudo make

したらbundle installが成功。

でもrails serverしたら

1
Could not find libv8-3.11.8.17 in any of the sources

って出る。

まず

1
brew update

brew updateでこんなエラーが出た

1
Error: Failure while executing: git pull -q origin refs/heads/master:refs/remotes/origin/master

pullできないみたいなので、ここを見つつ以下の様なことをやる。
http://d.conma.me/entry/20120301/1330613125

1
2
3
4
5
6
cd `brew --prefix`
git reset --hard origin/master #=> untrackファイルがあるからダメとか言われる
git add .
git checkout -f
git reset --hard origin/master
brew update

brew update成功

んでここを見つつ以下を実行
How to fix libv8 error from Gemfile on Mavericks?
http://stackoverflow.com/questions/19568852/how-to-fix-libv8-error-from-gemfile-on-mavericks

1
2
3
gem uninstall libv8
brew install v8
gem install therubyracer

brew install v8が通らない・・・

1
2
3
4
5
6
7
8
==> Downloading https://github.com/v8/v8/archive/3.19.18.4.tar.gz
######################################################################## 100.0%
==> make dependencies
            --revision 1501
dyld: Library not loaded: /usr/local/lib/libsqlite3.0.8.6.dylib
  Referenced from: /usr/local/bin/svn
  Reason: image not found
make: *** [dependencies] Trace/BPT trap: 5

また、エラー・・・

1
2
brew rm svn
brew install svn

んで再度

1
brew install v8

通った…

1
bundle install

してから、

1
2
3
rails server
Could not find libv8-3.11.8.17 in any of the sources
Run `bundle install` to install missing gems.

あれ、ダメだ・・

1
2
3
4
5
6
7
8
bundle exec rails server
=> Booting WEBrick
=> Rails 3.2.13 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2013-10-28 13:47:46] INFO  WEBrick 1.3.1
[2013-10-28 13:47:46] INFO  ruby 2.0.0 (2013-06-27) [x86_64-darwin12.5.0]
[2013-10-28 13:47:46] INFO  WEBrick::HTTPServer#start: pid=16067 port=3000

ま、まぁいいか。


find_byのようなダイナミックファインダで例外

find _byのようなダイナミックファインダで例外
びっくりまーくつければいいみたいです。

1
Prefecture.find_by_name!("存在しない件" ) # => ActiveRecord::RecordNotFound

rails4ではこうかな

1
Prefecture.find_by!(name: "存在しない件")


2つ以上のthrough

モデルで以下のABCDというクラスがあって、それぞれに親子関係がある場合。

A→B→C→D

かなり適当に書くとこうやって取れる。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class User_1 < ActiveRecord::Base
 has_many user_2
 has_many user_3, through: user_2
 has_many user_4, through: user_3
end

class User_2 < ActiveRecord::Base
 belongs_to user_1
 has_many user_3
end

class User_3 < ActiveRecord::Base
 belongs_to user_2
 has_many user_4
end

class User_4 < ActiveRecord::Base
 belongs_to user_3
end

User_1.user_4.first.class # => User_4

ちょっと感動したけど、普通にみんな使ってるのだろうか。

しかし、言いたいことは伝わると思うのだが、
コード適当すぎかも(–;)



Signature expired

踏み台サーバー上でcapistranoで

1
`return_or_raise': Signature expired: 20130924T043045Z is now earlier than 20130924T043046Z (20130924T043546Z - 5 min.) (AWS::ELB::Errors::SignatureDoesNotMatch)

って出て、デプロイ出来ない。

結論。
サーバーの時刻が遅れてたからでした。
はまったので、みなさんも同じことにならないように。



meat_sauce

thorとかanemoneとかselenium-driverとか使ってみたくて、ついでにgemにしてみました。
cliから一気に複数画面のスクリーンショット取ったり、リンク一覧取得したり、指定タグの中身を取得したりできるツールです(※要 ruby)

https://github.com/YoshitsuguFujii/meat_sauce

なんか勝手にブラウザとか立ち上がりますが。びびらないでください。
あと環境によっては動かないかもしれないです。ごめんなさい。
僕の環境ではFirefoxとsafariなら動きました。

インストール

1
$ gem install meat_sauce

使えるコマンドが見れます。

1
$ meat_sause

ページの全てのリンクを取得します

1
$ meat_sauce show_links --url http://www.yahoo.co.jp

 -d 数字をオプション追加でさらにリンクを踏んだ先をどんどん表示します。どんどん。

指定したタグの中身を抜き出します

1
$ meat_sauce scraping --url http://www.yahoo.co.jp --tag h2

ソースを表示します

1
$ meat_sauce show_source --url http://www.yahoo.co.jp

画面のスクリーンショットを撮ります

1
$ meat_sauce capture --url http://www.yahoo.co.jp --file-path ~/yahoo.jpg

 -d 数字をオプション追加でリンクを踏んだ先も画像を撮ります。パシャパシャ。

個人的にはスクショとスクレイピングが便利だと思うのですが。どうでしょうか?
動けばですが・・・


bunlderのデフォルトオプション

1
bundle install --without production

とか毎回オプション設定がめんどくさい。
gemrcみたいなのないのかなーと思って探してみたら答えはここにあった。
http://bundler.io/bundle_config.html

bundle config –globalでglobalな設定を見れる。
bundle config –global キー 値で設定できる。

よって

1
bundle config --global without production

ってやったら、出来た。

確認してみる

1
2
3
4
5
bundle config
Settings are listed in order of priority. The top value will be used.

without
Set for the current user (~/.bundle/config): "production"

ファイルも見てみる

1
2
3
vim ~/.bundle/config
---
BUNDLE_WITHOUT: production

できましたー。


N回injectしたいとき(数値型からinject)

2012-04-24 - なんとなく日々徒然との日記からの焼きまわし感半端ない

1
10.times.inject(0) {|sum, (i, j)| sum + i } # => 0から9までを足して、結果は45

上記を1から10足して55にしたい場合(つまり1始まりにしたい)

とりあえずこうしてみる

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
10.times.with_index(1).inject(0) {|sum, i| p i }

# =>
[0, 1]
[1, 2]
[2, 3]
[3, 4]
[4, 5]
[5, 6]
[6, 7]
[7, 8]
[8, 9]
[9, 10]
[
    [0] 9,
    [1] 10
]

ふむ。
展開させます

1
10.times.with_index(1).inject(0) {|sum, (times_count, index_count)| sum + index_count } # => 55