logo

Navigation:First_Community->Software->Script(Perl,Python,Ruby,etc.) Goto:New TopicSettingSearch
print不自动换行,puts会自动换行第1
Posted by: keven Date: April 4, 2008 05:38PM
1.print不自动换行,puts会自动换行,一行不够写的话,可以加 " "进行连接

MegaEntry - Social networking and discussion site!

ruby 代码
puts 6/2 print 6/1 puts 'hello world ' puts '我们都是 ' '中国人 '
运行结果: ruby 代码
3 6hello world 我们都是中国人
2.==,eql?,equal? 区别 ==值相等 eql?值相等,类型相等 equal?值相等,内存地址相等
a=1 b=1.0 c=1.0 d=1.0 e=c puts(a==b)#值相等 puts(a.eql?(b)) #值相等,类型相等 puts(c.equal?(d))#值相等,内存地址相等 puts(c.equal?(e))
运行结果:

CopyRight owned by the original author.--(www.MegaEntry.com)

ruby 代码
true false false true
3. #<=>比较2个对象的大小,大于,等于,小于分别返回1,0,-1 #===右边对象是否在左边区域里,返回true或false ruby 代码
puts( "abd " <=> "acd ") puts((0..5) === 10) puts((0..5) === 3.2)

MegaEntry - Social networking and discussion site!

运行结果:
-1 false true
4.case 分支条件语句
x=3 case x when 1..2 print "x= ",x, ",在1..2中 " when 4..9,0 print "x= ",x, ",在4..9,0中 " else print "x= ",x, ",其它可能 " end 输出结果: x=3,其它可能
5.while与until 注:

CopyRight owned by the original author.--(www.MegaEntry.com)

A:ruby里的字符串连接用的是逗号(java里用的是加号),如: puts( "aaa ", "bbb ") print( "aaa ", "bbb ") 分别输出为aaa换行bbb,aaabbb B:ruby里好像没有++,所以a++是错误的,只能a=a+1

MegaEntry - Social networking and discussion site!

ruby 代码
a=1 while( a < 10 ) print(a, " ") a=a+1 end b=1 until( b >= 10 ) print(b, " ") b=b+1 end 输出结果为: 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9
6. 几个函数说明 print "aa " if i < 2 等价于 if i < 2 print "aa "

MegaEntry - Social networking and discussion site!

end ruby 代码
3.times{print "hi "} 1.upto(9){|i| print i if i<7>#|i|可以看成是一个临时的引用变量,供后面使用 9.downto(1){|i| print i if i<7> (1..9).each{|i| print i if i<7> 0.step(11,3){|i| print i} 运行结果: hihihi123456654321123456036


Reply To This Message
Subject: 

IE 6.0 or above is perfect