ActiveSupport behind the scene - Screencast Outline

In this episode, we're going behind-the-scenes rails ActiveSupport module. And see how a few of the common methods are implemented. In doing so, we'll see examples of ruby metaprogramming concepts in practice.

[Format: flip between rails console session and the rails source code to demonstrate each method]

Open Class

  1. squish and squish!

source code

  1. blank? and present?
    It opens so many classes to get the desired behavior. Object, NilClass, FalseClass, TrueClass, Array, Hash, String, Numric, Time

But the end of the day is using empty? (the string part is bit more involved)


Method Missing
3. inquiry and StringInquirer - demonstrate method_missing and respond_to_missing

class StringInquirer < String
      def respond_to_missing?(method_name, include_private = false)
        method_name.end_with?("?") || super

      def method_missing(method_name, *arguments)
        if method_name.end_with?("?")
          self == method_name[0..-2]

For StringInquirer:
The sourcecode

Using it in Rails.env

It uses a subclass of StringInquirer called EnvironmentInquirer that does not rely on method_missing. uses instance_variable_set, class_eval

An aside - bunch of methods in ActiveSupport are simply an alias for existing ruby methods. starts_with? and ends_with? in both String and Symbol for example [not sure if worth mentioning this. alias is a keyword in Ruby and doesn't really have anything to do with metaprogramming]

[I found this class interesting. Not sure if it will fit in this screencast. Likely a separate episode, not mix too many things]
One example is constantize which is simply doing Object.const_get since all Class and Module names are constants in Ruby.