• Ruby


    Ruby is a true object oriented programming language.
    Ruby is a server-side scripting language similar to Python and PERL.

    Ruby can be embedded into HTML.

    All ruby files will have extension .rb

    #!/usr/bin/ruby  -w
    print <<EOF
      This is the first way of creating
      here document ie. multiple line string.
    EOF
    
    print
    <<"EOF"; # same as above   This is the second way of creating   here document ie. multiple line string. EOF
    print
    << `EOC` # execute commands   echo hi there   echo lo there EOC
    print
    << "foo", <<"bar" # you can stack them I said foo. foo I said bar. bar

    << can special a string or an identifier to terminate the string literal, and all lines following the current line up to the terminator are the value of the string. Example:

    Begin {

    } #decalres code to be called before the program is run

    End {} #decalres code to be called after the program is run.

    Commit:

    single Line: #

    multi line: =begin    =end

    Class and object:

    class: class Customer end

    Instance variables: using @ prefix the variable name

    Class Variables: Class variables are available across different objects. A

    using @@ prefix with the clas name

    Global Variables: $

    Example:

    class Customer
        @@no_of_customers =0
    end

    Creating objects using new method

    cust1 = Customer. new
    cust2 = Customer. new

    def:

    define a method: followed by the name of the method, then a list of arguments.

    end with a "end"

    def load_comics( path )
      comics = {}
      File.foreach(path) do |line|
        name, url = line.split(': ')
        comics[name] = url.strip
      end
      comics
    end

    Example:

    class Customer
    @@no_of_customers =0
        def initialize(id, name, addr)
         @cust_id = id
         @cust_name = name
         @cust_addr = addr
        end
    end


    cust1=Customer .new("1","John","Wisdom Apartments,Ludhiya")


    Data:

    Integer:

      -2^30 to 2^(30-1); with Fixnum, Bignum

    Floating number: 123.4, 1.0E6, 4E20, 4e+20

    String:

    using '' or "". You can substitute the value of any Ruby expression into a string using the sequence #{expr}. expr could be any ruby expression.

    puts  "Multiplication Value : #{24*60*60}";
    Multiplication Value: 86400

    Arrays:

    using []:  ary = ["a", "This is a string", 3.14, 10, "last element"].

    ary.each do |i|

      puts i

    end

     Hash(dictionary)

    hsh = colors = {"red" => 0xfoo, "green" => 0x0f0}

    Ranges:

    Range represents an interval.a set of values with a start and an end.

    Ranges constructed uisng ".." run from the start to the end. (1..5) means 1,2,3,4,5

    Ranges constructed uisng "..." run from the start to the end excluding the end. (1...5) means 1,2,3,4.

    Some special variables:

      self: The receiver object of the current method.

      nil: value representing undefined

      _LINE_: The current line number in the source file/

      _FILE_: The current file name

    Operations:

    operator description example
    <=>

    COmbined comparsion operator. Returns 0 if first operand equals second, 1 if operand is greater than the second.

    -1 if first operand is less than the second.

    (a<=>b) returns -1
    === Uesd to test equality within a when clause of a case statement (1...10) === 5 returns true
    .eql? True if the receiver and argument have both the same type and equal values 1.eql?(1.0) is false
    ** Exponent 2**3 is 8
    equal? True if receiver and argument have the same object ID aObj == bObj
    ?: Conditional Expression  
    defined?    

    Parallel Assignment:

    a,b,c = 10,20,30

    Double Colon "::" unary operator tat allows: constants, instance methods and class methods defined within a class or module, to be accessed from anywhere outside the class or modules. 

    Conditional:

    if conditional 
            code...
    elsif conditional 
            code...
    else code
    end
    if x>2
        puts "x is greater than 2"
    elsif x<=2 and x!=0
        puts "x is 1"
    else
        puts  "I can't guess the number"
    end

    if:

    code if condtion
    
    $debig =1
    print "debug
    " if $debug
    
    #will print debug

    Unless:executes the code if conditional is false

    unless condition
        code
    else 
        code
    end
    
    #example
    
    x=1
    
    unless x>2
        puts "x is less than 2"
     else
        puts "x is greater than 2"
    end
    
    #result: x is less than 2

    Case statements:

    case expr0
    when expr1, expr2
        stmt1
    when expr3, expr4
        stmt2
    else
        stmt3
    end
    
    
    $age = 5
    case $age
    when 0..2
    puts "baby"
    when 3..6
    puts "little child"
    when 7..12
    puts "child"
    when 13..18
    puts "youth"
    else
    puts "adult"
    end
    
    
    #result is "little child"
  • 相关阅读:
    广义表的创建和遍历
    dev c++ Boost库的安装
    NAT模式
    vmware桥接模式
    smb与samba
    利用Linux的Samba服务模拟NT域
    使用samba进行共享文件操作步骤
    安装chrome
    使用虚拟机上网第二步
    TCP协议三次握手过程分析
  • 原文地址:https://www.cnblogs.com/renyualbert/p/6206236.html
Copyright © 2020-2023  润新知