Getting an indication of the number of arguments accepted by a method

Posted by root Mon, 16 Jun 2008 08:57:00 GMT

class Object
  def arguments?(*methods)
    methods.each do |method|
      begin
        rval = self.method(method).arity
        if rval > 0
          puts "'#{method}' takes fixed (#{rval}) number of argument"
        elsif rval < 0
          puts "'#{method}' takes variable number of arguments"
        elsif rval == 0
          puts "'#{method}' takes no arguments"
        end
      rescue
       puts $!
      end
    end
  end
end

String.new.arguments?(:size, :to_i, :scan, :flatten)

www.ruby-doc.org arity

Posted in  | Tags  | no comments

Comments

Comments are disabled