Skip to content

Instantly share code, notes, and snippets.

@mccraigmccraig
Created July 2, 2009 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mccraigmccraig/139453 to your computer and use it in GitHub Desktop.
Save mccraigmccraig/139453 to your computer and use it in GitHub Desktop.
ruby : dynamically bound values
# evil Ruby dynamic variables
class DynamicValue
attr_read :default_value
def initialize(val)
@default_value = val
end
def method_missing(name, *args)
if tv=Thread.current['DynamicValue']
tv.send( name, *args )
else
default_value.send( name, *args)
end
end
def with_dynamic_value(value)
tmp = Thread.current['DynamicValue']
begin
Thread.current['DynamicValue'] = value
yield
ensure
Thread.current['DynamicValue'] = tmp
end
end
end
ActionController::Routing.const_set("Routes", DynamicValue.new(ActionController::Routing::Routes))
new_routes = blahblah
ActionController::Routing::Routes.with_dynamic_value( new_routes ) do
load "other_routes_file.rb"
do_some_other_shit
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment