I have a simple code snippet that defines a method (on Ruby's Main Object), and then checks to see if it is defined.
puts "#{self} #{self.class}"
def foo;end
puts self.methods.include?(:foo)
When I run this in a Ruby console. I get:
main Object
true
If I paste this code into a .rb file and run the file like so ruby test_script.rb
, I get the following output
main Object
false
I can't work out why I am seeing this behaviour. The method is being defined in the script, as I can call the method.
I'm running both on Ruby 2.3.4
IRB binds methods in the top level scope to main as public methods for convienience, but regular Ruby programs bind methods defined in the top level scope to main as private methods.
You can find the reference here Ruby main top level context
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With