monkeypatching

Is there any way to modify the value of a `private static final` field in java from outside the class? (Or, I need to monkey patch an external library).

I know this is normally rather stupid, but don't shoot me before reading the question. I promise I have a good reason for needing to do this :) It's possible to modify regular private fields in java using reflection, however Java throws a security exception when trying to do the same for final fields. I'd assume this is strictly enforc...

PowerShell, Extension Methods, and Monkey Patching

Is it possible to write extension method in PowerShell? or to bolt a new method on top of an existing type like [string] live at runtime? ...

In Django, how can you change the User class to work with a different db table?

We're running django alongside - and sharing a database with - an existing application. And we want to use an existing "user" table (not Django's own) to store user information. It looks like it's possible to change the name of the table that Django uses, in the Meta class of the User definition. But we'd prefer not to change the Dja...

How can you do a safe, backwards-compatible "reverse-monkeypatch" in Ruby?

If your coworker "opens" ("monkeypatches") a class in Ruby and redefines some important functionality that you need to use, how do you access that original pre-monkeypatched functionality without breaking a system that already relies/has dependencies on his monkeypatched definitions? ...

How can you influence the sequence in which Ruby code is loaded?

Let's say your coworker monkeypatches the Fixnum class and redefines the + method to subtract instead of add: class Fixnum def +(x) self - x end end >> 5 + 3 => 2 Your problem is you want to access the original functionality of the + method. So you drop this code in before his in the same source file. It aliases the + method ...

How does Smalltalk deal with monkeypatching ?

I'm a Ruby coder. For me, monkeypatching is to change, at runtime, classes or modules methods in an external project. What I'm interested in, is what mechanism you have in place that will protect you from some of the abuses of that nice feature. Follows, some scenarios that I have encountered, where monkeypatching has bitten me. While I...

Patching classes in Python

Suppose I have a Python class that I want to add an extra property to. Is there any difference between import path.MyClass MyClass.foo = bar and using something like : import path.MyClass setattr(MyClass, 'foo', bar) ? If not, why do people seem to do the second rather than the first? (Eg. here http://concisionandconcinnity.blog...

Redeclare .net classes in IronPython

Can anyone tell me if its possible to redeclare a C# class in IronPython? If I have a C# class, would I be able to monkey-patch it from IronPython? ...

Add functionality to Django FlatPages without changing the original Django App

Hello people, I would like to add a field to the Django FlatPage database model, but I do not really know how to extend this without editing the original application. What I want to do is to add the following field to the model: from django.db import models from django.contrib.flatpages.models import FlatPage as FlatPageOld class Fl...

Overriding base class methods in a Controller

I'm trying to fool a very complex black box into displaying some floats differently (it's the Gruff graphing library, so this is being rendered to an image). In the console, I can paste this: logger = RAILS_DEFAULT_LOGGER logger.debug "Here's a float #{455.67.to_s}" eval %{class Float def to_s_with_time h = (self / 60...

How do I get Rails to stop re-loading my gems?

I have a gem: # in /Library/Ruby/Gems/1.8/gems/my_gem-1.0.0/lib/my_gem.rb module MyGem def do_stuff .. end end And I loaded it in Rails: # in [rails_root]/config/environment.rb: config.gem 'my_gem', :version => '1.0.0' And used it: # in [rails_root]/app/controllers/application_controller.rb class ApplicationController < Ac...

Patch a Ruby Gem

What is the best way to apply a simple patch to a Ruby gem in a Rails app? Is it possible to keep the original gem code untouched? ...

Django and monkey patching issue

I have recently started experimenting with Django for some web applications in my spare time. While designing the data model for one, I came across the dilemma of using inheritance to define a user of the website or using a technique known as monkey patching with the User class already supplied by the framework. I tried to add a field b...

Is it possible to divert a module in python? (ResourceX diverted to ResourceXSimulated)

I want to simulate MyApp that imports a module (ResourceX) which requires a resource that is not available at the time and will not work. A solution for this is to make and import a mock module of ResourceX (named ResourceXSimulated) and divert it to MyApp as ResourceX. I want to do this in order to avoid breaking a lot of code and get...

Extending uniq method

This is Ruby 1.8 Question: We all know how to use Array#uniq : [1,2,3,1].uniq #=> [1,2,3] However I'm wondering if we can monkey patch it in a way to work with complex objects. The current behavior is like this: [{"three"=>"3"}, {"three"=>"4"}, {"three"=>"3"}].uniq #=> [{"three"=>"3"}, {"three"=>"4"}, {"three"=>"3"}] The requeste...

Can Ruby operators be aliased?

I'm interested in how one would go in getting this to work : me = "this is a string" class << me alias :old<< :<< def <<(text) old<<(text) puts "appended #{text}" end end I'd like that when something gets appended to the me variable, the object will use the redefined method. If I try to run this, I get syntax error, une...

Is it possible to redefine reverse in a Django project?

I have some custom logic that needs to be executed every single time a URL is reversed, even for third-party apps. My project is a multitenant web app, and the tenant is identified based on the URL. There isn't a single valid URL that doesn't include a tenant identifier. I already have a wrapper function around reverse, but now I need a...

Automatically require a class when running / starting ruby.

I am doing some monkey patching in some of Ruby classes and I would like that to be included AUTOMATICALLY whenever I run ruby. For example: I added the method trim to String. I want to be able to do this: ruby -e 'puts " aaaa ".trim' I don't want to do this: ruby -e 'require "monkey.rb"; puts " aaaa ".trim' Is there anyway to ...

What is the formal design pattern behind "monkeypatching"?

Core CS question here: of the Design Patterns listed in Gamma, etc, which (if any) cover monkeypatching? Additionally, for what class of problems is monkeypatching appropriate vs. subclassing? Patching bugs in core library classes is one, are there others? I hear lots of sturm und drang about monkeypatching on stackoverflow, most of you ...

"Extend" the "string" table - how to do it? Is it even a good idea?

I am developing a Lua library in which I needed to uppercase the first letter of a given string. Hence I created the following function: local capitalize = function(s) return string.gsub (s, "(%w)([%w]*)", function (first, rest) return string.upper(first) .. rest end, 1 ) end This initially was an "internal" ...