All Projects → jirutka → corefines

jirutka / corefines

Licence: MIT license
💎 A collection of refinements for Ruby core classes with a compatibility mode for older Rubies and a convenient syntactic sugar.

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to corefines

redmadrobot-android-ktx
Missing Android KTX extensions.
Stars: ✭ 27 (+3.85%)
Mutual labels:  extensions
extensions
👅 Parse Haskell Language Extensions
Stars: ✭ 45 (+73.08%)
Mutual labels:  extensions
docket-cache
A persistent object cache stored as a plain PHP code, accelerates caching with OPcache backend.
Stars: ✭ 17 (-34.62%)
Mutual labels:  extensions
crystal on steroids
A group of methods to make Crystal more programmer friendly (extracted from Rails ActiveSupport, Powerpack and others)
Stars: ✭ 48 (+84.62%)
Mutual labels:  extensions
VCore
VCore is a Swift collection containing objects, functions, and extensions that I use for my projects
Stars: ✭ 32 (+23.08%)
Mutual labels:  extensions
vuln-headers-extension
Firefox extension which parses the headers of all the requests which are being flowing through your firefox browser to detect for vulnerabilities.
Stars: ✭ 55 (+111.54%)
Mutual labels:  extensions
library
Optimizely Library
Stars: ✭ 62 (+138.46%)
Mutual labels:  extensions
UnityEventDrawerEx
This plugin extends the UnityEventDrawer to display runtime calls in the inspector.
Stars: ✭ 57 (+119.23%)
Mutual labels:  extensions
extensions-rig
A full development environment to build Twitch Extensions. Currently only supports panel extensions but video overlay coming soon.
Stars: ✭ 26 (+0%)
Mutual labels:  extensions
modfs
modify firmware for NAND-flash based FRITZ!Box routers and install it on such a device
Stars: ✭ 49 (+88.46%)
Mutual labels:  extensions
KrazyKotlin
A collection of useful Kotlin Extension
Stars: ✭ 75 (+188.46%)
Mutual labels:  extensions
windbgtree
A command tree based on commands and extensions for Windows Kernel Debugging.
Stars: ✭ 94 (+261.54%)
Mutual labels:  extensions
Boundary
Boundary is a CSS+Javascript library for Chrome extension developers to easily create HTML elements that won’t affect or be affected by the current webpage’s CSS. Strongly recommended if you are considering adding a sticker, a sidebar or any overlay box using content script.
Stars: ✭ 59 (+126.92%)
Mutual labels:  extensions
bazaar
The extension marketplace for your Flarum forum.
Stars: ✭ 58 (+123.08%)
Mutual labels:  extensions
transonic
🚀 Make your Python code fly at transonic speeds!
Stars: ✭ 93 (+257.69%)
Mutual labels:  extensions
glTF-Blender-IO-materials-variants
Blender3D addon for glTF KHR_materials_variants extension
Stars: ✭ 56 (+115.38%)
Mutual labels:  extensions
extensions-dependency-injection
Microsoft Extensions DependencyInjection for WCF, and asp.net classic projects
Stars: ✭ 18 (-30.77%)
Mutual labels:  extensions
screentime
A chrome extension for keeping track and managing your time on social media platforms and websites
Stars: ✭ 42 (+61.54%)
Mutual labels:  extensions
Analogy.LogViewer
A customizable Log Viewer with ability to create custom providers. Can be used with C#, C++, Python, Java and others
Stars: ✭ 172 (+561.54%)
Mutual labels:  extensions
detect-features
Detect and report browser and hardware features.
Stars: ✭ 63 (+142.31%)
Mutual labels:  extensions

Corefines

Build Status Test Coverage Code Climate Gem Version Yard Docs

Corefines is a collection of general purpose refinements for extending the core capabilities of Ruby’s built-in classes. It also provides a Compatibility mode for older Ruby versions and alternative Ruby implementations that don’t support refinements (yet).

Why refinements?

Extending core classes with so called monkey-paching pollutes the global scope, so it affects all files on the $LOAD_PATH, i.e. whole application including used gems. It’s not usually so big deal when you’re doing it in your application, but it’s very dangerous when used in a gem (library). This can result in strange and hard to debug behaviour if another gem overrides a core class with the same method as your gem, but different implementation, and both gems are used together.

Refinements basically allows you to put monkey patches in an isolated namespace, so that your changes to core classes don’t affect other code.

TODO

Installation

Add this line to your application’s Gemfile:

gem 'corefines', '~> 1.11.1'

or to your gemspec:

s.add_runtime_dependency 'corefines', '~> 1.11.1'

and then execute:

$ bundle install

Using

First, you must require corefines prior using:

require 'corefines'

This will not activate any extensions (just register them), even when running in compatibility mode. Extensions (refinements) are activated selectively with the method using.

Refinements are organized into modules by class which they refine, and further into submodules for individual methods. When an extension refines multiple classes, then it’s included in a module named after their nearest common ancestor (superclass).

Corefines::<CLASS>::<METHOD>

A single extension can be imported into the current scope classically, e.g.:

using Corefines::Object::ThenIf

or preferably using its “alias”:

using Corefines::Object::then_if

If you want to include all extensions for the class, then you can just import the parent module, e.g.:

using Corefines::Object

But more often you want to include multiple extensions for the class, but not all of them, e.g.:

using Corefines::Object::then_if
using Corefines::Object::in?

this can be abbreviated to:

using Corefines::Object[:then_if, :in?]

If you feel that Corefines is too long, then you can also use abbreviation CF instead:

using CF::Object::then_if

Refinements can be activated (with using) at top-level (per file), inside a class, module or a method.

Compatibility mode

Refinements are still a young feature, so there’s a possibility that your gem or application will have to work on a Ruby platform that doesn’t fully support refinements yet.

The main Ruby implementation, MRI (aka CRuby), supports refinements since version 2.1.0 (released in 25 Dec 2013). [1] Version 2.0.0 (released in 24 Feb 2013) is still supported though. JRuby doesn’t support refinements yet, it’s planned in the upcoming version 9.0.0.0 (#1062). Rubinius also doesn’t support refinements yet.

This gem is a collection of pure refinements, and yet, it works even on older Rubies that don’t support refinements. Wait… how? Well, when you use the gem with an older Ruby, it’s actually cheating. Instead of locally scoped changes, it falls back to global monkey-patching.

The Corefines gem adds refine and using methods to the core classes, so you can define and use refinements just like in newer Rubies. But internally it works very differently. The refine method adds a given block to a collection of pending “refinements” inside its module. When using is called first time for the module, it evaluates module’s “refinements” in context of the target classes (i.e. do a monkey-patch).

Not ideal indeed, but probably the best of what we can achieve.

Acknowledgement

Most of the extension methods are based on, or highly inspired from:

Very useful articles about refinements and how to “trick” them:

Contributing

  1. Fork it.

  2. Create your feature branch (git checkout -b my-new-feature).

  3. Commit your changes (git commit -am 'Add some feature').

  4. Push to the branch (git push origin my-new-feature).

  5. Create a new Pull Request.

License

This project is licensed under MIT License. For the full text of the license, see the LICENSE file.


1. Actually, refinements has been introduced to MRI in 2.0.0, as an experimental feature. However, its design and implementation has been changed then, so refinements in 2.0.x and 2.1+ behaves quite differently.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].