# #############################################TrueandFalse#Ruby中只有false和nil是FALSE的,其他情况下都是TRUE###############################################rubypredefinestheglobalsfalseandnil.#bothofthesevaluesaretreatedasbeingfalseinabooleancontext#allothervaluesaretreatedasbeingtrue.# unless nilputs " nilisfalse " end unless falseputs " falseisfalse " end if 0 puts " 0istrue " end # #############################################And,Or,Not,Defined?#and&&#and和&&是短路的。#or||#or和||是短路的。#!not############################################# exp1 = trueexp2 = false if exp1andexp2puts " exp2willeval! " end if exp2andexp1puts " exp1willnoteval! " end if exp1orexp2puts " exp2willnoteval! " end if exp2orexp1puts " exp1willeval! " end if ! exp2puts " exp2isfalse " end # #############################################defined?#一个对象或者变量是否定义。############################################# name = " mazhao " if defined ? nameputs " nameisdefined " end unless defined ? emailputs " emailisnotdefined " end # #############################################ComparisonOperators#==(!=),===,<=>,<,<=,>,>=,=~(!~)############################################# val1 = 1 val2 = 2 if val1 != val2puts " #{val1}!=val2 " end if " mary " === " mary " puts " maryismary " endputs " val1<=>val2:#{val1<=>val2} " if val1 < val2puts " #{val1}<#{val2} " end if ' LearnningRuby ' =~ / Ruby / puts " LearnningRubycontainsRuby! " end if ' LearnningRuby ' !~ / Java / puts " LearnningRubydoesn'tcontainJava " end # #############################################?expression#boolean_expression?exp1:exp2############################################# puts 1 < 2 ? " 1lessthan2 " : " 1largerorequalthan2 " # #############################################caseexpression#case#when############################################# factor = 90 casefactorwhen 0 .. 59 puts " F " when 60 .. 69 puts " D " when 70 .. 79 puts " C " when 80 .. 89 puts " B " when 90 .. 100 puts " A " end # #############################################LOOP#while#until#forin#each#loopdo###############################################while while line = getsbreak if line . eql ? ( " break " )puts " > " + lineend # until i = 0 until i > 10 putsii += 1 end # forin for iin 0 .. 10 putsiend # forin puts " iinanarray " a = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 ] for iinaputsiend # each a = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 ]a . each do | i | putsiend # time puts " timesloop " 10 . times do | i | putsiend # upto puts " uptotoloop " 0 . upto( 10 ) do | i | putsiend # loopdo puts " loopdo " j = 0 loop do putsjj += 1 break if j >= 10 end # #############################################break,redo,next,retry#break#redo#next#retry############################################# puts " testbreak " for iin[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 ]putsibreak if i == 5 endputs " testredo " for iin[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 ]putsi if i == 5 i = 6 redo endendputs " testretry " for iin[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 ] print " Nowat#{i},Restart? " retry if gets =~ /^ y / iendputs " testnext " for iin[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 ] next if i % 2 == 0 putsiend