SixArm.com » Ruby »
Class#defining and #redefining metaprogramming methods

Introduction

Meta-programming method that we call when we are defining (or redefining) a class method, to detect if we're accidentally overriding an existing method.

Example of defining a new method:

class C
  defining "foo" 
  #=> return true; it's safe to proceed because #foo is not defined.

Example of error detection:

class C
  defining "hash"
  #=> raise an error; it's not safe to proceed because #hash is already defined.

Example of intentionally overriding an existing method:

class C
  redefining "hash"  
  #=> return true; it's safe to proceed because #hash is already defined.

Example of error detection:

class C
  redefining "foo"  
  #=> raise an error; it's not safe to proceed because #foo is not yet defined.

For docs go to http://sixarm.com/sixarm_ruby_defining/doc

Want to help? We're happy to get pull requests.

Quickstart

Install:

gem install sixarm_ruby_defining

Bundler:

gem "sixarm_ruby_defining", "=1.1.0"

Require:

require "sixarm_ruby_defining"

Install with high security (optional)

To enable high security for all our gems:

wget http://sixarm.com/sixarm.pem
gem cert --add sixarm.pem
gem sources --add http://sixarm.com

To install with high security:

gem install sixarm_ruby_defining --test --trust-policy HighSecurity

Multiple method names

The methods can take multiple names.

Example of #defining multiple names:

class C
  defining "foo", "goo", "hoo"
  #=> return true if we're safe i.e. all the methods are not yet defined,
  #=> otherwise raise an error for each method that is already defined.

Example of #redefining multiple names:

class C
  redefining "hash", "inspect", "equal?"
  #=> return true if all the methods are already defined,
  #=> otherwise raise an error for each method that is not yet defined.

Intermixing

We can intermix the methods however we like:

class C
  ...
  defining "foo", "goo"
  ...
  redefining "hash", "inspect"
  ...
  defining "goo"
  ...
  redefining "equals?"

Customization for success and failure

For meta-programming we can customize what happens when #defining and #redefining encounter methods that are

Changes

License

You may choose any of these open source licenses:

The software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement.

In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software.

This license is for the included software that is created by SixArm; some of the included software may have its own licenses, copyrights, authors, etc. and these do take precedence over the SixArm license.

Copyright (c) 2005-2012 Joel Parker Henderson