2012/06/12

Ruby on Rails on Fedora 17

Fedora 17 上で Ruby on Rails を動かしたときのメモ.簡単には動かなかったので...

# yum install ruby  -> ruby, rubygems がインストールされる

$ gem install rails

$ rails new foo
$ cd foo

ま,ここまでは普通.

[error 1]
・症状
$ rails server
/home/hanayuki/.gem/ruby/1.9.1/gems/execjs-1.4.0/lib/execjs/runtimes.rb:51:in `autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)
        from /home/hanayuki/.gem/ruby/1.9.1/gems/execjs-1.4.0/lib/execjs.rb:5:in `'
...(以下略)

・対処法
Gemfileに下記を追加
gem 'therubyracer'
$ bundle update

参考:http://d.hatena.ne.jp/kano4/20110708/javascript_runtime_error

[error2]
・症状
$ rails server
/home/hanayuki/.gem/ruby/1.9.1/gems/railties-3.2.5/lib/rails/railtie/configuration.rb:85:in `method_missing': undefined method `active_record' for # (NoMethodError)

        from /home/hanayuki/herokucamp/rails_project/foo/config/application.rb:54:in `'
        from /home/hanayuki/herokucamp/rails_project/foo/config/application.rb:13:in `'
        from /home/hanayuki/herokucamp/rails_project/foo/config/application.rb:12:in `'
...(以下略)

・対処法
Gemfileに以下を追加
gem 'bigdecimal'
$ bundle update


これでやっと,rails server が動くようになります.

2012/01/21

python read subprocess stdout line by line


proc = subprocess.Popen(['python','fake_utility.py'],stdout=subprocess.PIPE)
for line in iter(proc.stdout.readline,''):
    print line

でまわす.

for line in proc.stdout:

ではうまくいかない

参考:
http://stackoverflow.com/questions/2804543/read-subprocess-stdout-line-by-line


追記:

hoge.sh | python foo.py

みたいな形で,パイプからの入力を標準入力で受ける場合も同じ.
CSV形式で読むならこんな感じ

csvreader = csv.reader(iter(sys.stdin.readline, ''))
for data_vals in csvreader:
    print data_vals

参考:
http://stackoverflow.com/questions/6556078/how-to-read-a-csv-file-from-a-stream-and-process-each-line-as-it-is-written