i’m still new to ruby. i dont understand the methods visibility. the documentation says, all methods are public by default (unless defined otherwise). so this should work (but it doesn’t, MWE):
modules/example.rb:
class Example
def do_stuff
puts 'hello world'
end
end
and testing.rb:
load 'modules/example.rb'
Example.new
Example.do_stuff
calling $ ruby testing.rb
results in
testing.rb:9:in `<main>': undefined method `do_stuff' for Example:Class (NoMethodError)
Can someone explain why? And how to fix it that I can call do_stuff directly?
You are defining an instance method and need to call it on an instance of Example class:
If you want to call it directly you need to define it as a class method:
then you can call it like this without the need of calling
Example.new