almost 9 years ago
Object Methods:
Example:
class Object
def initialize (name)
@name=name
end
def display
puts @name
end
end
obj = Object.new("Rahul") #creating the object of the class
obj.display
class Object
def initialize (name)
@name=name
end
def display
puts @name
end
end
obj = Object.new("Rahul") #creating the object of the class
obj.display
Output: Rahul
Class Methods:
Example:
class Apple
def self.fruit #self is a class method
puts "An Apple is an healthy fruit"
end
end
Apple.fruit #Invoking the fruit method by using the name of the class Apple
class Apple
def self.fruit #self is a class method
puts "An Apple is an healthy fruit"
end
end
Apple.fruit #Invoking the fruit method by using the name of the class Apple
Output: An Apple is an healthy fruit
0 Comment(s)