Home

ruby slice_before method

Today I got a requirement to separate a array into slices when encounter several specific marks.

After search ruby api I found slice_before method.

[1,2,3,'a',4,5,6,'a',7,8,9,'a',1,3,5].slice_before {|i| i == 'a'}.map{|n| n}
=>
[[1, 2, 3], ["a", 4, 5, 6], ["a", 7, 8, 9], ["a", 1, 3, 5]]

Pretty cool ;)


Advanced capistrano usage

I was looking for a deploy lock, then found this article it introduced below usages:

  1. Graceful Passenger restarts
  2. Generating deployment stages on the fly in multi-stage environments
  3. Campfire notifications
  4. Deploy locks
  5. Generating servers list on the fly

(http://kpumuk.info/development/advanced-capistrano-usage/)[http://kpumuk.info/development/advanced-capistrano-usage/]


How rails can use other orm's generator

I’m just suddenly curious about this, so I looked up a little about Rails source code.

I found this code is how it get the other ORM class.

"#{options[:orm].to_s.camelize}::Generators::ActiveModel".constantize

Then I checked mongoid, found it has the class: Mongoid::Generators::ActiveModel in lib/rails/mongoid_generator.rb.


what is module extend self?

Today when I reading about rails source code found

module Dependencies
  extend self
end

Yes, I never saw this usage before, this actually is a efficient way to change all instance methods into class methods.

Reference: (http://ozmm.org/posts/singin_singletons.html)[http://ozmm.org/posts/singin_singletons.html]


Use rails generator to generate plugin or engine

Before I use enginx to generate a rails engine, but after Rails 3.1 there is a new generator rails plugin new to do the same thing now.

It will has a test/dummy directory which used to test engine controller, model and view.