monkeypatching

monkey patching in php

久未见 提交于 2019-12-01 04:43:15
问题 I'm trying to figure out how monkey patching works and how I can make it work on my own objects/methods. I've been looking at this lib, it does exactly what I want to do myself: https://github.com/antecedent/patchwork With it you can redefine a method from an object. It uses the 'monkey patch' technique for that. But I couldn't really figure out what exactly is going on by looking at the source. So suppose I have the following object: //file: MyClass.php namespace MyClass; class MyClass {

MonkeyPatching: PrimeFaces widgets extend/override

放肆的年华 提交于 2019-12-01 00:49:29
I'm currently using (it's working fine) PrimeFaces.widget.OverlayPanel.prototype._old_init = PrimeFaces.widget.OverlayPanel.prototype.init; PrimeFaces.widget.OverlayPanel.prototype.init = function(cfg) { this._old_init(cfg); this.align(); } but I'd like to use something more readable and 'jQuery-ish' like this completely invented unrealistic code: PrimeFaces.widget.OverlayPanel.patch( { init: function(cfg) { super.init(cfg); this.align(); }, show: function() { console.log('blah blah blah'); super.show(); } }); I tried PrimeFaces.widget.Xxx.extend({...}) but in this case I have no access to

Golang monkey patching

北城以北 提交于 2019-11-30 19:00:24
I understand that if go code is structured such that it's programmed to interfaces, it's trivial to mock; however, I'm working with a code base that I cannot change (that is not mine) and this is not the case. This code base is heavily interconnected and nothing is programmed to an interface, only structs, so no dependency injection. The structs, themselves, only contain other structs, so I can't mock out that way either. I don't believe that I can do anything about methods, and the few functions that exist are not variables, so there's no way that I know of to swap them out. Inheritance isn't

Is it acceptable practice to patch Ruby's base classes, such as Fixnum?

社会主义新天地 提交于 2019-11-30 13:43:20
问题 I am still very new to Ruby (reading through the Pickaxe and spending most of my time in irb ), and now that I know it's possible to patch classes in Ruby, I'm wondering when it's acceptable to do so, specifically whether it's acceptable to patch Ruby's base classes. For example: I answered another Ruby question here where the poster wanted to know how to subtract hours from a DateTime . Since the DateTime class doesn't seem to provide this functionality, I posted an answer that patches the

monkey patching vs class_eval?

可紊 提交于 2019-11-30 11:35:17
class String def hello "world" end end String.class_eval { def world "hello" end } "a".world => "hello" "b".hello => "world" They seems to do the same thing -- adding a method to a existing class. So what's the difference? With class_eval you can do more dynamic things: >> met = "hello" #=> "hello" >> String.class_eval "def #{met} ; 'hello' ; end" #=> nil >> "foo".hello #=> "hello" class_eval do conceptually class reopening (or monkey patching). There are mostly syntactic differences. If you pass string to class_eval (as in Michael's example) you have mostly the same syntax inside the string

Monkey Patching in python: When we need it?

感情迁移 提交于 2019-11-30 10:21:50
In Python, the term monkey patch only refers to dynamic modifications of a class or module at runtime, As a beginner its really difficult to me understand this term in context of python. Can anybody explain to me with a real world example How exactly we do ? dynamic modifications of a class dynamic modifications of module at runtime I am insisting a real world example (as simple as possible) to understand in which scenarios we have to do such task ? Monkey-patching is a way to make some global under-the-hood change in a way that existing code will continue to run, but with modified behavior. A

Monkey patching a Django form class?

旧巷老猫 提交于 2019-11-30 09:48:25
Given a form class (somewhere deep in your giant Django app).. class ContactForm(forms.Form): name = ... surname = ... And considering you want to add another field to this form without extending or modifying the form class itself , why does not the following approach work? ContactForm.another_field = forms.CharField(...) (My first guess is that the metaclass hackery that Django uses applies only the first time the form class is constructed. If so, would there be a way to redeclare the class to overcome this?) Some pertinent definitions occur in django/forms/forms.py . They are: class BaseForm

How can I monkey-patch an instance method in Perl?

女生的网名这么多〃 提交于 2019-11-30 05:56:16
问题 I'm trying to monkey-patch (duck-punch :-) a LWP::UserAgent instance, like so: sub _user_agent_get_basic_credentials_patch { return ($username, $password); } my $agent = LWP::UserAgent->new(); $agent->get_basic_credentials = _user_agent_get_basic_credentials_patch; This isn't the right syntax -- it yields: Can't modify non-lvalue subroutine call at [module] line [lineno]. As I recall (from Programming Perl ), dispatch lookup is performed dynamically based on the blessed package ( ref($agent)

Golang monkey patching

点点圈 提交于 2019-11-30 03:37:53
问题 I understand that if go code is structured such that it's programmed to interfaces, it's trivial to mock; however, I'm working with a code base that I cannot change (that is not mine) and this is not the case. This code base is heavily interconnected and nothing is programmed to an interface, only structs, so no dependency injection. The structs, themselves, only contain other structs, so I can't mock out that way either. I don't believe that I can do anything about methods, and the few

Monkey Patching in python: When we need it?

倖福魔咒の 提交于 2019-11-29 16:10:11
问题 In Python, the term monkey patch only refers to dynamic modifications of a class or module at runtime, As a beginner its really difficult to me understand this term in context of python. Can anybody explain to me with a real world example How exactly we do ? dynamic modifications of a class dynamic modifications of module at runtime I am insisting a real world example (as simple as possible) to understand in which scenarios we have to do such task ? 回答1: Monkey-patching is a way to make some