Ruby 中的各种变量(local/instance/class/global variable and assignment method)
来源:互联网 发布:同花顺扩展数据管理器 编辑:程序博客网 时间:2024/05/16 04:49
从注释中可以看出每段代码中使用的变量类型
# local variable10.times{ |i| print("=")}puts("local variable")1.times do a = 1 b = "a" puts "local variables in the block: #{local_variables.join ", "}"endputs "no local variables outside the block" if local_variables.empty?# instance variable10.times{ |i| print("=")}puts("instance variable")class C def initialize(value) @instance_variable = value end def value @instance_variable endendobject1 = C.new "some value"object2 = C.new "other value"p object1.value # prints "some value"p object2.value # prints "other value"#class variable10.times{ |i| print("=")}puts("class variable")class A @@class_variable = 0 def value @@class_variable end def update @@class_variable = @@class_variable + 1 endendclass B < A def update @@class_variable = @@class_variable + 2 endenda = A.newb = B.newputs "A value: #{a.value}" #0puts "B value: #{b.value}" #0puts "update A"a.updateputs "A value: #{a.value}"puts "B value: #{b.value}"puts "update B"b.updateputs "A value: #{a.value}"puts "B value: #{b.value}"puts "update A"a.updateputs "A value: #{a.value}"puts "B value: #{b.value}"#global variable10.times{ |i| print("=")}puts("global variable")$global = 0class E puts "in a class: #{$global}" def my_method puts "in a method: #{$global}" $global = $global + 1 $other_global = 3 endendE.new.my_methodputs "at top-level, $global: #{$global}, $other_global: #{$other_global}"# Assignment method10.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}" endendF.new.my_method
0 0
- Ruby 中的各种变量(local/instance/class/global variable and assignment method)
- Ruby中的 class method, instance method, singleton method/class
- global variable and local variable
- python global and local variable
- Python全局变量global和局部变量 UnboundLocalError: local variable 'x' referenced before assignment
- instance variable 'name' accessed in class method
- Perl: class method and instance method
- Something about Instance Method and Class Method
- java中instance variable,class variable,local variable之间的区别
- ios 报错instance variable 'name' accessed in class method
- ruby 's instance variable
- Python中的static method、class method和instance method
- Class Instance Variable
- PHP 全局变量(Global Scope Variable) vs 局部变量(Local Scope Variable)
- [Ruby笔记]22.Ruby :: namespace 以及 instance method 与class method
- Spring Class Global Method
- global and local
- Global and Local Variables
- StateListDrawable cannot be cast to android.graphics.drawable.BitmapDrawable
- 模拟题3——c++的引用
- Git fetch和git pull的区别
- Android studio 下gradle配置不同环境的包配置(debug,release,innerRelease等)
- UILabel
- Ruby 中的各种变量(local/instance/class/global variable and assignment method)
- Hive(五):hive与hbase整合
- 上下界网络流小结
- VirtualBox安装Android x86虚拟机
- Android开发-Sharedpreferences-存储数据使用方法-完整Demo-AndroidStudio
- Java 实现顺时针螺旋二维数组输出
- android源码环境内置已编译好的模块apk_so_jar_bin
- Logger 与Log4j 使用介绍
- 二叉树的镜像 (剑指offer)!!!(两个有序链表的合并,链表的逆置)