Object Oriented Programming

Class Methods in Ruby

Used to declare methods called on the class itself (static).


Syntax
#class function, called on the class itself (static method)
def self.functionName(passedParameters)
    #code to be executed
end

#calling the function
className.functionName(passedParameters)

Notes

Implementation of class methods is the same across all class instances.

In general, methods are functions declared within classes.


Example
class Math
    def self.Add(a, b)
        return a + b
    end
end

puts "1+1 = " + Math.add(1, 1)

See Also
Related

Functions

Add to Slack

< Modules   |   Print >

© 2019 SyntaxDB. All Rights Reserved.