ruby基础1

替换字符串的某一部分,'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

 

posted on 2013-09-02 23:41  emlie  阅读(215)  评论(0)    收藏  举报