How to extends ruby classes

Add the quickshort algorithm to the array class.

     class Array
        def qs
            return self if size <=1
            pivot = shift
            less, more = partition{ |y| y < pivot }
            [*less.qs, pivot, *more.qs]
        end
    end

Add the factorial operation to the integer class

    class Integer
        def factorial
            return 1 if self <= 1
            self * (self-1).factorial
        end
    end

No comments yet

Leave a comment