您的位置:首页 > 编程语言 > Ruby

[Ruby笔记]25.local scope 本地作用域

2016-06-28 20:29 435 查看

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[/code] 
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ruby local class module