using module function in main ruby
module MathFunctions
def self.double(var)
var * 2
end
def self.triple(var)
var * 3
end
end
puts MathFunctions.double(5) # Output: 10
puts MathFunctions.triple(3) # Output: 9
Explanation:
module MathFunctions
: Defines a module namedMathFunctions
.def self.double(var)
: Defines a methoddouble
inside theMathFunctions
module that takes a parametervar
.var * 2
: Multiplies thevar
parameter by 2.end
: Ends thedouble
method definition.def self.triple(var)
: Defines a methodtriple
inside theMathFunctions
module that takes a parametervar
.var * 3
: Multiplies thevar
parameter by 3.end
: Ends thetriple
method definition.puts MathFunctions.double(5)
: Calls thedouble
method from theMathFunctions
module with an argument of5
and prints the result, which is10
.puts MathFunctions.triple(3)
: Calls thetriple
method from theMathFunctions
module with an argument of3
and prints the result, which is9
.