Ruby 入门随笔
变量
全局变量
定义:在变量名前加"$"
例如:
$testString = "23333"
类成员
class test
#静态变量(类变量)
@@A
#普通类成员
@B
end
静态变量(类变量)
定义:在变量名前加"@@"
普通类成员
定义:在变量名前加"@"
访问类中的变量
为变量设置getter方法
def At
@@A
end
这样一来就可以通过 $变量名.At 来调用@@A
通过attr_accessor为指定的属性创建
class test
@@A
@B
attr_accessor :A,:B
end
等价于
class test
@@A
@B
attr_reader :A,:B
attr_writer :A,:B
end
与
class test
@@A
@B
def A
@@A
end
def B
@B
end
def A=(value)
@@A=value
end
def B=(value)
@B=value
end
end
这样一来就可以通过 $变量名.A来调用@@A , $变量名.B来调用@@B
函数
ruby 中函数的返回值不需要定义
def test
code
return xxx
end
ruby 中的模板引擎
ruby-on-rails
暂时没学,欸嘿