Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4002020
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:58:31+00:00 2026-05-20T07:58:31+00:00

I’m working with Spree, which uses Rails engines extensively. A common pattern I find

  • 0

I’m working with Spree, which uses Rails engines extensively. A common pattern I find is this. However I have trouble understanding this code.

class Engine < Rails::Engine 
  def self.activate
    ...
  end     

  config.to_prepare &method(:activate).to_proc
end
  1. What does .to_prepare do
  2. What does the &method operator do?
  3. What is the overall effect of the code?

Many Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:58:32+00:00Added an answer on May 20, 2026 at 7:58 am

    &method(:activate).to_proc

    This is best consumed one item at a time. (It should be noted that this portion of the code is 100% non-Rails-specific Ruby.)

    method is a Ruby method (it’s all very meta). It is documented thusly:

    Looks up the named method as a
    receiver in obj, returning a Method
    object (or raising NameError). The
    Method object acts as a closure in
    obj’s object instance, so instance
    variables and the value of self remain
    available.

    Check out the following IRB session:

    ruby-1.9.2-p136 :001 > def my_method
    ruby-1.9.2-p136 :002?>   puts "in my_method"
    ruby-1.9.2-p136 :003?>   10
    ruby-1.9.2-p136 :004?>   end
     => nil 
    ruby-1.9.2-p136 :005 > method(:my_method)
     => #<Method: Object#my_method>
    

    So the method call is looking up (in your example) the activate method and creating a Method object for it. Method contains a method called to_proc, which “returns a Proc object corresponding to this method“. Continuing our IRB session:

    ruby-1.9.2-p136 :006 > method(:my_method).to_proc
     => #<Proc:0x000001010a3e38 (lambda)>
    

    Finally, we use the ampersand operator, which, when preceding a Proc object during a method call, will be replaced by the block the Proc contains. One more time in IRB:

    ruby-1.9.2-p136 :007 > def executor(&block)
    ruby-1.9.2-p136 :008?>   block.call
    ruby-1.9.2-p136 :009?>   end
     => nil 
    ruby-1.9.2-p136 :010 > executor( &method(:my_method).to_proc )
    in my_method
     => 10
    

    So, in pseudocode, what the line you listed is saying is:

    config.to_prepare(a block containing the functionality of the method activate)
    

    config.to_prepare

    [Edited based on schof’s comment]

    config.to_prepare takes a block that should be run to set up your Railtie/Engine. It is run once in production mode and on every request in development, and is the only code guaranteed to be called on every single request in development mode. This is important if you are modifying classes (class_eval, etc.) as part of your engine initialization; otherwise, you’re going to see different behavior in development vs. production.

    But Why?

    The reason this is a common idiom is so that you don’t have to define your preparation code inside the to_prepare block itself; you can define method(s) in your class and then convert them to a block (which remembers its scope) using the magic described above. The code is functionally equivalent to:

    class Engine < Rails::Engine 
      config.to_prepare do
        (the contents of self.activate)
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I have just tried to save a simple *.rtf file with some websites and
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.