forked from gialib/vimfile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.rb
121 lines (100 loc) · 1.74 KB
/
test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
$gname="Paul"
NAME="Bill"
class Customer
@@no_of_customers=0
NUM=2
def num()
puts "23"
end
def initialize(id, name, addr)
@cust_id=id
@cust_name=name
@cust_addr=addr
end
def display_details()
puts "Customer id #@cust_id"
puts "Customer name #@cust_name"
puts "Customer address #@cust_addr"
puts $gname
end
def total_no_of_customers()
@@no_of_customers += 1
puts "Total number of customers: #@@no_of_customers"
end
end
puts Customer::NUM
# 创建对象
cust1=Customer.new("1", "黎明", "香港")
cust2=Customer.new("2", "吴京", "大陆")
# 调用方法
$gname="Gates"
cust1.display_details()
cust1.total_no_of_customers()
cust2.display_details()
cust2.total_no_of_customers()
puts $gname
puts NAME
#puts defined?NAME
puts defined?$gname
puts defined?Customer
$age=40
case $age
when 0..2
puts "婴儿"
when 3..12
puts "小孩"
when 13..18
puts "少年"
when 19..44
puts "青年"
when 45..59
puts "中年"
else
puts "老年"
end
def test
yield 1,2,3
end
#block,必须要放在方法之后
test{|a,b,c| puts a+b+c}
module Hani
AGE=22
def Hani.happy(name)
puts name+" is so happy"
end
def happy(name)
puts name+" is so happy"
end
end
class Huni
include Hani
@@sex="male"
attr_accessor :name,:age
def Huni.go
puts Hani::AGE
puts Hani.happy "ja"
end
end
puts Huni.go
huni=Huni.new
huni2=huni
puts huni.happy ("Mall")
puts Huni::AGE
animals=%w{cat dog sheep cow}
animals.each{|animal| puts animal}
$x=gets
if $x.to_i>3
puts "#{$x}\大于3"
elsif $x.to_i<0
puts "#{$x}\小于0"
else
puts "0<=#{$x}\<=3"
end
huni.name="Huni"
huni.age=22
puts huni.name+"今年#{huni.age}岁了"
puts huni.name
huni.freeze
huni2.name="huni2"
huni2.age=32
puts huni2.name