Ruby-第一天
找:
Ruby API文档: http://www.ruby-doc.org/core-2.0/
Programming Ruby: http://www.ruby-doc.org/docs/ProgrammingRuby/
替换字符串方法:
1.单个替换
"hello ruby".sub('ruby','rb')
"hello ruby ruby".gsub('ruby','rb')
Ruby正则表达式资料:http://ruby-doc.org/core-2.0/Regexp.html
Ruby区间(range)资料:http://ruby-doc.org/core-2.0/Range.html
做:
打印字符串"Hello, world.":
puts "Hello, world."
在字符串"Hello, Ruby."中,找出"Ruby."所在下标:
x = "Hello, Ruby."
x.scan(/[Ruby.]/){|y| puts "'#{y}'在'#{x}'的下标为:#{x.index(y)}"}
打印你的名字十遍:
10.times do puts "jdys" end
打印字符串"This is sentence number 1.",其中的数字1会一直变化到10。
x = 1
while x <= 10
puts "This is sentence number #{x}"
x = x + 1
end
从文件运行Ruby程序:(文件放在F盘 名字叫Test.rb)
f:
ruby Test.rb
猜数字程序:
isRun = true
randNum = rand(10)
while isRun == true
puts "请输入要猜的数字"
y = gets.to_i
if y > randNum
puts "猜大了"
elseif y < randNum
puts "猜小了"
else
puts "猜中了"
isRun = false
end
end
| 著作权声明:本文由几度易水原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢! |

浙公网安备 33010602011771号