Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lsiden/overider
Over-ride ruby class methods without using 'alias'
https://github.com/lsiden/overider
Last synced: about 2 months ago
JSON representation
Over-ride ruby class methods without using 'alias'
- Host: GitHub
- URL: https://github.com/lsiden/overider
- Owner: lsiden
- Created: 2011-12-23T01:47:28.000Z (about 13 years ago)
- Default Branch: master
- Last Pushed: 2011-12-23T15:24:29.000Z (about 13 years ago)
- Last Synced: 2024-03-15T03:55:06.383Z (10 months ago)
- Language: Ruby
- Homepage:
- Size: 97.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.mkd
Awesome Lists containing this project
README
# Overider
## Background
Despite Ruby's clean design and flexibility, the language has no native feature
that allows one to over-ride a method without irrevocably losing the original binding
of the method name.
`alias` is a well-worn idiom that circumvents this limitation,
but blogger Jay Fields wrote an [interesting post](http://blog.jayfields.com/2006/12/ruby-alias-method-alternative.html)
that reminds us that `alias` comes with some less-than-desireable side-effects
that may not be significant in small scripts and projects
but may become a problem for larger projects.In the [same post](http://blog.jayfields.com/2006/12/ruby-alias-method-alternative.html),
Jay offered an interesting alternative to `alias`,
using instead `Module#instance_method` and `Module#define_method`.
This is much better, but I'm not quite satisfied.
While obvious enough to experienced Ruby-hands,
people who are newer to Ruby (like me!)
might have trouble remembering the syntax of `x = self.instance_method(:x)`
and `x.bind(self).call` every time they need to use this.
What I'd really like is a way to override defined methods that's a no-brainer.So I thought about how I could take Jay's example one step further
and after some trial and lots of error came up with this.Thanks and credit go to [Jay Fields](http://blog.jayfields.com/) for his post.
## Description
A mix-in module that allows for super-clean method over-riding without resorting to `alias`
or making unbound methods visible.## Synopsis
class A
def hello
"hello"
end
end# Later, I want to overide class A methods
class A
extend Overideroveride (:hello) do |*a|
overiden(*a) + " overide"
end
endputs A.new.hello # ==> "hello overide"
## See also
* [Ruby: Alias method alternative, Jay Fields](http://blog.jayfields.com/2006/12/ruby-alias-method-alternative.html)
*## LICENSE
* [Ruby License](http://www.ruby-lang.org/en/LICENSE.txt)
*
overider.gem by Lawrence Siden is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Based on a work at github.com.