《Ruby基础教程》第三部分提取笔记

第三部分

数值类

  1. 一些方法:

    • 返回商的整数:x.quo(y)

    • 返回一个数组,[商,余数]:x.divmod(y)

    • Math模块 你想得到的数学函数都在里面

    • 返回整数部分:.floor

    • 返回100以内的随机数:Random.rand(100) (比我以前学的VB方便太多了好嘛!)

    • Integer的迭代方法:

      • upto遍历

      • downto遍历

    • 以分数的形式存在:Rational(1, 10)

    • step迭代的方法:

      • 2到10,每次加3:

        <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n518" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">
        2.step(10,3) do |i|
        p i
        end #=> 2, 5, 8</pre>

      • 10到2,每次减3:

        <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n522" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">
        10.step(2,-3) do |i|
        p i
        end #=> 10, 7, 4</pre>

数组:

  1. Array.new方法

    <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n528" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">
    Array.new #=> []
    Array.new(5) #=> [nil,nil,nil,nil,nil]
    Array.new(5,0) #=> [0,0,0,0,0]</pre>

  2. %w%i

    • 创建不包含空白的字符串数组时,可以使用%w:

      <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n536" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">
      lang = %w(Ruyb Perl Python Scheme Pike)
      p lang #=>["Ruby","Perl","Python","Scheme","Pike"]</pre>

    • 创建不包含空白的符号数组时,可以使用%i:

      <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n540" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">
      lang = %w(Ruyb Perl Python Scheme Pike)
      p lang #=>[:Ruby,:Perl,:Python,:Scheme,:Pike]</pre>

    其中()可替换成其他符号:<> || !! @@ AA

  3. 将散列转换成数组:

    <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n546" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">
    a = {a: 1, b: 2}
    a.to_a #=> [[:a, 1], [:b, 2]]</pre>

  4. 一次性拿到多个值:

    • a[n] or a[-n] 如果是负数,就倒着开始取值。超过总数值会报错。
  • a[n..m] or a[n…m]

    • a[n,len] 从某个元素起,取n个字符。
  1. 一次性赋多个值:

    • <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n569" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">
      a = [1,2,3,4,5,6]
      a[2,3] = [c,d,e] #=> [1,2,c,d,e,6]
      a[2,0] = [a,b] #=> [1,2,a,b,3,4,5,6]</pre>

    • <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n571" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">
      a = [1,2,3,4,5,6]
      a[2..3] = [a,b] #=> [1,2,a,b,5,6]</pre>

    • .values_at方法

      <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n575" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">
      a = [1,2,3,4,5,6]
      a.values_at(1,3,5) #=> [2,4,6]</pre>

  2. 把数组当集合来运算:

    • 交集:ary = ary1 & ary2

    • 并集:ary = ary1 | ary2

    • 集合的差:ary = ary1 - ary2 (在1中找2没有的元素)

      <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n589" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">
      ary1 = ["a","b","c"]
      ary2 = ["b","c","d"]
      p (ary1 - ary2) #=> ["a"]</pre>

  3. 把数组当<u style="box-sizing: border-box;"><u style="box-sizing: border-box;"></u></u>:

    数组结构:

    • 队列:以排列的顺序,先进先出。(像排队过关一样)
    • 栈:以相反的顺序,先进后出。(像堆东西一样,最慢放入的最容易取出)
    • 追加,删除,引用:

    <figure class="md-table-fig" contenteditable="false" cid="n607" mdtype="table" style="box-sizing: border-box; margin: -8px 0px 0px -8px; overflow-x: auto; max-width: calc(100% + 16px); padding: 8px;">

    操作 在头部开刀 在尾部开刀
    追加元素 unshift push
    删除元素 shift pop
    引用元素 first last

    </figure>

    例子:

    <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n626" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">
    a = [1,2,3,4,5]
    a.push("E") #=> [1,2,3,4,5,E]
    a.shift #=> 1
    a #=> [2,3,4,5,E]</pre>

一些操作方法:
  1. 为数组增加元素:

    • a.unshift(item)

    • a.push(item) ~= a << item

    • a.concat(b) & a+b

      这个concat方法是破坏性方法,会改变被引用的对象。

    什么是<u style="box-sizing: border-box;"><u style="box-sizing: border-box;">破坏性的方法</u></u>

    :会改变接收对象值的方法!要注意:被引用的对象也会被破坏!如:

    <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n651" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">
    a = [1,2,3,4]
    b = a
    b.pop #=> 4
    p a #=> [1,2,3]</pre>

    提示:这里b引用了a,并不是复制一个a,而是让a和b同时引用一个对象!这一点要纠正认识!

  2. 从数组中删除元素:

    • a.compact & a.compact! :会把a数组中的空元素nil去掉!区别:

      • 第一种会返回一个新的数组。

      • 第二种会直接替换掉原来的数组。

    • a.delete(x) :从数组中删除x元素。

    • a.delete_at(n) : 从数组中删除a[n]元素。

    • a.delete_if {|item| … }``a.reject {|item| … }``a.reject! {|item| … }: 这三个方法表示:遍历所有元素,如何右边的block块成立就删掉,其中带感叹号表示破坏性的方法!

    • a.slice!(n)``a.slice!(n..m)``a.slice!K(n,len): 这三个方法表示:从数组a中删除指定的部分,并返回被删除的部分的值。slice!是具有破坏性的方法。

    • a.uniq & a.uniq! :表示:去掉重复的元素。

    • a.shift :删除开头的元素。返回删除的值。

    • a.pop :删除末尾的元素。返回删除的值。

  3. 替换数组元素:

    • a.collect{|item| … }``a.collect!{|item| … }``a.map{|item| … }``a.map!{|item| … }: 遍历数组a和各元素传给block中的item,最后付出处理后的结果。

    • a.fill(value)``a.fill(value, begin)``a.fill(value, begin, len)``a.fill(value, n..m): 把数组指定元素全部替换成value,默认为全部。

    • a.flatten & a.flatten! :平坦化数组a,就是把里面嵌套的数组展开成一个大数组。(soga,原来是这样啊!)

      <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n716" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">
      a = [1,[2,[3]],[4],5]
      a.flatten!
      p a #=> [1,2,3,4,5]</pre>

    • a.reverse & a.reverse! :反转数组a的元素顺序。

    • a.sort``a.sort!``a.sort {|i,j| … }``a.sort {|i,j| … }:排序,其中block中的i & j 表示:从数组中两个两个拿出来的数据。

    • a.sort_by{|i| … } :根据块的运行结果时序排序。

      <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n730" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">
      a = [2,4,3,5,1]
      p a.sort_by{|i| -i } #=> [5,4,3,2,1] (这里的block只是参与计算而已,不会破坏原来的数组。)</pre>

  4. 数组与迭代器:有些使用迭代器的对象不是数组,但处理后会返回一个数组:

    <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n734" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">
    a = 1..5
    b = a.collect{|i| i += 2}
    p b #=> [3,4,5,6,7]</pre>

  5. 数组的初始化问题:

    <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n738" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">
    a = Array.new(3, [0,0,0]) #=> [[0,0,0],[0,0,0],[0,0,0]]
    a[0][1] = 2 #=> [[0,2,0],[0,2,0],[0,2,0]]
    数组的初始化就是有这些问题,要注意,可以使用block带解决:
    a = Array.new(3) {[0,0,0])} #=> [[0,0,0],[0,0,0],[0,0,0]]
    a[0][1] = 2 #=> [[0,2,0],[0,0,0],[0,0,0]]</pre>

  6. zip方法 :引进多个数组,然后同步遍历!:

    <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n742" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">
    ary1 = [1,2,3,4,5]
    ary2 = [10,20,30,40,50]
    ary3 = [100,200,300,400,500]

    result = []
    ary1.zip(ary2,ary3) do |a, b, c|
    result << a + b + c
    end
    p result #=> [111, 222, 333, 444, 555]</pre>

  7. 一个过滤数组的方法:.select

    <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n746" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">
    a = (1..100).to_a
    a.select{|i| i % 3 == 0} 过滤3的倍数。</pre>

13章练习题:
  1. 创建一个1到100的整数按升序排列的数组:

    <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n754" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">
    (1..100).to_a</pre>

  2. 累加1中的数组:

    <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n758" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">
    a.reduce :+</pre>

  3. inject方法

    <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n762" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">
    a.inject(0){|memo, i| memo += i}
    表示:遍历数组元素赋值给i,每次结果回传给memo.实现累加的效果。</pre>

字符串String

  1. 什么是内嵌表达式?

    <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n770" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">
    "String#{ruby}" #=> 这个#{}里可以执行ruby表达式的东西就是啦!</pre>

  2. 创建字符串

    • 使用%Q与%q

      <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n778" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">
      desc = %Q{Ruby 的字符中也可以使用'' 和 "".}
      str = %q|Ruby said, 'Hello world!'|

      其中%Q相当于"",%q相当于''。</pre>

    • Here Document方法

      <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n782" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">
      <<"结束标识"
      内容
      结束标识

      => (这里更多的时候要换成<<-"结束标识",这样能让结束的标识不一定在)


      这个方法应该很少用。</pre>

    • sprintf方法 & printf方法

      • printf

      <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n790" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">
      n = 123
      printf("%d\n", n) #=> %d表示以整数形式输出
      printf("%4d\n", n) #=> %4d表示以4位数格式输出
      printf("%04d\n", n) #=> %04d表示不够4位时被零
      printf("%+d\n", n) #=> %+d表示输出结果带 + or -</pre>

      <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n791" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">
      n = "Ruby"
      printf("Hello,%s!\n",n) #=> %s表示以字符串形式输出
      printf("Hello,%8s!\n",n) #=> %8s表示输出炎8位字符串
      printf("Hello,%-8s!\n",n) #=> %-8s表示输出左对齐的8位字符</pre>

      • sprintf

      printf一样的结果。但书中没有说明具体区别,差评!

  3. 获取字符串的长度

    • length or size两种方法可以,随意!

    • bytesize可以获取字节数。

    • 判断是否为0:empty?

  4. 字符串的索引

    • 当成数组一样用就可以了。
  5. 连接字符串

    • +

      <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n825" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">
      a = "Hello,"
      b = "World!"
      a + b #=> "Hello,World!"</pre>

    • <<

      <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n829" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">
      a = "Hello,"
      b = "World!"
      a << b #=> "Hello,World!" (a会被改变!)</pre>

  6. 字符串的比较

    • 判断两个字符串是否相同:== !=

    • 比较大小

      <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n840" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">
      "aaaaa" < "b" #=> true 一般按照a~z的顺序排序</pre>

    • 如果查看码位 : .ord

  7. 字符串的分割:split(x) :以x为分割点进行分割!

  8. 换行符的操作:

    • 删掉

      <figure class="md-table-fig" contenteditable="false" cid="n854" mdtype="table" style="box-sizing: border-box; margin: -8px 0px 0px -8px; overflow-x: auto; max-width: calc(100% + 16px); padding: 8px;">

      属性 删掉最的一个字符 删掉挑选符
      非破坏性的 chop chomp
      破坏性的 chop! chomp!

      </figure>

      <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n867" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">
      a = "abcde"
      b = "abcde\n"
      a.chop #=> "abcd"
      b.chop #=> "abcde"
      b.chomp #=> "abcde"</pre>

  9. 字符串的检索与置换

    • 字符串的检索

      • index方法 从左到右检索,返回第一个字母的索引

      • rindex方法 从右到左检索,返回第一字母的索引

      <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n882" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">
      a = "abbbbbb"
      a.index("bb") #=> 1
      a.rindex("bb") #=> 5</pre>

    • 判断是否包含某个索引值: include?

      <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n886" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">
      a = "abbbbbb"
      a.include?("bb") #=> true</pre>

  10. 字符串与数组有很多共同的索引的方法:

  • s[n] s[n..m] s.slice!(n)

  • s.concat(s2) s+s2 s.delete(str) s.reverse

  1. 其他方法:
*   `strip` : 删除头尾的空白字符

*   `upcase` :小写转大写

*   `downcase` :大写转小写

*   `swapcase` :大的转小,小的转大

*   `capitalize` :首字母大写,其余转小写

*   `tr` :置换字符,与`gsub`相似,但这里可以一次转换多个字符。

    <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n920" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, &quot;Liberation Mono&quot;, Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">
    "ABCDE".tr("BD", "bd")    #=> "AbCdE"</pre>
14章节练习题提取:
  1. 一个打散的字符串数组,如何快速连接成句子:

    <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n927" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">
    a = ["Ruby", "is", "an", "object", "oriented", "programming", "language"]
    a.join(" ")

    => "Ruby is an object oriented programming language"</pre>

  2. 统计下面各个字母出现次数,并用*的个数来表示次数:

    <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n931" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; caret-color: rgb(51, 51, 51); color: rgb(51, 51, 51); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; background-position: inherit inherit; background-repeat: inherit inherit;">
    a = "Ruby is an object oriented programming language"
    b = Hash.new(0)
    a.each_char do |c|
    b[c] += 1
    end
    b.sort.each do |k, v|
    printf("'%s': %s\n", c, "*" * v.to_i)
    end</pre>

    运行结果如图:

    image

    很酷!

    • 散列直接用a[c]就可以拿到这个值,不需要进行检索

    • printf格式输出,方便!先定义好整个字符串样式,再后面定义各个引用值。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,547评论 6 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,399评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,428评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,599评论 1 274
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,612评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,577评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,941评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,603评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,852评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,605评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,693评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,375评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,955评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,936评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,172评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,970评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,414评论 2 342

推荐阅读更多精彩内容