fredwu/ruby_decorators
Ruby method decorators inspired by Python.
{ "createdAt": "2012-09-11T16:27:52Z", "defaultBranch": "master", "description": "Ruby method decorators inspired by Python.", "fullName": "fredwu/ruby_decorators", "homepage": null, "language": "Ruby", "name": "ruby_decorators", "pushedAt": "2013-11-09T07:23:10Z", "stargazersCount": 63, "topics": [], "updatedAt": "2020-09-30T16:57:39Z", "url": "https://github.com/fredwu/ruby_decorators"}Ruby Decorators
Section titled “Ruby Decorators”Ruby method decorators inspired by Python.
Section titled “Ruby method decorators inspired by Python.”I wrote this as a small practice for some ruby meta-programming fun. The implementation is relatively simple, and is thread safe.
There are also these other two implementations:
- Yehuda Katz’s Ruby Decorators
- Michael Fairley’s Method Decorators
Installation
Section titled “Installation”Add this line to your application’s Gemfile:
gem 'ruby_decorators'And then execute:
$ bundleOr install it yourself as:
$ gem install ruby_decoratorsclass Hi < RubyDecorator def call(this, *args, &blk) this.call(*args, &blk).sub('hello', 'hi') endend
class Batman < RubyDecorator def call(this, *args, &blk) this.call(*args, &blk).sub('world', 'batman') endend
class Catwoman < RubyDecorator def initialize(*args) @args = args.any? ? args : ['catwoman'] end
def call(this, *args, &blk) this.call(*args, &blk).sub('world', @args.join(' ')) endend
class World extend RubyDecorators
def initialize @greeting = 'hello world' end
def hello_world @greeting end
+Batman def hello_batman @greeting end
+Hi +Catwoman def hello_catwoman @greeting end
+Catwoman.new('super', 'catwoman') def hello_super_catwoman @greeting endend
world = World.new
world.hello_world # => "hello world"world.hello_batman # => "hello batman"world.hello_catwoman # => "hi catwoman"world.hello_super_catwoman # => "hello super catwoman"License
Section titled “License”Copyright (c) 2012 Fred Wu
Licensed under the MIT license.
