# From method redefinition to classless Ruby

**URL:** <https://www.ruby-forum.com/t/from-method-redefinition-to-classless-ruby/111133>\
**Category:** Ruby\
**Created:** [August 2, 2007, 10:44pm UTC](https://www.ruby-forum.com/t/from-method-redefinition-to-classless-ruby/111133 "2007-08-02T22:44:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [August 2, 2007, 10:44pm UTC](https://www.ruby-forum.com/t/from-method-redefinition-to-classless-ruby/111133/1 "2007-08-02T22:44:19Z")

</div>

Hi list

Ara’s post about method redefinition was extremely rich intellectual  
food, took some time to digest, but I just thought I share my findings  
with you.

[http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

Cheers  
Robert

---

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [August 3, 2007, 5:01am UTC](https://www.ruby-forum.com/t/from-method-redefinition-to-classless-ruby/111133/2 "2007-08-03T05:01:53Z")

</div>

On Aug 2, 4:43 pm, “Robert D.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi list
> 
> Ara’s post about method redefinition was extremely rich intellectual  
> food, took some time to digest, but I just thought I share my findings  
> with you.

Ara and Pit are impressive meta-coders indeed. Think Ara might even  
have a Prototype lib  
you’d enjoy.

BTW, if we were all good little scouts and always modularized our code  
one could use:

class Module  
# Prepend an “aspect” module to a module.  
#  
# module X  
# def x; “x”; end  
# end  
#  
# module U  
# def x; ‘{’ + super + ‘}’; end  
# end  
#  
# X.prepend U  
#  
# X.x # =\> “{x}”

```
def prepend( aspect )
  aspect.send(:include, self)
  extend aspect
end

```

end

Another trick for common injuns:

class Class

```
# Prepend an "aspect" module to a class.
#
# class Firetruck
# def put_out_fire(option)
# "Put out #{option}"
# end
# end
#
# module FastFiretruck
# def put_out_fire(option)
# super("very #{option}!")
# end
# end
#
# Firetruck.prepend(FastFiretruck)
#
# ft = Firetruck.new
# ft.put_out_fire('fast') #=> "Put out very fast!"

def prepend( aspect )
  _new = method(:new)
  _allocate = method(:allocate)
  (class << self; self; end).class_eval do
    define_method(:new) do |*args|
      o = _new.call(*args)
      o.extend aspect
      o
    end
    define_method(:allocate) do |*args|
      o = _allocate.call(*args)
      o.extend aspect
      o
    end
  end
end

```

end

Albeit that has some unfortunate shortcomings.

T.

---

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [August 3, 2007, 11:26am UTC](https://www.ruby-forum.com/t/from-method-redefinition-to-classless-ruby/111133/3 "2007-08-03T11:26:38Z")

</div>

On 8/3/07, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> have a Prototype lib  
> # end  
> aspect.send(:include, self)  
> # class Firetruck  
> #  
> o = \_new.call(\*args)
> 
> end
> 
> Albeit that has some unfortunate shortcomings.
> 
> T.  
> Well I do not really see these shortcomings, I’d love to have this  
> mail as a comment to my blogpost BTW, would you mind?

Concerning Ara’s Prototype library I have seen it flying by, I will  
have a closer look, sure deserves an Exolink. But probably Ara’s had  
all the fun for himself already and there is nothing left to do for me  
:(.

Cheers  
Robert

---

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [August 3, 2007, 12:38pm UTC](https://www.ruby-forum.com/t/from-method-redefinition-to-classless-ruby/111133/4 "2007-08-03T12:38:46Z")

</div>

On Aug 3, 2:25 am, “Robert D.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > # module X
> > 
> > ```
> 
> > ```
> > # Prepend an "aspect" module to a class.
> > # end
> > (class << self; self; end).class_eval do
> > end
> > end
> > 
> > ```
> 
> > end
> 
> > Albeit that has some unfortunate shortcomings.
> 
> > T.
> 
> Well I do not really see these shortcomings, I’d love to have this  
> mail as a comment to my blogpost BTW, would you mind?

Go for it. The shortcoming in the later example are b/c it’s static  
and not dynamic, ie. it happens at the time of instantiation. So it’s  
not a simple thing to adjust on the fly. One could make it more  
flexiable, but alas the Double Module Inclusion problem gets in the  
way.

> Concerning Ara’s Prototype library I have seen it flying by, I will  
> have a closer look, sure deserves an Exolink. But probably Ara’s had  
> all the fun for himself already and there is nothing left to do for me  
> :(.

Not at all, I think Ara’s been looking for Prototype allies 🙂 The  
only problem with prototype.rb is that it’s so far removed for writing  
ordinary Ruby, few are willing to embrace it --it’s almost like  
writing in a different language.

T.

---

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [August 4, 2007, 4:56pm UTC](https://www.ruby-forum.com/t/from-method-redefinition-to-classless-ruby/111133/5 "2007-08-04T16:56:30Z")

</div>

2007/8/2, Robert D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Ara’s post about method redefinition was extremely rich intellectual  
> food, took some time to digest, but I just thought I share my findings  
> with you.
> 
> [http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

Robert, c’est très intéressant! I will keep your code and play a  
little bit with it some time.

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [August 4, 2007, 6:25pm UTC](https://www.ruby-forum.com/t/from-method-redefinition-to-classless-ruby/111133/6 "2007-08-04T18:25:41Z")

</div>

On 8/4/07, Pit C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 2007/8/2, Robert D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > Ara’s post about method redefinition was extremely rich intellectual  
> > food, took some time to digest, but I just thought I share my findings  
> > with you.
> > 
> > [http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)
> 
> Robert, c’est très intéressant! I will keep your code and play a  
> little bit with it some time.

Merci, very kind of you, I am sure you are aware of the very basic  
Object#deepdup, I am about to “fix” it…  
it will become

Kernel# **deepdup** object, object\_track={}

I guess you know what it will eventually do, it is the workerbee stuff  
which is not interesting but has to be done :(.

Cheers  
Robert
