Ruby 入门随笔

Ruby 入门随笔

变量

全局变量

定义:在变量名前加"$"  
例如:
1
$testString = "23333"

类成员

1
2
3
4
5
6
class test
#静态变量(类变量)
@@A
#普通类成员
@B
end

静态变量(类变量)

定义:在变量名前加"@@"

普通类成员

定义:在变量名前加"@"

访问类中的变量

为变量设置getter方法
1
2
3
def At
@@A
end

这样一来就可以通过 $变量名.At 来调用@@A

通过attr_accessor为指定的属性创建
1
2
3
4
5
class test
@@A
@B
attr_accessor :A,:B
end

等价于

1
2
3
4
5
6
class test
@@A
@B
attr_reader :A,:B
attr_writer :A,:B
end

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 中函数的返回值不需要定义

1
2
3
4
5
def test
code

return xxx
end

ruby 中的模板引擎

ruby-on-rails

暂时没学,欸嘿

简单使用

https://www.imooc.com/wenda/detail/564294