模块内部的局部变量引用跟其他动态语言差别么?
➤➤ ruby -v
ruby 2.0.0p481 (2014-05-08 revision 45883) [universal.x86_64-darwin14]
➤➤ uname -v
Darwin Kernel Version 14.3.0: Mon Mar 16 23:12:31 PDT 2015; root:xnu-2782.20.48
➤➤ bundle gem a-b
create a-b/Gemfile
create a-b/Rakefile
create a-b/LICENSE.txt
create a-b/README.md
create a-b/.gitignore
create a-b/a-b.gemspec
create a-b/lib/a/b.rb
create a-b/lib/a/b/version.rb
Initializing git repo in /Users/chen/Drafts/ruby/a-b
➤➤ tree
.
├── Gemfile
├── LICENSE.txt
├── README.md
├── Rakefile
├── a-b.gemspec
└── lib
└── a
├── b
│ └── version.rb
└── b.rb
3 directories, 7 files
Rakefile
require "bundler/gem_tasks"
require 'a/b'
desc 'show'
task 'show' do
A::B::show 'demo'
end
lib/a/b.rb
require "a/b/version"
module A
module B
def B.show(a)
p "in b show a:", a
xmethod a
end
def xmethod(a)
p "in x show a", a
end
end
end
运行时候报错了:
➤➤ rake show
"in b show a:"
"demo"
rake aborted!
NoMethodError: undefined method `xmethod' for A::B:Module
/Users/chen/Drafts/ruby/a-b/lib/a/b.rb:7:in `show'
/Users/chen/Drafts/ruby/a-b/Rakefile:7:in `block in <top (required)>'
Tasks: TOP => show
(See full trace by running task with --trace)