-
-
Save mark/11a68288a57379caebe4 to your computer and use it in GitHub Desktop.
Nested function definitions are a bad idea in Ruby.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby -w | |
def defit | |
def ohai | |
puts "hello from #{self.class.inspect} #{self.inspect}" | |
end | |
puts "called defit in #{self.class.inspect} #{self.inspect}" | |
end | |
module MyModule | |
defit | |
end | |
:whatever.ohai |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby -w | |
module YourModule | |
def self.defit | |
ohai = ->() { puts "hello from #{self.class.inspect} #{self.inspect}" } | |
puts "called defit in #{self.class.inspect} #{self.inspect}" | |
end | |
end | |
YourModule.defit | |
:whatever.ohai | |
#=> fails (yay!) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby -w | |
module YourModule | |
def self.defit | |
def ohai | |
puts "hello from #{self.class.inspect} #{self.inspect}" | |
end | |
puts "called defit in #{self.class.inspect} #{self.inspect}" | |
end | |
end | |
YourModule.defit | |
:whatever.ohai | |
#=> fails (yay) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby -w | |
def defit | |
def ohai | |
puts "hello from #{self.class.inspect} #{self.inspect}" | |
end | |
puts "called defit in #{self.class.inspect} #{self.inspect}" | |
end | |
# module MyModule | |
defit | |
# end | |
:whatever.ohai | |
#=> works (boo) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
inside_a_different_module
is subtly wrong, too… it definesohai
as an instance method onYourModule
rather than leaving it local to the body ofdefit