第三部分
数值类
-
一些方法:
返回商的整数:
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>
-
数组:
-
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> -
%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
-
-
将散列转换成数组:
<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> -
一次性拿到多个值:
-
a[n]
ora[-n]
如果是负数,就倒着开始取值。超过总数值会报错。
-
-
a[n..m]
ora[n…m]
-
a[n,len]
从某个元素起,取n个字符。
-
-
一次性赋多个值:
<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>
-
把数组当集合来运算:
交集:
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>
-
把数组当<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>
一些操作方法:
-
为数组增加元素:
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同时引用一个对象!这一点要纠正认识!
-
从数组中删除元素:
-
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
:删除末尾的元素。返回删除的值。
-
-
替换数组元素:
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>
-
数组与迭代器:有些使用迭代器的对象不是数组,但处理后会返回一个数组:
<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> -
数组的初始化问题:
<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> -
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> -
一个过滤数组的方法:
.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到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> -
累加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> -
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
-
什么是内嵌表达式?
<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> -
创建字符串
-
使用%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
一样的结果。但书中没有说明具体区别,差评!
-
-
获取字符串的长度
length
orsize
两种方法可以,随意!bytesize
可以获取字节数。判断是否为0:
empty?
-
字符串的索引
- 当成数组一样用就可以了。
-
连接字符串
-
用
+
号<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>
-
-
字符串的比较
判断两个字符串是否相同:
==
!=
-
比较大小
<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
字符串的分割:
split(x)
:以x
为分割点进行分割!-
换行符的操作:
-
删掉
<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>
-
-
字符串的检索与置换
-
字符串的检索
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>
-
字符串与数组有很多共同的索引的方法:
s[n]
s[n..m]
s.slice!(n)
s.concat(s2)
s+s2
s.delete(str)
s.reverse
- 其他方法:
* `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, "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;">
"ABCDE".tr("BD", "bd") #=> "AbCdE"</pre>
14章节练习题提取:
-
一个打散的字符串数组,如何快速连接成句子:
<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>
-
统计下面各个字母出现次数,并用*的个数来表示次数:
<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>运行结果如图:
很酷!
散列直接用
a[c]
就可以拿到这个值,不需要进行检索printf
格式输出,方便!先定义好整个字符串样式,再后面定义各个引用值。