在Flex框架中,可以在加载网站时使用自定义预加载器.
在Adobe文档中,它指定如果在下载700毫秒后下载的应用程序少于一半,则会显示进度条[预加载器]."
但是我总是希望预加载器能够立即显示,因为我知道95%的用户是第一次访问者,而且网站超过500kb.我不希望人们必须等待.7秒才能显示预加载器动画.
我认为理论上可以"修补"框架以消除这个.7秒限制.我没时间弄清楚如何,我以前从未做过.
有人帮忙吗?
我想扩展 Object 以添加一些方法。
所以我可以做@object.table_name而不是@object.class.name.tableize
以及类似的事情。
我正在使用 Ruby 1.8.7 和 Rails 2.3.8,所以也许这种东西会作为模块放入 lib 文件夹中?我不知道。
具体来说,我想将delayed_job指向另一个表:
set_table_name"my_table"
我刚从售卖宝石切换到Bundler.我曾经只是改变了售卖宝石中的代码,这可能是愚蠢的.无论如何,我可能需要为此做一个猴子路径,我只是不确定如何.
我正在尝试使用元类来实现以下功能:
class foo( object ):
def __init__( self ):
self.val = 'foo'
def bar( self ):
print 'hello world'
print self.val
f = foo()
f.bar() #prints 'hello world' followed by foo
def newbar( self ):
super( **?**, self).bar()
print 'another world!'
fooNew = type('fooNew', (foo,), {'bar':newbar})
n = fooNew()
n.bar() # should print everything in f.bar() followed by 'another world!'
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用猴子修补来实现我自己的功能newbar.但是有一个细微的区别,我希望新的bar函数首先运行基类bar函数,然后才运行任何其他功能.
我怎样才能做到这一点?或者我怎么能做得更好?
我试图像这样覆盖Enumerable模块上的方法:
module Enumerable
def collect(&block)
puts 'collect'
super
end
end
Run Code Online (Sandbox Code Playgroud)
(注意这是一个简单的例子).
从理论上讲,当我打电话collect或者map,Ruby应该使用我的重写版本,对吧?但事实并非如此.它总是使用内置的Enumerable方法.是因为collect实际上是否enum_collect遵守了来源?
[1,2,3].map(&:to_s) # never prints anything
Run Code Online (Sandbox Code Playgroud)
是的,我知道Monkey-Patching是坏的,等等,我知道有其他选择,包括子类等,但我想知道是否可以用Ruby覆盖内置的C函数.
Enumerable.class_eval do
def collect(&block)
puts 'collect was class_eval'
super
end
end
Run Code Online (Sandbox Code Playgroud)
eigen = class << Enumerable; self; end
eigen.class_eval do
def collect(&block)
puts 'collect was eigen'
super
end
end
Run Code Online (Sandbox Code Playgroud)
module Enumerable
def collect(&block)
puts 'collect was opened up'
super
end
end
Run Code Online (Sandbox Code Playgroud)
Array.send(:include, Enumerable)
Run Code Online (Sandbox Code Playgroud)
几乎每一个组合.
PS.这是Ruby 1.9.3,但理想情况下我正在寻找适用于所有版本的解决方案.
有人可以解释这与Python解释器的工作原理背后的逻辑吗?这种行为只是本地线程吗?为什么第二个模块导入后第一个模块导入中的赋值仍然存在?我刚刚进行了长时间的调试会议.
external_library.py
def the_best():
print "The best!"
Run Code Online (Sandbox Code Playgroud)
modify_external_library.py
import external_library
def the_best_2():
print "The best 2!"
external_library.the_best = the_best_2
Run Code Online (Sandbox Code Playgroud)
main.py
import modify_external_library
import external_library
external_library.the_best()
Run Code Online (Sandbox Code Playgroud)
测试:
$ python main.py
The best 2!
Run Code Online (Sandbox Code Playgroud) python monkeypatching side-effects global-variables python-internals
我想在运行时向对象添加一个方法。
class C(object):
def __init__(self, value)
self.value = value
obj = C('test')
def f(self):
print self.value
setattr(obj, 'f', f)
obj.f()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: f() takes exactly 1 argument (0 given)
Run Code Online (Sandbox Code Playgroud)
但似乎 setattr 并没有将方法绑定到对象。是否有可能做到这一点?
我有一个看起来像这样的方法:
def calculate_the_thing(hsh)
hsh[:x] + hsh[:y] + hsh[:z]
end
Run Code Online (Sandbox Code Playgroud)
这需要这样的事情:
{:x => 5, :y => nil, :z => 2, :a => 5}
Run Code Online (Sandbox Code Playgroud)
我想补丁一些类,以便当+方法获得nil值时,它将其视为零.似乎合理.我怎么能这样做?
我需要猴子补丁库来替换符号的实例,并且它已被某些函数闭包引用。我需要复制那些函数(因为我也需要访问该函数的原始未打补丁版本),但是它__closure__是不可变的,而且我做不到copy.copy,因此如何在Python 2.7中创建新的闭包单元对象?
我例如给出这个功能
def f():
def incorrectfunction():
return 0
def g():
return incorrectfunction()
return g
def correctfunction():
return 42
func = f()
patched_func = patchit(f) # replace "incorrectfunction"
print func(), patched_func()
Run Code Online (Sandbox Code Playgroud)
我想看看
0, 42
Run Code Online (Sandbox Code Playgroud) Ruby max_by 方法查找数组中的最大元素。有时最大元素具有多重性,在这种情况下 max_by 只选择其中之一,看起来是任意的。当我需要所有这些时,我当前使用这种方法来查找数组数组中的最大值:
sorted=ary.sort_by{|a,b| b}.reverse
max_score=sorted.first[1]
t=sorted.take_while{|z| z[1]==max_score}
Run Code Online (Sandbox Code Playgroud)
但是我如何使用“maxes_by”方法对 Array 类进行猴子修补,该方法接受一个块,类似于 max_by,并返回最大值的数组?
monkeypatching ×10
ruby ×5
python ×4
apache-flex ×1
arrays ×1
c ×1
closures ×1
maxima ×1
metaclass ×1
preloader ×1
python-2.7 ×1
setattr ×1
side-effects ×1
sorting ×1