Use of classes and objects in ruby id different to other languages . And tell how to declare and define function in ruby and how to call the functions .
Example : In below example i am creating a program of simple arithmetic program .
class Arith
def addition(a,b)
puts a+b
end
def subtraction(a,b)
puts a-b
end
def multiplication(a,b)
puts a*b;
end
def divide(a,b)
puts a/b
end
def xor(a,b)
puts a^b;
end
// self loop means it is the class method
def self.help
puts "hepl us"
end
end
Arith.help // no need to create objects to call class methods
x = Arith.new // need to create objects to call objects methods
x.addition(4,4) // output : 8
x.subtraction(3,2) // output : 1
x.multiplication(3,2) // output : 6
x.divide(5,5) // output : 1
x.xor(1,1) // output : 0
0 Comment(s)