Skip to content

Instantly share code, notes, and snippets.

@levinalex
Created January 29, 2009 10:29
Show Gist options
  • Save levinalex/54492 to your computer and use it in GitHub Desktop.
Save levinalex/54492 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# some oddities with rails constant lookup
#
# "Foo::Bar" will return the toplevel constant "Bar" the first time it is referenced
# and fail the second time.
#
# (if Foo is a class instead of a module, the second call will print a warning and
# not raise a NameError)
#
RP=/tmp/tmp_rails
rm -rf $RP
rails $RP > /dev/null
cd $RP
cat <<EOF > app/models/bar.rb
class Bar
def self.hello
puts "should never be printed"
end
end
EOF
./script/console <<EOF
module Foo
end
Foo::Bar.hello # this returns the toplevel constant without printing a warning
Foo::Bar.hello # this does fail
EOF
# prints:
#
# >> Foo::Bar.hello
# should never be printed
# >> Foo::Bar.hello
# NameError: uninitialized constant Foo::Bar
# from /Library/Ruby/Gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:445:in `load_missing_constant'
# from /Library/Ruby/Gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:77:in `const_missing'
# from (irb):5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment