ruby基础1

it2022-05-05  183

替换字符串的某一部分,'world'部分亦可以用正则表达式表示

"Hello world".sub('world','ruby')

打印字符串"Hello, World."

puts("Hello, Wolrd.")

在字符串"Hello, Ruby."中,找出"Ruby."所在的下标

"Hello, Ruby.".index('Ruby.')

打印你的名字十遍

i=0 while i<10 i = i +1 puts("my name") end

打印字符串"This is sentence number 1.",其中的数字1会一直变化到10

i=0 while i<10 i = i +1 puts("This is sentence numbber "+i.to_s+'.') end

让玩家猜随机数,文件保存问.rb

puts("please enter an number:") y = rand(10) x = gets() while true if x.to_i >y puts("bigger\n") end if x.to_i<y puts("small\n") else puts("all rights\n") exit end puts("please enter an number:") x = gets() end

 

转载于:https://www.cnblogs.com/reita/p/3297867.html

相关资源:Ruby基础教程(第4版)

最新回复(0)