Skip to content

Instantly share code, notes, and snippets.

@just3ws
Last active April 5, 2017 21:32
Show Gist options
  • Save just3ws/876178a2964fb4976470cabb5b824a3c to your computer and use it in GitHub Desktop.
Save just3ws/876178a2964fb4976470cabb5b824a3c to your computer and use it in GitHub Desktop.
Inline vs Nested Ruby Module Declarations
ruby -W2 -v -e ' (ruby-2.2.2@benchprep) wip-simulation-endpoints 9a603b95e ✗
puts "before"
module Foo
puts "- Foo -"
end
puts "more"
module Foo::Bar
puts "- Foo::Bar -"
end
puts "and more"
module Foo::Bar::Baz
puts "- Foo::Bar::Baz -"
end
puts "after"
'
# ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin14]
# before
# - Foo -
# more
# - Foo::Bar -
# and more
# - Foo::Bar::Baz -
# after
ruby -W2 -v -e ' (ruby-2.2.2@benchprep) wip-simulation-endpoints 9a603b95e ✗
puts "before"
module Foo::Bar::Baz
puts "Nope"
end
puts "after"
'
# ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin14]
# before
# -e:3:in `<main>': uninitialized constant Foo (NameError)
# Recommended
ruby -W2 -v -e ' (ruby-2.2.2@benchprep) wip-simulation-endpoints 9a603b95e ✗
puts "before"
module Foo
puts "- Foo -"
module Bar
puts "-- Bar -"
module Baz
puts "--- Baz -"
end
puts "-- Bar -"
end
puts "- Foo -"
end
puts "after"
'
# ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin14]
# before
# - Foo -
# -- Bar -
# --- Baz -
# -- Bar -
# - Foo -
# after
@just3ws
Copy link
Author

just3ws commented Apr 5, 2017

building_up_module_declarations.sh depends on the modules already being loaded which can sometimes be inconsistent depending on how the application is loaded. Safer to default with nested module so the module is defined automatically as needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment