• ruby学习笔记(5)


    Array 类

    1)新建数组。 ruby中新建数组有多种方法。

        <1> 使用[],类似于js。

    ================================

    a = [1,2,3]

    p a         #=> [1,2,3]

    ================================

          <2> 使用Array.new。

    ================================

    a = Array.new

    p a        #=> []

    a = Array.new(5)

    p a        #=> [nil,nil,nil,nil,nil]

    a = Array.new(5,0)

    p a        #=> [0,0,0,0,0]

    ================================

       <3> 使用%w。 %w方法很奇怪,后面接的可以直接是字符串,而不必带"号。通常情况下,可以选用()做为参数的边界符,如果字符串本身包含()这样的字符,为免造成影响,还可以使用<> || !! @@ AA这样的来做边界符。%w方法会以空格为分隔,将字符拆分成数组。

    ================================

    a = %w(abc 123 def 456 ghi)

    p a   #=> ["abc","123","def","456","ghi"]

    a = %w<(abc) 1(23 g()h>

    p a #=> ["(abc)",“1(23”,"g()h"]

    ================================

        <4>其它对象的to_a方法。 比如说hash对象就可以通过to_a转换成数组。

    ================================

    h = {"name" => "adang","sex" => "male"}

    a = h.to_a

    p a           #=>[["name","adang"],["sex","male"]]

    ================================

            <5>字符串的split方法。

    ================================

    s = "ab-123-cd-45"

    a = s.split("-")

    p a         #=>["ab","123","cd","45"]

    ================================

    2) 数组的索引。

         <1> a[n]

          这个用法和别的语言没啥区别,唯一奇怪的是n可以为负值,表示倒数。另外,a.slice(n)和a.at(n)效果和a[n]相同。

    ================================

    a = ["a","b","c","d"]

    p a[0]   #=> a

    p a[-2] #=> c

    p a.slice(-2) #=> c

    p a.at(-2) #=> c

    ================================

        <2> a[n..m]

         会将a[n]到a[m]之间的范围建立出新的数组返回。a.slice(n..m)和a[n..m]效果相同。

    ================================

    a = ["a","b","c","d"]

    p a[1..3]                    #=> ["b","c","d"]

    p a[1..6]                    #=> ["b","c","d"]

    p a.slice(1..3)            #=> ["b","c","d"]

    p a[a]                        #=> ["a","b","c","d"]

    ================================

           <3> a[n,len]

           从a[n]处开始获取len个元素,建立新数组并返回。a.slice(n,len)和a[n,len]效果相同。

    ================================

    a = ["a","b","c","d"]

    p a[1,2]                  #=>["b","c"]

    p a[1,6]                   #=>["b","c","d"]

    p a.slice(1,2)          #=>["b","c"]

    p a                         #=>["a","b","c","d"]

    ================================

             <4> a.values_at(n1,n2,...)

             a[n..m],a[n,len]方法新建的数组都只能是连续位置上元素,a.values_at方法可以返回不连续的索引组成的新数组。

    ================================

    a = ["a","b","c","d"]

    p a.values_at(1,3,0)        #=> ["b","d","a"]

    p a                                   #=> ["a","b","c",“d”]

    ================================    

    3) 改写数组。a[n],a[n..m],a[n,len]不仅可以读数据,还能写数据。注a.at,at.slice方法都只能读,不能写,只有a[]可以写。

    ================================

    a = ["a","b","c","d","e","f","g"]


    a[1..3] = ["B","C","D"]


    a[4,2] = [1,2]


    p a                  #=> ["a","B","C","D",1,2,"g"]

    ================================

    4) 插入元素。 ruby中插入数据原理和js里一样。js中是通过splice方法先删几个元素,再添几个元素,ruby中也是如此。

    ================================

    a = ["a","b","c"]

    a[2,0] = [1,2]

    p a    #=> ["a","b",1,2,"c"]

    a = ["a","b",“c”]

    a[1,1] = [1,2,3]

    p a     #=> ["a",1,2,3,"c"]

    a = ["a","b","c"]

    a[1..2] = [1,2,3]

    p a     #=> ["a",1,2,3]

    ================================

    5) 交集、并集和差集。   ruby中数组还可以进行交集和并集的计算,分别使用 & 和 | 作为运算符。

    ================================

    a = ["a","b","c","d"]

    b = ["b","e","a"]

    p a & b   #=> ["a","b"]

    p a | b     #=> ["a","b","c",“d”,"e"]

    p a         #=> ["a","b","c","d"]

    ================================

    对数组直接使用 + - 运算符,可以分别得到数组连接的效果和差集的效果。

    ================================

    a = ["a","b","c","d"]

    b = ["b","e","a"]

    p a - b    #=> ["c","d"]                                 (a有,但b没有的元素)

    p   a + b   #=> ["a","b","c","d","b","e","a"]        (a和b简单地叠加)

    p   a | b     #=> ["a","b","c","d","e"]                   (a和b的并集,重复元素去除)

    ================================

    6)数组的队列性。 和js一样,ruby中的数据也有push,pop,unshift,shift方法,此外a << 赞同于a.push。

    ================================

    a = ["a","b","c"]

    a.unshift(1)

    a << 2

    a.push(3)

    p a                         #=> [1,"a","b","c",2,3]

    p a.shift                 #=> 1

    p a                         #=> [“a”,"b","c",2,3]

    p a.pop                 #=> 3

    p a                         #=> ["a","b","c",2]                

    ================================

    7) a.concat和 + 。    在js中数组是不能直接相加的,如果需要组合两个数组,是用a.concat(b)来实现的,但js中a.concat(b)是会返回一个新数组的,还可以利用这一点轻松复制一个复杂类型的数组。但ruby中concat是具有破坏性的,会影响到a。如果想不影响到a,可以使用+号。

    ================================

    a = ["a","b","c"]

    b = [1,2,3]

    c = a + b

    p c                                   #=>["a","b","c",1,2,3]

    p a                                  #=>["a","b","c"]

    c = a.concat(b)

    p c                                    #=>["a","b","c",1,2,3]

    p a                                   #=>["a","b","c",1,2,3]

    ================================

    8)a.compact。 a.compact可以从数组中删除nil元素。有compcat和compact!之分。

    ================================

    a = [nil,"a",nil,"b","c",nil]

    a.compact!

    p a            #=> ["a","b","c"]

    ================================

    9)a.uniq。 a.uniq可以删除数组中的重复元素。有a.uniq和a.uniq!之分。

    ================================

    a = [1,2,3,4,3,2,1]

    a.uniq!

    p a           #=> [1,2,3,4]

    ================================

    10) a.delete(x)、a.delete_at(n)、a.delete_if{|item| ...}

    a.delete(x)可以直接从数组中删除指定元素(为啥js中没有这么好用的方法?T_T )。

    a.delete_at(n)删除指定索引的元素(js的splice方法去死吧 T_T)。

    a.delete_if{|item| ...} 遍历数组元素,如果区块返回结果为真,则删除。 (for循环去死吧 T_T)。

    ================================

    a = [1,2,3,2,4,nil]

    a.delete(2)

    p a             #=> [1,3,4,nil]

    a = [1,2,3,2,4,nil]

    a.delete_at(2)

    p a             #=> [1,2,2,4,nil]

    a = [1,2,3,2,4,nil]

    a.delete_if{|item| item > 2 if item}

    p a             #=> [1,2,2,nil]

    ================================

    11) a.slice!()。 前面说过a.slice的效果等同于a[],包括a.slice(n),a.slice(n..m),a.slice(n,len)。slice方法还有slice!的形式,slice!方法的返回值相同,但会从数组中删除掉返回值。

    ================================

    a = [1,2,3,4,5,6]

    p a.slice!(1)       #=> 2

    p a                     #=> [1,3,4,5,6]

    p a.slice!(2..3)    #=> [4,5]

    p a                    #=> [1,3,6]

    p a.slice!(1,1)     #=> [3]

    p a                      #=> [1,6]   

    ================================

    12) a.collect和a.map。 如果需要让数组中所有元素统一变化,再也不用for()去循环,再赋值这么麻烦了,a.collect和a.map都可以返回让数组中的值进行处理之后,将返回的结果组成新数组。a.collect和a.map效果相同,且都有!的形式。

    ================================

    a = [1,2,3]

    a.collect!{|item| item * 2}

    p a              #=> [2,4,6]

    a.map!{|item| item * 3}

    p a              #=> [6,12,18]

    ================================

    13) a.fill。 a.fill可以将数组a的元素改写为指定的参数。但我a[n..m]=和a[n,len]=不同的是,a.fill只能将指定范围内的元素修改成一种参数(只有一种)。a.fill有a.fill(value),a.fill(value,begin),a.fill(value,begin,end),a.fill(value,n..m)几种不同的方式。

    ================================

    a = [1,2,3,4,5,6]

    a.fill(8)

    p a         #=> [8,8,8,8,8,8]

    a = [1,2,3,4,5,6]

    a.fill(8,3)

    p a         #=> [1,2,3,8,8,8]

    a = [1,2,3,4,5,6]

    a.fill(8,3,2)

    p a         #=> [1,2,3,8,8,6]

    a = [1,2,3,4,5,6]

    a.fill(8,2..4)

    p a         #=> [1,2,8,8,8,6]

    ================================

    14)a.flatten。 将数组扁平化,多层数组可以变成一个单层的数组。有a.flatten和a.flatten!之分.

    ================================

    a = [1,[2,3],[[4,5],6,[7,[8,9]]]]

    a.flatten!

    p a          #=> [1,2,3,4,5,6,7,8,9]

    ================================

    15)a.reverse。 逆转数组,有a.reverse和a.reverse!之分。

    ================================

    a = [1,2,3]

    a.reverse!

    p a      #=> [3,2,1]

    ================================

    16)sort和sort_by。 sort对数组排序,对应的还有sort!方法。另外ruby还提供sort_by方法,sort_by返回新数组,没有对应的sort_by!

    =================================

    a = [2,4,3,5,1]

    a.sort!

    p a                  #=> [1,2,3,4,5]

    b = a.sort_by{|i| -i}

    p b                   #=> [5,4,3,2,1]

    p a                  #=> [1,2,3,4,5]

    =================================

    17) each和each_width_index。 数组遍历最常用的做法是使用each方法,但each方法只会返回一个返回值,只有元素,没有索引,如果需要索引的话,可以使用each_with_index方法。

    =================================

    a = [1,2,3]
    a.each{|i| print i,"\n"}
    a.each_with_index{|i,n| print n," : ",i,"\n"}

    =================================

  • 相关阅读:
    收到Google Wave要求,想要的留下信箱。
    [集成IronPython] 添加方法到运行环境
    [集成IronPython] 集成IronPython系列
    智能播放器,也许有商业价值。
    [集成IronPython] 使CLR对象对动态语言更友好(二)—— 支持切片
    [集成IronPython] 使CLR对象对动态语言更友好(一)—— 支持运行时添加删除属性
    2012新年愿望
    使用SvrAny和InstSvr将应用程序作为Window服务启动
    谁来代替博客园 —— C社区
    谁来代替博客园——寄生博客
  • 原文地址:https://www.cnblogs.com/cly84920/p/4426912.html
Copyright © 2020-2023  润新知