关键要点
我们代码示例中使用的 Ruby 核心语法简要总结。
输出文本
puts "a" + "b"; # => "ab" puts 1 + 2; # => 3 puts "1" + "2"; # => "12"
输出时不换行
print "Hi" print "Bob"
屏幕输出为 "HiBob"
输出多个字符串连接
print "Bye", " John" # => "Bye John"
计算总和与平均值
total = 1 + 2 + 3; # => total 的值为 6 average = total / 3; puts average; # => 2
在字符串中插入变量
puts "total is " + total.to_s puts "average is #{average}"
获取用户输入并转换为整数
input = gets.chomp.to_i
处理负分情况
score = -70
if score < 0 puts score = 0 # 不能有负分 end
使用 unless 处理负分情况
score = -70
unless score >= 0 puts score = 0 # 不能有负分 end
使用 unless 和 if 的条件提示
user_count = 7
puts "Warning: limit exceeded" unless user_count < 10 puts "Warning: close to limit" if user_count <= 10
if-else 条件判断
if true puts "OK" else puts "Never reach here" end
a = 10 b = 20
if a > b puts "a is bigger" elsif a < b puts "b is bigger" else puts "a,b the same" end
使用 case 进行多条件判断
score = 70
result = case score
when 0..60
"Fail"
when 61..70
"Pass"
when 71..95
"Pass with Distinction"
when 96, 97, 98, 99
"Distinction"
when 100
"High Distinction"
else
"Invalid Score"
end
puts result # => Pass
while 循环
count = 0 while count < 10 count = count + 1 print count, " " end
1 2 3 4 5 6 7 8 9 10
for 循环
for i in 1..9 print i, " " end
1 2 3 4 5 6 7 8 9
until 循环
count = 1 until count >= 5 print count, " " count +=1 end
1 2 3 4
重复执行三次计算
number = 2 3.times do number = number * number end
3 次计算:2 * 2, 4 * 4, 16 * 16
puts number # => 256
遍历数字并跳过奇数
(1..5).each do |num| puts "Checking #{num} ..." next if num % 2 != 0 # 跳过奇数 puts "#{num} is an even number." end
使用 break 退出 while 循环
在 Ruby 中定义无限循环
while true
里面的代码会不断重复执行
必须添加退出条件
end
乘法表
for i in 1..9 do for j in 1..9 do puts "#{i} x #{j} = #{i * j}" end end
数组操作
score_list = [20, 10, 30, 50] puts score_list.inspect # => [20, 10, 30, 50] puts score_list.size # => 4 p score_list.reverse # => [50, 30, 10, 20] p score_list.sort # => [10, 20, 30, 50],原数组未改变 p score_list # => [20, 10, 30, 50] p score_list.sort! # 修改原数组 p score_list # => [10, 20, 30, 50]
数组索引与查找
most_spoken_langs = ["Chinese", "Spanish", "English", "Hindi", "Arabic"] puts most_spoken_langs[0] # => "Chinese",索引从 0 开始 puts most_spoken_langs[2] # => "English" puts most_spoken_langs[-1] # => "Arabic",最后一个元素 puts most_spoken_langs[-2] # => "Hindi" puts most_spoken_langs.include?("Spanish") # => true puts most_spoken_langs.include?("Italian") # => false
修改数组
score_list = [20, 10, 30, 50] score_list.delete(30) # score_list 变为 [20, 10, 50] score_list << 90 score_list << 80 print score_list # => [20, 10, 50, 90, 80]
遍历数组并输出平方值
[20, 10, 30].each do |elem| puts "Square of #{elem} is #{elem * elem}" end
哈希操作
us_states = {"CA" => "California", "NY" => "New York", "TX" => "Texas"} us_states["FL"] = "Florida" us_states.size # => 4 us_states.delete("NY") puts us_states.inspect
遍历哈希并输出键值对
us_states = {"CA" => "California", "NY" => "New York", "TX" => "Texas"}
us_states.each { |key, value| puts "#{value}'s abbreviation is " + key }
定义函数
def square(num) num * num # => 等同于 return num * num end
puts square(2) # => 4 puts square(9) # => 81
带默认参数的函数
def exponentiate(num, power = 2) # power 的默认值为 2 num ** power # 指数运算 end
puts exponentiate(5) # => 25 puts exponentiate(5, 3) # => 125
变量作用域示例
@c = 1 # @实例变量,作用域为文件级别
def add(a, b) c = a + b # 函数内部局部变量 @c = c # 设置实例变量的值 puts "inside c = #{c}" end
c = 100 # 外部局部变量,与 add() 中的 c 不同 add(1, 2) add(2,3)
puts "Value of c declared outside the method = #{c}" puts "Value of @c set inside the method = #{@c}"