六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 39|回复: 0

Ruby

[复制链接]

升级  7.67%

65

主题

65

主题

65

主题

举人

Rank: 3Rank: 3

积分
223
 楼主| 发表于 2013-1-27 05:18:35 | 显示全部楼层 |阅读模式
#filename:hellworld.rb#this is my first ruby programmeclass 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 instSectiondef verifyNumber(count)    if count > 10    puts "Try again"  elsif tries == 3    puts "You lose"  else    puts "Enter a number"  endend#puts Helloworld.new.verifyNumber(12)def whileMethod(square)  while square < 521    square = square*square  end  return squareend#invoke above method:whileMethod with one para:square#puts Helloworld.new.whileMethod(7)#得到多份指定参数的复制品def callBlock  yield  yield  yieldend#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"  enddef whileMeth(gets)       if gets=='Ruby'               print gets+"\r"    endend Helloworld.new.whileMeth('Ruby')end  
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表