luodaoyi

Ruby3 流程控制
if/else/elsifa = 'hello' b = false if a p a elsif b ...
右键或长按保存图片
16
2018/02

Ruby3 流程控制

if/else/elsif

a = 'hello'
b = false

if a
    p a
elsif b
    p b
else
    p 'ok'
end

unless

unless相当于if的反向断言

unless false
    'ok'
end
# => 'ok'

if/unless

a = 1 if a != 1 #如果a不是1 则a复制为1
b = 2 unless defined?(b) #如果b未定义 那么就定义b赋值为2

case

case a
when 1
    1
when /hello/
    "hello world"
when Array
    []
else
    'ok'
end

while

a = 0
while a < 100 do
    p a
    a += 1
end

ruby 没有++和--操作符

Iterators

for x in [1, 2, 3] do
    p x
end

[1, 2, 2].each do |x|
    p x
end

10.times {|x| p x}
Last modification:February 17th, 2018 at 04:19 am
If you think my article is useful to you, please feel free to appreciate

Leave a Comment