Project

overider

0.0
No commit activity in last 3 years
No release in over 3 years
class A def hello "hello" end end # Later, I want to overide class A methods class A extend Overider overide (:hello) do |*a| overiden(*a) + " overide" end end A.new.hello # ==> "hello overide"
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

>= 0
 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 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, 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 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 Overider

  overide (:hello) do |*a|
    overiden(*a) + " overide"
  end
end

puts A.new.hello # ==> "hello overide"

See also

LICENSE