Ruby 常用表达式

#############################################
# 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

if 0
    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"

if defined? name
    puts 
"name is defined"
end

unless defined? 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
= 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"
= [1,2,3,4,5,6,7,8,9,0]
for i in a
    puts i
end


# each
= [1,2,3,4,5,6,7,8,9,0]
a
.each do |i|
    puts i
end


# time
puts "times loop"
10.times do |i|
    puts i
end


# upto 
puts "upto to loop"
0.upto(10do |i|
    puts i
end


# loop do 
puts "loop do"
= 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]
    
next if i % 2 == 0
    puts i
end


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值