Skip to content

Instantly share code, notes, and snippets.

@olistik
Created November 2, 2015 17:24
Show Gist options
  • Save olistik/8cb55815197f9a43118d to your computer and use it in GitHub Desktop.
Save olistik/8cb55815197f9a43118d to your computer and use it in GitHub Desktop.
Prevents raised exceptions to be computationally expensive. Reference: https://gist.github.com/olistik/46a0b2879d6b5e7f616a
class ApplicationController < ActionController::Base
def index
foo_data.foo1
foo_data.foo2
request.xxx
end
private
def foo_data
@foo_data ||= foo_data
end
end
class Foo
def initialize(name)
@name = name
log(:initialize)
end
def inspect
log(:inspect)
end
def to_s
log(:to_s)
end
def log(context)
Rails.logger.debug "XXX Foo(#{@name}).#{context}"
end
end
class FooData
def inspect
"this is a cheap string"
end
def foo1
@foo1 ||= Foo.new(:foo1)
end
def foo2
@foo2 ||= Foo.new(:foo2)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment