Forum: Ruby Rough idea regarding modification of core classes

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A777f1a2049d78a12ead38efb8f75f97?d=identicon&s=25 Tanner Burson (Guest)
on 2006-05-06 00:21
(Received via mailing list)
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"
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2006-05-06 05:34
(Received via mailing list)
On May 5, 2006, at 3:20 PM, Tanner Burson 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 Hodel - drbrain@segment7.net - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
37ee5fa90f5eaeef62553629382497f7?d=identicon&s=25 Leslie Viljoen (Guest)
on 2006-05-06 20:52
(Received via mailing list)
On 5/6/06, Eric Hodel <drbrain@segment7.net> 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
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2006-05-08 22:21
(Received via mailing list)
On May 6, 2006, at 11:49 AM, Leslie Viljoen 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 Hodel - drbrain@segment7.net - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
This topic is locked and can not be replied to.