Skip to content

Instantly share code, notes, and snippets.

@peagha
Last active September 26, 2018 14:14
Show Gist options
  • Save peagha/293a2ac20e1c6435c989677d7350fbd9 to your computer and use it in GitHub Desktop.
Save peagha/293a2ac20e1c6435c989677d7350fbd9 to your computer and use it in GitHub Desktop.
How to create a global method without defining it in Object
# global method defined in Object
def pp1
'pp1!'
end
pp1 # => "pp1!"
Object.instance_methods.include?(:pp1) # => true
class PP1
def my_pp1
pp1
end
end
PP1.new.my_pp1 # => "pp1!"
# global method defined only in the "main" object
def self.pp2
'pp2!'
end
pp2 # => "pp2!"
Object.instance_methods.include?(:pp2) # => false
class PP2
def my_pp2
pp2
end
end
PP2.new.my_pp2
# => NameError: undefined local variable or method `pp2' for #<PP2:0x00007fcc8e9d87c8>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment