Skip to content

Instantly share code, notes, and snippets.

@markburns
Last active July 13, 2017 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markburns/fbea61114176667f453fecc24d75c936 to your computer and use it in GitHub Desktop.
Save markburns/fbea61114176667f453fecc24d75c936 to your computer and use it in GitHub Desktop.
Defining methods at the top level scope
#!/usr/bin/env ruby
def do_something_probably_just_in_this_script!
puts "called in #{self} from #{caller[-1]}"
end
module HeresAnother
def this_one
puts "called in #{self} from #{caller[-1]}"
end
end
include HeresAnother
do_something_probably_just_in_this_script!
require "./b"
class Something
def a_method
send :do_something_probably_just_in_this_script!
end
end
Something.new.a_method
String.new.send :do_something_probably_just_in_this_script!
puts String.private_methods.grep /do_somet/
puts String.new.private_methods.grep /do_som/
send :this_one
$ ruby temp.rb
called in main from temp.rb:15:in `<main>'
called in #<Something:0x007f8a3b82d9b8> from temp.rb:17:in `<main>'
called in from temp.rb:17:in `<main>'
do_something_probably_just_in_this_script!
do_something_probably_just_in_this_script!
called in main from temp.rb:17:in `<main>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment