How do I write a monkey patch for Ruby?
I am usi开发者_如何学Cng Rails 3 and getting an error that looks like this:
undefined method `persisted?' for []:Array
I want to monkeypatch to fix this problem. First of all: what is it supposed to look like? I know very little about the nesting of the Array class in Ruby's source code. I'd appreciate the guidance.
basically you just write the class and function like you would for any other class and it gets added to the original class definition.
Like So:
class Array
def persisted?
# Does it persist?
end
end
Monkey-patching looks like this:
# patches/array.rb
class Array # Array is a top-level class
def persisted?
false # or your own implementation
end
end
# some/other/script.rb
require 'path/to/patches/array.rb'
my_array = [1, 2, 3]
puts my_array.persisted?
Now: what can you possibly mean by asking an Array instance whether it is persisted?
精彩评论