monkeypatching

What work-arounds can be applied to thread-unsafe autoload in ruby?

As mentioned in this question, autoloading within a thread can cause problems. What work-arounds can be applied? ...

Custom Form Elements in Rails

So I'm new to Rails and I'm trying to figure out what the canonical way to add custom form elements is. Currently the way I'm doing it is super grotty. module ActionView module Helpers module FormOptionsHelper def some_new_field(object, method, options = {}, html_options = {}) #code code end end class...

To (monkey)patch or not to (monkey)patch, that is the question

I was talking to a colleague about one rather unexpected/undesired behavior of some package we use. Although there is an easy fix (or at least workaround) on our end without any apparent side effect, he strongly suggested extending the relevant code by hard patching it and posting the patch upstream, hopefully to be accepted at some poin...

Pickling array.array in 2.4 using cPickle

Hey All, I am working on a project built on python 2.4 (It is an embedded python project, so I don't have a choice on the version of python used). Throughout the application, we use array.array to store data. Support for pickling array.array objects was added to pickle (and cPickle) in 2.5. We have a viable workaround in 2.4 when usi...

How does one monkey patch a function in python?

I'm having trouble replacing a function from a different module with another function and it's driving me crazy. Let's say I have a module bar.py that looks like this: from a_package.baz import do_something_expensive def a_function(): print do_something_expensive() And I have another module that looks like this: from bar import...

Ruby: add custom properties to built-in classes

Question: Using Ruby it is simple to add custom methods to existing classes, but how do you add custom properties? Here is an example of what I am trying to do: myarray = Array.new(); myarray.concat([1,2,3]); myarray._meta_ = Hash.new(); # obviously, this wont work myarray._meta_['createdby'] = 'dreftymac'; myarray._meta_['lastupda...

Can I replace or modify a function on a jQuery UI widget? How? (Monkey Patching)

If I want to tweak some of the capability of a jQuery UI object, by replacing one of the functions, how would I go about doing that? Example: suppose I wanted to modify the way the jQuery autocomplete widget rendered the suggestions. There's a method on the autocomplete object that looks like this: _renderItem: function( ul, item) { ...

monkey patching time.time() in python

Hello guys, I've an application where, for testing, I need to replace the time.time() call with a specific timestamp, I've done that in the past using ruby (code available here: http://github.com/zemariamm/Back-to-Future/blob/master/back_to_future.rb ) However I do not know how to do this using Python. Any hints ? Cheers, Ze Maria ...

py2app prescripts

The py2app documentation mentions prescripts, being run by __boot__.py prior to the main python script. I couldn't find a way to easily specify any prescript on the setup.py file or build process. I did however manage to 'hack' __boot__.py manually and add another _run(prescript) command before my main _run(main_script) and it seemed to...

Monkeypatch a model in a rake task to use a method provided by a plugin?

During some recent refactoring we changed how our user avatars are stored not realizing that once deployed it would affect all the existing users. So now I'm trying to write a rake task to fix this by doing something like this. namespace :fix do desc "Create associated ImageAttachment using data in the Users photo fields" task :us...

Lua + SWIG Monkey Patching

I have used SWIG to bind a set of classes to lua. I know C++ itself doesn't support monkey patching, and I'm not trying to modify my C++ objects, merely their lua representations. The problem comes if I want to start monkey patching the lua tables and objects exported by SWIG, so that I can modify the API presented on the lua side. e.g....

C# monkey patching - is it possible?

Is it possible to write a C# assembly which when loaded will inject a method into a class from another assembly? If yes, will the injected method be available from languages using DLR, like IronPython? namespace IronPython.Runtime { public class Bytes : IList<byte>, ICodeFormattable, IExpressionSerializable { internal by...

jekyll - add stuff to pages automatically, on github pages

Hi! Is there a way to obtain the url of a page on Jekyll? By pages I mean non-post textile files, like about.html and download.html on the following hierarchy: root | +- _includes | +- _layouts | +- _posts | +- _config.yml | +- index.textile | +- about.textile | `- download.textile I'd like to do something like this: <...

MonkeyPatching UITextField in Flashbuilder 4

I am trying to monkeypatch a UITextField in Flashbuilder 4 for a flex 3 project, so I can change the selectionColor with colorMatrixFilter so all the textfields will get this style update. When I try, I get numerous build errors. ...

Do ruby gems ever conflict?

As a ruby newbie, I was wondering, will gems ever conflict with eachother? For example, if 2 gems overrode the << method on array, which would win, or is there something to stop this? Thanks ...

Ruby monkey patching pitfalls

Hi all, I'm looking for examples of why it's not a good idea to extend base classes in ruby. I need to show some people why it's a weapon to be wielded carefully. Any horror stories you can share? ...

Is monkeypatching stdlib methods a good practice in Python?

Over time I found the need to override several stdlib methods from Python in order to overcome limitation or to add some missing functionality. In all cases I added a wrapper function and replaced the original method from the module with my wrapper (the wrapper was calling the original method). Why I did this? Just to be sure that al...

How can I get pointer type behaviour in python

Hi, I want to write a test case which will test a list of functions. Here is an example of what I want to do: from mock import Mock def method1 (): pass def method2 (): pass ## The testcase will then contain: for func in method_list: func = Mock() # continue to setup the mock and do some testing What I want to achie...

Can F# modules be monkey-patched?

Quick question. I just read that if you wanted to add a function to e.g. the List module, you can define a new List module with that function: module List let foo = // ... Does this have the effect of adding foo to the main List module, or do you have to explicitly open the new List? The former seems like Ruby's "monkey patching"; I...

How can I replace the class by monkey patching?

How can I replace the ORM class - so it should not cause recursion !!! Problem: original class has the super call, when its got replaced - it causes self inheritance and causes maximum recursion depth exceed exception. i.e. class orm is calling super(orm, self).... and orm has been replaced by another class which inherits original orm.....