############################################# # True and False # Ruby中只有false和nil是FALSE的,其他情况下都是TRUE ############################################# # # ruby predefines the globals false and nil. # both of these values are treated as being false in a boolean context # all other values are treated as being true. # unless nil puts "nil is false" end unless false puts "false is false" end if0 puts "0 is true" end ############################################# # And, Or, Not, Defined? # and && # and和&&是短路的。 # or || # or和||是短路的。 # ! not ############################################# exp1 = true exp2 = false if exp1 and exp2 puts "exp2 will eval!" end if exp2 and exp1 puts "exp1 will not eval!" end if exp1 or exp2 puts "exp2 will not eval!" end if exp2 or exp1 puts "exp1 will eval!" end if!exp2 puts "exp2 is false" end ############################################# # defined? # 一个对象或者变量是否定义。 ############################################# name ="mazhao" ifdefined? name puts "name is defined" end unlessdefined? email puts "email is not defined" end ############################################# # Comparison Operators # ==(!=), ===, <=>, <, <=, >, >=, =~(!~) ############################################# val1 =1 val2 =2 if val1 != val2 puts "#{val1} != val2" end if"mary"==="mary" puts "mary is mary" end puts "val1 <=> val2 : #{val1 <=> val2}" if val1 < val2 puts "#{val1} < #{val2}" end if'Learnning Ruby'=~/Ruby/ puts "Learnning Ruby contains Ruby!" end if'Learnning Ruby'!~/Java/ puts "Learnning Ruby doesn't contain Java" end ############################################# # ? expression # boolean_expression ? exp1 : exp2 ############################################# puts 1<2?"1 less than 2":"1 larger or equal than 2" ############################################# # case expression # case # when ############################################# factor =90 case factor 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 # for in # each # loop do # ############################################# # while while line = gets break if line.eql?("break ") puts ">"+line end # until i =0 until i >10 puts i i +=1 end # for in for i in 0..10 puts i end # for in puts "i in an array" a = [1,2,3,4,5,6,7,8,9,0] for i in a puts i end # each a = [1,2,3,4,5,6,7,8,9,0] a.eachdo|i| puts i end # time puts "times loop" 10.timesdo|i| puts i end # upto puts "upto to loop" 0.upto(10) do|i| puts i end # loop do puts "loop do" j =0 loop do puts j j +=1 break if j >=10 end ############################################# # break, redo, next, retry # break # redo # next # retry ############################################# puts "test break" for i in [1,2,3,4,5,6,7,8,9,0] puts i break if i ==5 end puts "test redo" for i in [1,2,3,4,5,6,7,8,9,0] puts i if i ==5 i =6 redo end end puts "test retry" for i in [1,2,3,4,5,6,7,8,9,0] print"Now at #{i}, Restart?" retry if gets =~/^y/i end puts "test next" for i in [1,2,3,4,5,6,7,8,9,0] nextif i %2==0 puts i end
############################################## True and False# Ruby中只有false和nil是FALSE的,其他情况下都是TRUE############################################### ruby predefines the globals false and nil.# both