On Tue, 13 Dec 2005, Jeremy Kemper wrote:

I'd like a Module#wrap_method in Active Support to DRY up the repetitive,
error-prone aliasing and give us a single place to more thoroughly check for
double-wrapping.

    [EMAIL PROTECTED] ~]$ cat a.rb
    require "tsort"

    class TSortHash < ::Hash
      include TSort
      def tsort_each_node *a, &b
        each_key *a, &b
      end
      def tsort_each_child *a, &b
        (self[a.shift] || []).each &b
      end
    end

    class Module
      unless @__alias_method_redfined__
        alias_method "__alias_method__", "alias_method" # yikes!
        def alias_method dst, src
          dst, src = dst.to_s, src.to_s
          begin
            (@__aliased_methods__ ||= TSortHash::new).update(dst => [src]).tsort
          rescue TSort::Cyclic => e
            raise "circular definition of <#{ dst }> => <#{ src }>!"
          end
          __alias_method__ dst, src
        end
        @__alias_method_redfined__ = true
      end
    end

    module Kernel
      alias_method "foo", "puts"
      alias_method "bar", "foo"
    end

    bar 42

    module Kernel
      alias_method "puts", "bar"
    end



    [EMAIL PROTECTED] ~]$ ruby a.rb
    42
    a.rb:21:in `alias_method': circular definition of <puts> => <bar>! 
(RuntimeError)
            from a.rb:37


testers on different boxen?  feedback?

-a
--
===============================================================================
| ara [dot] t [dot] howard [at] noaa [dot] gov
| all happiness comes from the desire for others to be happy.  all misery
| comes from the desire for oneself to be happy.
| -- bodhicaryavatara
===============================================================================

_______________________________________________
Rails-core mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails-core

Reply via email to