SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Aliasing class methods

    Is there any way to alias a class method? I am familiar with alias_method but I'm not sure if that works with class methods. But if there is none available im sure it wouldn't be too hard to write my own.

    But it'd be nice if Ruby has it already out there

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Until someone replies with one, I have taken it upon myself to write my own. Tested basically and it works:

    Code:
    class Object
      def self.alias_class_method(new, old)
    	new = new.to_s if new.is_a?(Symbol)
    	old = old.to_s if old.is_a?(Symbol)
    	raise ArgumentError.new('new and old must be a symbol or a string of the method name') unless new.is_a?(String) and old.is_a?(String)
    	class_eval <<-EVALEND
    	  def self.#{new}(*args)
    		self.#{old}(*args)
    	  end
    	EVALEND
      end
    end
    Usage:
    Code:
    class Classtest
       def self.callme(param, oh = nil)
     	puts param
     	puts oh unless oh.nil?
       end
       
       alias_class_method :bla, :callme
     end
     
     Classtest.callme('rawr')
     Classtest.bla('rawr')
     Classtest.bla('rawr', 'hehe')

  3. #3
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think this works:

    Code:
    class Test
       #normal scope
      class <<self
        #scope of class
         def bar
           puts "bar"
         end
    
         alias_method :foo, :bar
      end
    end

  4. #4
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2
    I think this works:
    If the class Test already exists, this works too:

    Code:
    >> class <<Test
    ?>    def bar
    >>      puts "bar"
    >>    end
    >>
    ?>    alias_method :foo, :bar
    >> end
    => #<Class:Test>
    >> Test.bar
    bar
    => nil
    >> Test.foo
    bar
    => nil
    I am familiar with alias_method but I'm not sure if that works with class methods.
    The important thing to remember is that the class is an object just like any other (You can even have "class methods" for a Class )

    Code:
    >> class Alpha
    >> end
    => nil
    >> Alpha.class
    => Class
    
    >> Alpha.methods.sort
    => ["<", "<=", "<=>", "==", "===", "=~", ">", ">=", "__id__", "__send__", "alloc
    ate", "ancestors", "autoload", "autoload?", "class", "class_eval", "class_variab
    les", "clone", "const_defined?", "const_get", "const_missing", "const_set", "con
    stants", "display", "dup", "eql?", "equal?", "extend", "freeze", "frozen?", "has
    h", "id", "include?", "included_modules", "inspect", "instance_eval", "instance_
    method", "instance_methods", "instance_of?", "instance_variable_get", "instance_
    variable_set", "instance_variables", "is_a?", "kind_of?", "method", "method_defi
    ned?", "methods", "module_eval", "name", "new", "nil?", "object_id", "private_cl
    ***_method", "private_instance_methods", "private_method_defined?", "private_met
    hods", "protected_instance_methods", "protected_method_defined?", "protected_met
    hods", "public_class_method", "public_instance_methods", "public_method_defined?
    ", "public_methods", "require", "require_gem", "require_gem_with_options", "resp
    ond_to?", "send", "singleton_methods", "superclass", "taint", "tainted?", "to_a"
    , "to_s", "type", "untaint"]
    Douglas
    Hello World

  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks! Oh of course class <<self would work! Doh! Thank you very much though for pointing that out


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •