Ruby中的字符串处理非常灵活
"abc"[0] 返回97 就是a
"abc"[1] 98
"abc"[2] 99
"abc"[3] nil
"abc"[-1] 99 从后向前数
"abc"[-2] 98
"abc"[-3] 97
"abc"[-4] nil
"abc" * 2 返回"abcabc" 支持乘法运算
"abc" + "def" 返回"abcdef"
"abcdef"[0,1] "a" 从位置0开始抽取1位
"abcdef"[1,3] "bcd"
"abcdef"[-3,3] "def"
"abcdef"[1..3] "bc" 从1到3
来一个猜谜语的程序
"abc"[0] 返回97 就是a
"abc"[1] 98
"abc"[2] 99
"abc"[3] nil
"abc"[-1] 99 从后向前数
"abc"[-2] 98
"abc"[-3] 97
"abc"[-4] nil
"abc" * 2 返回"abcabc" 支持乘法运算
"abc" + "def" 返回"abcdef"
"abcdef"[0,1] "a" 从位置0开始抽取1位
"abcdef"[1,3] "bcd"
"abcdef"[-3,3] "def"
"abcdef"[1..3] "bc" 从1到3
来一个猜谜语的程序
words = ["hallo","world","lee"]
puts "guess word by \"#{words.join(",")}\""
scret = words[rand(3)]
while guess = gets
guess.chop!
if guess == scret
puts "you win"
break
else
puts "you loss,guess?"
end
end
puts "the words is #{scret}"
puts "guess word by \"#{words.join(",")}\""
scret = words[rand(3)]
while guess = gets
guess.chop!
if guess == scret
puts "you win"
break
else
puts "you loss,guess?"
end
end
puts "the words is #{scret}"