Ruby语言 (一)

#=> Hello World      

    标准版:    print "hello world!"

    太长了:    puts "hello world!"

    再短一点:    p "hello world!"

#=> Comment

    # say hello

    =begin
      this is a long comment
    =end

#=> variables

    local: time or _time

    instance: @time

    class: @@time

    global $time

#=> data types

    Numeric

    String

    Symbol

    Boolean

    Array

    Hash

#=> variables tricks

    "hello #{name}"

    a,b = b,a

    3.times{ puts "hello"}

    "hello" * 3


#=> condition if

    质樸的if:
	
    if(a>5)
      puts a
    end

    一行版:

    if a > 5 then puts a end

    语义不够顺畅:    puts a if a > 5

#=> condition unless

    与if语义相反的unless

    puts "miss it" if !name

    puts "miss it" unless name

    三元不能少:    a > 5 ? puts(a) : "oh no"

#=> condition if else

    if elsif else:

    if name == "jack"
      "i am rose"
    elsif name == "rose"
      "jack i miss u"
    else
      "get out from here"
    end
   
    这个肯定是switch的场景啊:

    case name
    when "jack" then "i am rose"
    when "rose" then "jack i miss u"
    else "get out from here"
    end    
	
#=> loop

    循环怎么写:    3.times{ puts "hello world" }

    for:    
	
	for x in [1,2,3]
      puts x
    end

    while:

    while i > 5 do
      i -= 1
    end
    i -= 1 while i > 5		

    while的好兄弟until:

    until i <= 5 do
      i -= 1
    end
    i -= 1 until i<= 5		

    while true太不洋气了:

    loop do
      puts "我自豪"
    end		

    打断罪恶的连锁:

    break

    next

    redo

    retry

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值