https://github.com/jimmycuadra/guerilla_patch
Monkey patch objects using Ruby 2.0 refinements, if available.
https://github.com/jimmycuadra/guerilla_patch
library monkey-patching ruby ruby-refinements
Last synced: 6 months ago
JSON representation
Monkey patch objects using Ruby 2.0 refinements, if available.
- Host: GitHub
- URL: https://github.com/jimmycuadra/guerilla_patch
- Owner: jimmycuadra
- License: mit
- Created: 2012-11-02T08:19:39.000Z (about 13 years ago)
- Default Branch: master
- Last Pushed: 2012-11-04T11:59:17.000Z (about 13 years ago)
- Last Synced: 2024-04-24T22:42:46.132Z (almost 2 years ago)
- Topics: library, monkey-patching, ruby, ruby-refinements
- Language: Ruby
- Size: 125 KB
- Stars: 27
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
[](https://travis-ci.org/jimmycuadra/guerilla_patch)
# GuerillaPatch
**GuerillaPatch** is a Ruby gem to ease the transition to Ruby 2.0 when redefining behavior in existing objects (monkey patching). Ruby 2.0 introduces a new feature called *refinements* which allows you to create monkey patches that only exist in a scope where the refinement is explicitly included, preventing your changes from affected code that has not opted in.
Using GuerillaPatch, you can use a common interface for writing a monkey patch that will use refinements if available, working seemlessly for both Ruby 1.9 and Ruby 2.0.
## Installation
Add this line to your application's Gemfile:
gem 'guerilla_patch'
And then execute:
$ bundle
Or install it yourself with:
$ gem install guerilla_patch
## Usage
To create a new monkey patch, use `GuerillaPatch.patch`. The first argument is the object you are patching, and the second argument is the name to use for the refinement if run under Ruby 2.0. Pass a block with the methods you want to add to the object, or any other modifications you want to make.
``` ruby
GuerillaPatch.patch(Fixnum, "TimeExtensions") do
def minutes
self * 60
end
end
```
Then, in Ruby 1.9:
``` ruby
2.minutes # => 120
```
And in Ruby 2.0:
``` ruby
class MyApp
using TimeExtensions
def minutes_to_seconds(m)
m.minutes
end
end
MyApp.new.minutes_to_seconds(2) # => 120
```