zoukankan      html  css  js  c++  java
  • ruby的书写

    可以用{}书写一个block,如  {puts "hello"}

    也可以不用{},直接写为puts "hello"

    或用

    begin

      puts "hello"

    end

    在每一行可以加;

    puts "hello";

    puts "hey";

    或不加;

    puts "hello"

    puts "hey"

    或在do..end中定义

    do

      club.enroll(person)

      person.socialize

    end

    另外

    if radiation>3000

      puts "Danger"

    end

    可写为

    puts "Danger" if radiation>3000

    square=2

    while square<1000

      square=square*square

    end

    等同与

    square=2

    square=square*square while square<1000

    类似perl,ruby运行并行赋值

    a,b=b,a

    表示将a与b互换

    x=0

    a,b,c=x,(x+=1),(x+=1) 得到 [0,1,2]

    case语句

    rating=case votes_cast

        when 0..10   then Rating::SkipThisOne

        when 10..50  then Rating::CouldDoBetter

        else

        end

    +号的重载

    class Fixnum

      alias old_plus +

      def +(other)

        old_plus(other).succ

      end

    end

    1+2 得到 4

    a=3

    a+=4  得到  8

    a+a+a  得到 26

    注: alias为取别名

    if 和unless

    if和unless互为相反的符号

    如if a>2 then puts "s" 可写为 unless a<=2 then puts "s"

    if a="ddd" then puts "1" puts "2"

    else puts "3"

    end

    可加上then,写为

    if a="ddd" then

      puts "1"

    elseif a="ttt" then

      puts "2"

    else

      puts "3"

    end

  • 相关阅读:
    【python ----集合练习】
    【python ---字典练习】索引 增删改 嵌套
    词频统计
    枚举
    【python--集合】增删改 交集 差集 并集 反交集 子集和超集
    迭代器和迭代对象 生成器 推导式
    tuple 元组
    【python--字典】 字典的拆包
    Python -- 函数
    Python -- 文件的copy以及读写
  • 原文地址:https://www.cnblogs.com/djcsch2001/p/2418506.html
Copyright © 2011-2022 走看看