# Rough idea regarding modification of core classes

**URL:** <https://www.ruby-forum.com/t/rough-idea-regarding-modification-of-core-classes/59748>\
**Category:** Ruby\
**Created:** [May 6, 2006, 12:21am UTC](https://www.ruby-forum.com/t/rough-idea-regarding-modification-of-core-classes/59748 "2006-05-06T00:21:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tanner\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecd19e/32.png) [@Tanner\_B](https://www.ruby-forum.com/u/Tanner_B)\
**Post date:** [May 6, 2006, 12:21am UTC](https://www.ruby-forum.com/t/rough-idea-regarding-modification-of-core-classes/59748/1 "2006-05-06T00:21:51Z")

</div>

After reading the sharp knives and glue thread, my head started spinning  
around some ways to minimize or localize the modifications to core  
classes.  
This is what I came up with, it’s obviously rough, not-all-inclusive,  
and  
has some fairly obvious and severe drawbacks, but none-the-less I  
thought  
I’d share my 20 minutes of fiddling in the hopes of sparking some  
conversation if nothing else.

class Object  
def define\_meta\_methods(name,&blk)  
@@ **meta\_methods** =  
(Object.class\_variables.include?("@@\_\_meta\_methods")  
? @@ **meta\_methods** : Hash.new)  
@@ **meta\_methods** [name] = Array.new if  
@@ **meta\_methods** [name].nil?  
@@ **meta\_methods** [name] \<\< blk  
end

```
def add_meta_methods
    @@ __meta_methods__ [self.class].each do |mm|
        instance_eval &mm
    end
end

```

end

Object.define\_meta\_methods(String) do  
def test\_meta  
puts “Tested Meta!”  
end  
end

Object.define\_meta\_methods(String) do  
@something=“Hello”  
def something; @something; end; # had to go this route instead of  
attr\_reader, because of private restriciton. Can avoid it with the send  
hack though.  
end

some\_string = String.new  
some\_other\_string = String.new

some\_string.add\_meta\_methods  
some\_string.test\_meta # -\> “Tested Meta!”  
puts some\_string.something # -\> “Hello”

some\_other\_string.test\_meta #–\> “No method error”

---

<div class="post-metadata">

**Author:** ![Tanner\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecd19e/32.png) [@Tanner\_B](https://www.ruby-forum.com/u/Tanner_B)\
**Post date:** [May 6, 2006, 5:34am UTC](https://www.ruby-forum.com/t/rough-idea-regarding-modification-of-core-classes/59748/2 "2006-05-06T05:34:16Z")

</div>

On May 5, 2006, at 3:20 PM, Tanner B. wrote:

> After reading the sharp knives and glue thread, my head started  
> spinning  
> around some ways to minimize or localize the modifications to core  
> classes.  
> This is what I came up with, it’s obviously rough, not-all-  
> inclusive, and  
> has some fairly obvious and severe drawbacks, but none-the-less I  
> thought  
> I’d share my 20 minutes of fiddling in the hopes of sparking some  
> conversation if nothing else.

[…]

Ruby does all that with #extend.

module Meta

attr\_reader :something

def self.extended(o)  
o.instance\_variable\_set :@something, ‘Hello’  
end

def test\_meta  
puts ‘Tested Meta!’  
end

end

> some\_string = String.new  
> some\_other\_string = String.new

some\_string.extend Meta

> some\_string.test\_meta # → “Tested Meta!”  
> puts some\_string.something # → “Hello”
> 
> some\_other\_string.test\_meta #–\> “No method error”

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![Tanner\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecd19e/32.png) [@Tanner\_B](https://www.ruby-forum.com/u/Tanner_B)\
**Post date:** [May 6, 2006, 8:52pm UTC](https://www.ruby-forum.com/t/rough-idea-regarding-modification-of-core-classes/59748/3 "2006-05-06T20:52:02Z")

</div>

On 5/6/06, Eric H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > I’d share my 20 minutes of fiddling in the hopes of sparking some  
> > def self.extended(o)  
> > some\_other\_string = String.new
> 
> some\_string.extend Meta
> 
> > some\_string.test\_meta # → “Tested Meta!”  
> > puts some\_string.something # → “Hello”
> > 
> > some\_other\_string.test\_meta #–\> “No method error”

This is cool, very similar to the “adding functionality to the strings  
passed to me” idea (in the glue thread), except it’s cleaner than  
having a def inside every function that wants an extended string. Now,  
is there a clever way to extend all strings passed to any methods of a  
specific class?

Les

---

<div class="post-metadata">

**Author:** ![Tanner\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecd19e/32.png) [@Tanner\_B](https://www.ruby-forum.com/u/Tanner_B)\
**Post date:** [May 8, 2006, 10:21pm UTC](https://www.ruby-forum.com/t/rough-idea-regarding-modification-of-core-classes/59748/4 "2006-05-08T22:21:52Z")

</div>

On May 6, 2006, at 11:49 AM, Leslie V. wrote:

> > > thought
> > 
> > > some\_string = String.new  
> > > having a def inside every function that wants an extended string. Now,  
> > > is there a clever way to extend all strings passed to any methods of a  
> > > specific class?

Use delegate.rb.

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)
