I am trying to write a Ruby Gem which, when require
d, adds a function to the global scope.
I have followed the ideas here: How can I add a method to the global scope in Ruby?, however, it just doesn't work! (on Ruby 2.4.3 anyway)
Here is my actual source code, but the below also summarises what I've done and what isn't working:
# example.rb
module Example
def self.hello()
puts "Hello"
end
end
extend Example
Then
# app.rb
require 'example' # Having built as a gem
hello() #=> `<main>': undefined method `hello' for main:Object (NoMethodError)
Where did I go wrong?
self.
self.
causes every RSpec test to fail - but when I rebuilt the gem anyway, it works.def hello
and later calling it as justhello
is the style to use.