1. 成员变量的读写属性: attr_reader 和 attr_writer
class Test

def initialize(name)
@name
=
name
end
attr_reader
:
name
attr_writer
:
name

end

test
=
Test
.
new(
"AAA
"
)
class Test

def initialize(name)
@name
=
name
end
def getName
return
@name
end
def setName(name)
@name
=
name
end
end

test
=
Test
.
new(
"
AAA
"
)
test
.
setName(
"
BBB
"
)
puts test
.
getName
class Song

@
@count
=
0

def initialize
@count
=
0
end
def play
@count
=
@count
+
1
@
@count
=
@
@count
+
1
puts
"
This song #@count plays. Total #@@count plays.
"
end

end

s1
=
Song
.
new()
s2
=
Song
.
new()
s1
.
play
s2
.
play
s1
.
play
s1
.
play
class Song

@
@count
=
0
def Song
.
TotalCount
@
@count
end

end

s1
=
Song
.
new
puts Song
.
TotalCount
class Song

private_class_method
:
new
@
@name
=
nil
def Song
.
create
@
@name
=
new
unless
@
@name
@
@name
end

end

song
=
Song
.
create
class Test

a
=
[
'
ant
'
,
'
bee
'
,
'
cat
'
,
'
dog
'
]
puts a[
0
]
b
=
%w
{ ant bee cat dog }
puts b[
2
]
c
=
{
'
a
'
=>
'
ant
'
,
'
b
'
=>
'
bee
'
,
'
c
'
=>
'
cat
'
,
'
d
'
=>
'
dog
'
}
puts c[
'
d
'
]
d
=
Hash
.
new
d[
'
a
'
]
=
'
ant
'
d[
'
b
'
]
=
'
bee
'
d[
'
c
'
]
=
'
cat
'
d[
'
d
'
]
=
'
dog
'
puts d.length
end
class Test

person1
=
"
Tim
"
person2
=
person1
person1[
0
]
=
'
J
'
puts person1
puts person2
end
class Test

person1
=
"
Tim
"
person2
=
person1
.
dup
person1[
0
]
=
'
J
'
puts person1
puts person2
end
class Test

person1
=
"
Tim
"
person2
=
person1
person1
.
freeze
person1[
0
]
=
'
J
'
puts person1
puts person2
end
class Test
def initialize(name)
@name
=
name
end
attr_reader
:
name
attr_writer
:
name
end
test
=
Test
.
new(
"AAA
"
)
puts test.name
test
.
name
=
"
BBB
"
puts test
.
name
test
.
name
=
"
BBB
"
puts test
.
name
如果不使用这2个关键字,则按照Java的风格这样写
class Test
def initialize(name)
@name
=
name
end
def getName
return
@name
end
def setName(name)
@name
=
name
end
end
test
=
Test
.
new(
"
AAA
"
)
test
.
setName(
"
BBB
"
)
puts test
.
getName
注意:getName方法中的return是可以省去的。
2.类变量和类方法
class Song
@
@count
=
0

def initialize
@count
=
0
end
def play
@count
=
@count
+
1
@
@count
=
@
@count
+
1
puts
"
This song #@count plays. Total #@@count plays.
"
end
end
s1
=
Song
.
new()
s2
=
Song
.
new()
s1
.
play
s2
.
play
s1
.
play
s1
.
play
类方法:
class Song
@
@count
=
0
def Song
.
TotalCount
@
@count
end
end
s1
=
Song
.
new
puts Song
.
TotalCount
实现单例模式(Singleton)
class Song
private_class_method
:
new
@
@name
=
nil
def Song
.
create
@
@name
=
new
unless
@
@name
@
@name
end
end
song
=
Song
.
create
3.数组和散列表:
class Test
a
=
[
'
ant
'
,
'
bee
'
,
'
cat
'
,
'
dog
'
]
puts a[
0
]
b
=
%w
{ ant bee cat dog }
puts b[
2
]
c
=
{
'
a
'
=>
'
ant
'
,
'
b
'
=>
'
bee
'
,
'
c
'
=>
'
cat
'
,
'
d
'
=>
'
dog
'
}
puts c[
'
d
'
]
d
=
Hash
.
new
d[
'
a
'
]
=
'
ant
'
d[
'
b
'
]
=
'
bee
'
d[
'
c
'
]
=
'
cat
'
d[
'
d
'
]
=
'
dog
'
puts d.length
end
4.ruby中的变量不是对象,只是引用。
class Test
person1
=
"
Tim
"
person2
=
person1
person1[
0
]
=
'
J
'
puts person1
puts person2
end
一旦改变person1的值,person2也会跟随改变
但是下面的代码情况就不一样了。
class Test
person1
=
"
Tim
"
person2
=
person1
.
dup
person1[
0
]
=
'
J
'
puts person1
puts person2
end
我们还可以使用freeze方法冻结一个对象。下面的代码运行会报错。
class Test
person1
=
"
Tim
"
person2
=
person1
person1
.
freeze
person1[
0
]
=
'
J
'
puts person1
puts person2
end
5.ruby的方法调用作用域
本文介绍了Ruby语言中的几个关键特性,包括成员变量的读写属性、类变量与类方法的使用方式、数组和散列表的基本操作,以及Ruby中变量作为引用的行为特点。通过具体的代码示例展示了如何在Ruby中实现这些功能。

857

被折叠的 条评论
为什么被折叠?



