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 6857923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:05:34+00:00 2026-05-27T02:05:34+00:00

I’m trying to understand how gems work, and how you can create a gem

  • 0

I’m trying to understand how gems work, and how you can create a gem to override or add additional functionality to a rails 3 application.

This is a error catching gem that posts errors via http to a server.

To install it you simply add the gem, and then it creates an initializer with your api key.

This part I get so far, but I need help understand the execution of the gem and how it hooks into rails.

The relevant file that I found are:

https://github.com/airbrake/airbrake/blob/master/lib/airbrake/rails.rb

Where it adds methods to the ActionController class:

  if defined?(ActionController::Base)
    ActionController::Base.send(:include, Airbrake::Rails::ActionControllerCatcher)
    ActionController::Base.send(:include, Airbrake::Rails::ErrorLookup)
    ActionController::Base.send(:include, Airbrake::Rails::ControllerMethods)
    ActionController::Base.send(:include, Airbrake::Rails::JavascriptNotifier)
  end

And at the bottom of the file it executes it:

Airbrake::Rails.initialize


1. You add the gem to your GEMFILE, and call bundle.
2. You run the generator which creates the initializer file:

So when my rails application starts up, it will go through the initializer.

Is this where the gem will get instantiated and then somewhere down the line run the Airbrake::Rails.initialize method when it finishes parsing the /lib/airbrake/rails.rb file?

I can see the initializer code in the railtie.rb file:

https://github.com/airbrake/airbrake/blob/master/lib/airbrake/railtie.rb

config.after_initialize do
  Airbrake.configure(true) do |config|
    config.logger           ||= ::Rails.logger
    config.environment_name ||= ::Rails.env
    config.project_root     ||= ::Rails.root
    config.framework        = "Rails: #{::Rails::VERSION::STRING}"
  end

  if defined?(::ActionController::Base)
    require 'airbrake/rails/javascript_notifier'
    require 'airbrake/rails/controller_methods'

    ::ActionController::Base.send(:include, Airbrake::Rails::ControllerMethods)
    ::ActionController::Base.send(:include, Airbrake::Rails::JavascriptNotifier)
  end
end

This seems to make sense now since it is running this code after the initializer runs and sets up the configuration, it sends the ControllerMethods, etc to the base class.

But this seems to duplicate the code I posted earlier when things were already being sent to the ApplicationController file in the call to Airbrake::Rails.initialize.

I am hoping someone can tell me how things execute and it what order as I just don’t see how this process works in a clear way.

  • 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-27T02:05:35+00:00Added an answer on May 27, 2026 at 2:05 am

    The duplicated code is here to support different versions of rails.

    Rails 2.X

    Rails 2.X looks for rails/init.rb to determine if a gem is a rails plugin or not. The airbrake one contains a single line:

    require 'airbrake/rails'
    

    Your summary of the next step was spot-on:

    Is this where the gem will get instantiated and then somewhere down
    the line run the Airbrake::Rails.initialize method when it finishes
    parsing the /lib/airbrake/rails.rb file?

    Rails 3.X

    Rails 3.X plugins often inherit from Rails::Railtie, to be able to define rake tasks, plug into the boot sequence or do a lot of other interesting things.

    The following line in lib/airbrake.rb checks the availability of the Railtie class (equivalent to checking if you’re running rails 3) and defines Airbrake::Railtie if it’s the case:

    require 'airbrake/railtie' if defined?(Rails::Railtie)
    

    When defining a Railtie, you don’t have to run it manually (the equivalent of Airbrake::Rails.initialize), subclassing Rails::Railtie is enough to create your plugin.

    Additional reading

    If you want to understand how rails 3 achieves its modularity, the following documentation will be quite helpful:

    • Rails::Railtie
    • Rails::Engine
    • Rails::Plugin
    • Rails::Application
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to create an if statement in PHP that prevents a single post
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

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.