среда, 25 января 2012 г.

Simple ruby profiling

Just run ruby script with -rprofile option.
For example:
>ruby -rprofile script.rb
% cumulative self self total
time seconds seconds calls ms/call ms/call name
74.73 9.51 9.51 20 475.60 1271.40 IO#each_line
13.38 11.21 1.70 448096 0.00 0.00 String#=~
11.78 12.71 1.50 448129 0.00 0.00 Fixnum#+
0.12 12.73 0.02 8 1.88 1.88 IO#write
0.00 12.73 0.00 2 0.00 0.00 IO#set_encoding
0.00 12.73 0.00 22 0.00 0.00 String#==
0.00 12.73 0.00 22 0.00 0.00 BasicObject#!=
0.00 12.73 0.00 57 0.00 0.00 Array#[]
0.00 12.73 0.00 22 0.00 0.00 Array#first
0.00 12.73 0.00 22 0.00 0.00 String#split
0.00 12.73 0.00 2 0.00 6357.00 IO#open
0.00 12.73 0.00 19 0.00 0.00 IO#rewind
0.00 12.73 0.00 1 0.00 0.00 IO#close
0.00 12.73 0.00 4 0.00 0.00 Fixnum#to_s
0.00 12.73 0.00 2 0.00 0.00 File#initialize
0.00 12.73 0.00 2 0.00 7.50 Kernel.print
0.00 12.73 0.00 1 0.00 12729.00 #toplevel

понедельник, 23 января 2012 г.

Rails 3.1 on Heroku

Firstly I tried to do everything according to Hartl's railstutorial.org, but fail expected me on last step - migrating data to heroku's DB.

After realizing some heroku features I developed following steps for successful deployment:
1) Use ruby 1.9.2 at development side, because heroku use this version
2) Create heroku app with cedar stack. "Rails 3.1 runs best on Heroku’s Cedar stack"
3) Correctly handle 'pg' issue.

Seems easy, but for noob each step takes much time.

суббота, 21 января 2012 г.

pg gem installation

If you try bundle install after adding gem 'pg' in your Gemfile you'll get:
$ bundle install
Installing pg (0.12.2) with native extensions
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

/usr/local/bin/ruby extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
--with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
...
An error occured while installing pg (0.12.2), and Bundler cannot continue.
Make sure that `gem install pg -v '0.12.2'` succeeds before bundling.


I need pg gem for heroku deployment, but don't want to have postgres on development side.
There are 2 solutions:
1) It won't install gems from production group. Obviously, you should place pg gem in it.
$ bundle install --without production

2) It will install gems from production group only on corresponding environment.
Open config/application.rb
Do comment: Bundler.require(*Rails.groups(:assets => %w(development test)))
Uncomment: Bundler.require(:default, :assets, Rails.env)

Voila

пятница, 20 января 2012 г.

Heroku rake aborted

You should get this error on first db:migrate at heroku.

>heroku rake db:migrate
rake aborted!
Please install the postgresql adapter: `gem install activerecord-postgresql-adapter` (pg is not part of the bundle. Add
it to Gemfile.)

Tasks: TOP => db:migrate => db:load_config
(See full trace by running task with --trace)



Adding
group :production do
gem 'pg'
end

to Gemfile solves the problem.

Heroku installation

Got encoding error while installing heroku gem on Windows:

> gem install heroku
...
Installing RDoc documentation for rest-client-1.6.7...
ERROR: While generating documentation for rest-client-1.6.7
... MESSAGE: error generating C:/Ruby193/lib/ruby/gems/1.9.1/doc/rest-client-1.6.7/rdoc/README_rdoc.html: incompatible encoding regexp match (UTF-8 regexp with IBM866 string) (Encoding::CompatibilityError)
... RDOC args: --op C:/Ruby193/lib/ruby/gems/1.9.1/doc/rest-client-1.6.7/rdoc lib README.rdoc history.md --title rest-client-1.6.7 Documentation --quiet


Found a solution here:

> set RDOCOPT="--encoding=UTF-8"
> gem install heroku
Successfully installed heroku-2.18.1
1 gem installed
Installing ri documentation for heroku-2.18.1...
Installing RDoc documentation for heroku-2.18.1...



Update.
I've also taken that while installing rails on Mac:
$ sudo gem install rails
Installing RDoc documentation for i18n-0.6.0...
ERROR: While generating documentation for i18n-0.6.0
... MESSAGE: error generating I18n.html: incompatible encoding regexp match (UTF-8 regexp with ASCII-8BIT string) (Encoding::CompatibilityError)
... RDOC args: --op /usr/local/lib/ruby/gems/1.9.1/doc/i18n-0.6.0/rdoc lib --title i18n-0.6.0 Documentation --quiet


All installed successfully after:
$ export RDOCOPT="--encoding=UTF-8"