code
# file : nest2.rb
class A
x = 'A'
module M
x = 'M'
class B
x = 'B'
def show_x
x = 'X'
puts x
end
puts x
end
puts x
end
puts x
end
puts "a = A.new"
a = A.new
puts "b = A::M::B.new"
b = A::M::B.new
puts "b.show_x"
b.show_x
output
- 仔细看输出,发现
a = A.new
以及b = A::M::B.new
的执行其实并没有带来任何输出; - 在
show_x
语句执行之后,才会打印出X; - 而
B M A
是ruby nest2.rb
命令行运行之后就直接输出了;
PS C:\Users\Administrator\RubyCode> ruby nest2.rb
B
M
A
a = A.new
b = A::M::B.new
b.show_x
X
reference
《The Well-Grounded Rubyist, Second Edition》
(https://www.manning.com/books/the-well-grounded-rubyist-second-edition)
5.2.2. Local scope
ねむ…
∧_∧
゚(´д⊂ヽ/⌒ヽ
./ ノ )
⊂_/⌒ ,, )
(ニニニニニニニニ)
http://emoji.vis.ne.jp/nemui44.htm