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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:29:59+00:00 2026-05-16T14:29:59+00:00

Inside ActionController class (rails/actionpack/lib/action_controller.lib) I found several weird code. I don’t really have a

  • 0

Inside ActionController class (rails/actionpack/lib/action_controller.lib) I found several weird code. I don’t really have a mentor to learn Ruby on Rails from, so this forum is my only hope:

Question #1: Could anyone help me explain these lines of codes?

begin
  require 'active_support'
rescue LoadError
  activesupport_path = "#{File.dirname(__FILE__)}/../../activesupport/lib"
  if File.directory?(activesupport_path)
    $:.unshift activesupport_path
    require 'active_support'
  end
end

Especially the line with $:.unshift activesupport_path

In my thought, it tries to require active_support class, and if that doesn’t work, it looks if activesupport_path is a directory, if it is, then . . . I totally lost it.

Question #2: What autoload method is for?

module ActionController
  # TODO: Review explicit to see if they will automatically be handled by
  # the initilizer if they are really needed.
  def self.load_all!
    [Base, CGIHandler, CgiRequest, Request, Response, Http::Headers, UrlRewriter, UrlWriter]
  end

  autoload :Base, 'action_controller/base'
  autoload :Benchmarking, 'action_controller/benchmarking'
  autoload :Caching, 'action_controller/caching'
  autoload :Cookies, 'action_controller/cookies'
  .
  .
  .

Question #3: If I later find a method I don’t understand what for, how is the best way to find out? As for that autoload method case, I tried to find it across my project (I have my Rails code frozen there) but couldn’t find any clue. I searched for “def autoload”. Am I doing things wrong? Is my IDE, TextMate just doesn’t cut it?

Thank you!

  • 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-16T14:29:59+00:00Added an answer on May 16, 2026 at 2:29 pm

    In order for a file to be required you have to ensure that the path to it is in the Ruby $LOAD_PATH variable. This is has a short-hand version $: for legacy reasons, inheriting this from Perl.

    When you call require, the interpreter looks for a .rb file in each of the paths given there until it finds a match. If it finds one, it is loaded. If not you get an exception.

    Often you will see lines like this in files:

    # script/something
    
    # This appends "script/../lib" to the $LOAD_PATH, but this expands to
    # something like "/home/user/project/lib" depending on the details of
    # your installation.
    $: << File.expand_path(File.join('..', 'lib'), File.dirname(__FILE__))
    

    You can use standard Array modifiers on $LOAD_PATH like unshift, push, and <<.

    The first block of code is attempting to load active_support and only if that fails does it go about modifying the $LOAD_PATH to include the likely location of this file based on the path to the file making the require call. They do this because typically all gems from the Rails bundle are installed in the same base directory.

    The reason for using unshift is to put that path at the highest priority, inserted at the front of the list. The << or push method adds to the end, lowest priority.

    When you require a file it is loaded in, parsed, and evaluated, an operation which can take a small but measurable amount of time and will consume more memory to hold any class or method definitions inside the file, as well as any data such as string constants that may be declared. Loading in every single element of a library like ActiveRecord using require will require a considerable amount of memory, and this will import every database driver available, not just the ones that are actually used.

    Ruby allows you to declare a class and a path to the file where it is defined, but with the advantage of not actually loading it in at that moment. This means that references to that class don’t cause script errors in other parts of your application that make use of them.

    You will often see declarations like this:

    class Foo
      # Declare the class Foo::Bar to be defined in foo/bar.rb
      autoload(:Bar, 'foo/bar')
    end
    

    When using autoload you need to keep in mind that the class name is always defined within the scope of the module or class declaring it. In this example Bar is within Foo, or Foo::Bar using Ruby naming conventions.

    When you make use of the Bar class, the foo/bar.rb file will be required. Think of it as creating a stub Bar class that transforms into the real class once it’s actually exercised.

    This is a great way of keeping a lot of options open, with many different modules ready to use, but without having to load everything into memory up front.

    As for the third question, searchable documentation like APIDock will help you try and find more information on methods. The distinction between Ruby and Rails is often blurred, so you may have to check through both to be sure. Rails adds a lot of methods to core Ruby classes, so don’t take the listing of methods available to be complete on either side. They work in conjunction.

    Sometimes it pays to search for def methodname when trying to find out about where methodname originates, although this covers only conventional declarations. That method may be an alias from a mechanism like method_alias or may have been dynamically created using define_method, you can never really be sure until you dig around. At least 90% of the methods in Rails are declared the conventional way, though, so most of the time a simple search will yield what you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Inside my .aspx I have some JSON code that looks like this: function someFunctionName()
Inside an initialization method, I have the following code - (id)init { self =
Inside a templated class, I found the expression, *this = NULL What does such
Inside a container object, I have created a class Factory , that is responsible
Inside a Webservice method , i have created a helper class whose responsibility is
Inside my user object I have the following code to generate a new 'session'
Inside my Controller i have function that runs after user clicks on item, which
Inside a form I have a button. What is the difference between when I
Inside my control, I have: ContextMenu = new ContextMenu(); ContextMenu.MenuItems.Add(new MenuItem(&Add Item, onAddSpeaker)); ContextMenu.MenuItems.Add(new
im new to ruby and rails. in RoR3 a controller inherits from the ActionController::Base

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.