#
############################################
#TrueandFalse
#Ruby中只有false和nil是FALSE的,其他情况下都是TRUE
#############################################
#
#rubypredefinestheglobalsfalseandnil.
#bothofthesevaluesaretreatedasbeingfalseinabooleancontext
#allothervaluesaretreatedasbeingtrue.
#
unless nil
puts " nilisfalse "
end
unless false
puts " falseisfalse "
end
if 0
puts " 0istrue "
end
# ############################################
#And,Or,Not,Defined?
#and&&
#and和&&是短路的。
#or||
#or和||是短路的。
#!not
#############################################
exp1 = true
exp2 = false
if exp1andexp2
puts " exp2willeval! "
end
if exp2andexp1
puts " exp1willnoteval! "
end
if exp1orexp2
puts " exp2willnoteval! "
end
if exp2orexp1
puts " exp1willeval! "
end
if ! exp2
puts " exp2isfalse "
end
# ############################################
#defined?
#一个对象或者变量是否定义。
#############################################
name = " mazhao "
if defined ? name
puts " nameisdefined "
end
unless defined ? email
puts " emailisnotdefined "
end
# ############################################
#ComparisonOperators
#==(!=),===,<=>,<,<=,>,>=,=~(!~)
#############################################
val1 = 1
val2 = 2
if val1 != val2
puts " #{val1}!=val2 "
end
if " mary " === " mary "
puts " maryismary "
end
puts " val1<=>val2:#{val1<=>val2} "
if val1 < val2
puts " #{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
casefactor
when 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 = gets
break if line . eql ? ( " break " )
puts " > " + line
end
# until
i = 0
until i > 10
putsi
i += 1
end
# forin
for iin 0 .. 10
putsi
end
# forin
puts " iinanarray "
a = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 ]
for iina
putsi
end
# each
a = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 ]
a . each do | i |
putsi
end
# time
puts " timesloop "
10 . times do | i |
putsi
end
# upto
puts " uptotoloop "
0 . upto( 10 ) do | i |
putsi
end
# loopdo
puts " loopdo "
j = 0
loop do
putsj
j += 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 ]
putsi
break if i == 5
end
puts " testredo "
for iin[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 ]
putsi
if i == 5
i = 6
redo
end
end
puts " testretry "
for iin[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 ]
print " Nowat#{i},Restart? "
retry if gets =~ /^ y / i
end
puts " testnext "
for iin[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 ]
next if i % 2 == 0
putsi
end
#TrueandFalse
#Ruby中只有false和nil是FALSE的,其他情况下都是TRUE
#############################################
#
#rubypredefinestheglobalsfalseandnil.
#bothofthesevaluesaretreatedasbeingfalseinabooleancontext
#allothervaluesaretreatedasbeingtrue.
#
unless nil
puts " nilisfalse "
end
unless false
puts " falseisfalse "
end
if 0
puts " 0istrue "
end
# ############################################
#And,Or,Not,Defined?
#and&&
#and和&&是短路的。
#or||
#or和||是短路的。
#!not
#############################################
exp1 = true
exp2 = false
if exp1andexp2
puts " exp2willeval! "
end
if exp2andexp1
puts " exp1willnoteval! "
end
if exp1orexp2
puts " exp2willnoteval! "
end
if exp2orexp1
puts " exp1willeval! "
end
if ! exp2
puts " exp2isfalse "
end
# ############################################
#defined?
#一个对象或者变量是否定义。
#############################################
name = " mazhao "
if defined ? name
puts " nameisdefined "
end
unless defined ? email
puts " emailisnotdefined "
end
# ############################################
#ComparisonOperators
#==(!=),===,<=>,<,<=,>,>=,=~(!~)
#############################################
val1 = 1
val2 = 2
if val1 != val2
puts " #{val1}!=val2 "
end
if " mary " === " mary "
puts " maryismary "
end
puts " val1<=>val2:#{val1<=>val2} "
if val1 < val2
puts " #{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
casefactor
when 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 = gets
break if line . eql ? ( " break " )
puts " > " + line
end
# until
i = 0
until i > 10
putsi
i += 1
end
# forin
for iin 0 .. 10
putsi
end
# forin
puts " iinanarray "
a = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 ]
for iina
putsi
end
# each
a = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 ]
a . each do | i |
putsi
end
# time
puts " timesloop "
10 . times do | i |
putsi
end
# upto
puts " uptotoloop "
0 . upto( 10 ) do | i |
putsi
end
# loopdo
puts " loopdo "
j = 0
loop do
putsj
j += 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 ]
putsi
break if i == 5
end
puts " testredo "
for iin[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 ]
putsi
if i == 5
i = 6
redo
end
end
puts " testretry "
for iin[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 ]
print " Nowat#{i},Restart? "
retry if gets =~ /^ y / i
end
puts " testnext "
for iin[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 ]
next if i % 2 == 0
putsi
end