Ruby 中的各种变量(local/instance/class/global variable and assignment method)
2016-08-18 11:06
537 查看
从注释中可以看出每段代码中使用的变量类型
# local variable 10.times{ |i| print("=")} puts("local variable") 1.times do a = 1 b = "a" puts "local variables in the block: #{local_variables.join ", "}" end puts "no local variables outside the block" if local_variables.empty? # instance variable 10.times{ |i| print("=")} puts("instance variable") class C def initialize(value) @instance_variable = value end def value @instance_variable end end object1 = C.new "some value" object2 = C.new "other value" p object1.value # prints "some value" p object2.value # prints "other value" #class variable 10.times{ |i| print("=")} puts("class variable") class A @@class_variable = 0 def value @@class_variable end def update @@class_variable = @@class_variable + 1 end end class B < A def update @@class_variable = @@class_variable + 2 end end a = A.new b = B.new puts "A value: #{a.value}" #0 puts "B value: #{b.value}" #0 puts "update A" a.update puts "A value: #{a.value}" puts "B value: #{b.value}" puts "update B" b.update puts "A value: #{a.value}" puts "B value: #{b.value}" puts "update A" a.update puts "A value: #{a.value}" puts "B value: #{b.value}" #global variable 10.times{ |i| print("=")} puts("global variable") $global = 0 class E puts "in a class: #{$global}" def my_method puts "in a method: #{$global}" $global = $global + 1 $other_global = 3 end end E.new.my_method puts "at top-level, $global: #{$global}, $other_global: #{$other_global}" # Assignment method 10.times{ |i| print("=")} puts("Assignment method") class F @value attr_accessor :value def my_method #self.value = 42 @value = 42; puts "local_variables: #{local_variables.join ", "}" puts "@value: #{@value.inspect}" end end F.new.my_method
相关文章推荐
- OC第二讲(InstanceVariableAndMethod实例变量和方法)
- Oc(实例变量可见度与方法)(InstanceVariableAndMethod)
- [Ruby笔记]28.Ruby @@class_variables 类变量 vs @instance_variable 实例变量
- 【Python基础】之全局变量(UnboundLocalError: local variable referenced before assignment)
- 全局变量报错:UnboundLocalError: local variable 'l' referenced before assignment
- PHP 全局变量(Global Scope Variable) vs 局部变量(Local Scope Variable)
- global variable and local variable
- 全局变量报错 UnboundLocalError: local variable 'todayPrice' referenced before assignment
- Partial mock local private method or public method in the class and suppress static initial block
- Ruby中 局部变量(local variable) 块变量(block variable) 与块局部变量(block local variable)
- python global and local variable
- What’s the difference between a class variable and an instance variable?
- 全局变量报错:UnboundLocalError: local variable 'l' referenced before assignment
- Local variable AND Instance variable / 局部变量 与 实例变量区别
- Something about Instance Method and Class Method
- Python全局变量global和局部变量 UnboundLocalError: local variable 'x' referenced before assignment
- 016: class, objects and instance: instance method
- Perl: class method and instance method
- Class and Instance Variables In Ruby
- global variable and local variable