Ruby入门小练习

下载安装ruby186-26.exe

URL http://rubyforge.org/frs/download.php/29263/ruby186-26.exe
安装完成后即可在程序菜单中找到一项SciTE

 

#filename:Hellworld.rb
#this is my first ruby programme

class Helloworld
  def sayGoodnight(name)
    return "hello,"+name
  end
  #问候方法
  def sayBye(name)
    return "Byebye,#{name}"
  end
  #去除return 关键字的方法
  def sayBye2(msg)
    "I want to say:#{msg}"
  end
 #puts Helloworld.new.sayGoodnight("zhaowm")+"!"
  #puts "hello,world!"
  a=1;
  b=2;
  #puts  "a+b=",a+b
  #三次输出Hello,zhaowm
  #3.times { puts "Hello,zhaowm!" }
  #puts "Good afternoon,\n Wenwen!"
  # and so on
  #song1 = Song.new("Ruby Tuesday")
  #song2 = Song.new("Enveloped in Python")


  #puts "gin joint".length  
  #puts "Rick".index("c")    
  #puts -1942.abs  
#puts sam.play(song1)  
  bb="1"
  cc="2"
  #puts bb+cc+"10"
  #puts Helloword.new.sayGoodnight "zhaowmbbbb"
  #puts Helloword.new.sayBye("wb")
  #puts Helloword.new.sayBye2("放手去爱");
  #puts 1%2
  def addMethod(a,b)
    return a+b
  end 
  #字符串连接符号=>
  #puts "12+13="=>Helloword.new.addMethod(12,13)
  a =[1, 'cat', 3.14 ]
  #puts "打印出数组中的某一个元素:"+a[1]
  ArrayOne=Array.new
  ArrayOne[0]="张三丰"
# puts ArrayOne
  
  instSection = {
  'cello'     => 'string\r',
  'clarinet'  => 'woodwind\r',
  'drum'      => 'percussion\r',
  'oboe'      => 'woodwind\r',
  'trumpet'   => 'brass\r',
  'violin'    => 'string\r'
}
#puts instSection
def verifyNumber(count)  
  if count > 10
    puts "Try again"
  elsif tries == 3
    puts "You lose"
  else
    puts "Enter a number"
  end
end
#puts Helloworld.new.verifyNumber(12)

def whileMethod(square)
  while square < 521
    square = square*square
  end
  return square
end
#invoke above method:whileMethod with one para:square
#puts Helloworld.new.whileMethod(7)

#得到多份指定参数的复制品
def callBlock
  yield
  yield
  yield
end

#Helloworld.new.callBlock { puts "I Clove you." }
a = %w( ant bee cat dog elk )    # create an array
#puts a
 


[ 'cat', 'dog', 'horse' ].each do |animal|
  if(animal=='horse')
    break
  end
  print "<name>"+animal+"</name>\r"  
end


def whileMeth(gets)   
    if gets=='Ruby'         
      print gets+"\r"
    end
end 
Helloworld.new.whileMeth('Ruby')



end 





 

 

 

你可能感兴趣的:(c,PHP,python,ant,Ruby)