Answer the question
In order to leave comments, you need to log in
How to make a method public in Ruby?
There is a code:
# encoding: UTF-8
module SharedMethods
def meth1
puts 'meth1'
end
def meth2
puts 'meth2'
end
end
class SlaveClass
def blabla
meth2
end
end
module MasterModule
def do_some
SlaveClass.new().blabla
end
end
Answer the question
In order to leave comments, you need to log in
If you want methods to be available everywhere, you will have to extend Object, I would do it through refinements , use using only in those places where you need these methods
Example:
module SuperString
refine Object do
def ii
puts "ii"
end
end
end
using SuperString
5.ii
"r".ii
ii
In order for the `SharedMethods` module to be available in the `SlaveClass`, it must be included.
class SlaveClass
include SharedMethods
def blabla
meth2
end
end
module MasterModule
def self.do_some
SlaveClass.new.blabla
end
end
module SharedMethods
def meth1
puts 'meth1'
end
def meth2
puts 'meth2'
end
end
class Object
include SharedMethods
end
#
class SlaveClass
def blabla
meth2
meth1
end
end
module MasterModule
def self.do_some
SlaveClass.new.blabla
end
end
MasterModule.do_some
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question