Coder Social home page Coder Social logo

guerilla_patch's Introduction

Build Status

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.

GuerillaPatch.patch(Fixnum, "TimeExtensions") do
  def minutes
    self * 60
  end
end

Then, in Ruby 1.9:

2.minutes # => 120

And in Ruby 2.0:

class MyApp
  using TimeExtensions

  def minutes_to_seconds(m)
    m.minutes
  end
end

MyApp.new.minutes_to_seconds(2) # => 120

guerilla_patch's People

Contributors

jimmycuadra avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.