Skip to content

Instantly share code, notes, and snippets.

# An example of inject: deep_const_get.
# This versions of const_get works with nested constants
# (unlike regular const_get, which chokes on "::").
#
# This implementation does some output along the way to
# show you what's happening.
class Module
def deep_const_get(const)
segments = const.split("::")
class Bar
attr_reader :hash
def method_missing(m, *args)
if m.to_s =~ /((set|get)_(.+))/
func =
case $2
when 'set' then lambda {|value| @hash ||= {}; @hash[$3] = value}
when 'get' then lambda {@hash ||= {}; @hash[$3]}
end
self.class.send(:define_method, $1, &func)
# To use this, put it in per_action_middleware.rb,
# add PerActionMiddleware to the class_eval at the end
# of base.rb, and add this line to action_controller.rb:
#
# autoload :PerActionMiddleware, 'action_controller/per_action_middleware'
#
# I don't like the dummy module thing but at least this seems to do
# the trick on the initializations.
module PerActionMiddleware
module PerActionMiddleware
# DUMMY MODULE, just to get the file loaded.
end
module ActionController
class Base
class << self
alias _call call
def call(env)