当前位置: 代码迷 >> 综合 >> Ruby七天入门(3 Mixin,集合,文件操作)
  详细解决方案

Ruby七天入门(3 Mixin,集合,文件操作)

热度:16   发布时间:2023-12-09 08:29:11.0


DAY 3

学习进入第三天,今天计划学习模块,集合以及简单的文件操作。

3.1Mixin 模块

面向对象语言使用继承,来将行为传播到相似的对象上。具体到语言,C++使用的是多继承,但是过于复杂,Java采用接口的方式,而Ruby则选择使用了模块,先来看下代码:

<code class="hljs livecodeserver has-numbering">module ToFiledef filename<span class="hljs-string">"object_#{self.object_id}.txt"</span><span class="hljs-function"><span class="hljs-keyword">end</span></span>def to_f<span class="hljs-built_in">file</span> = File.<span class="hljs-built_in">open</span>(filename,<span class="hljs-string">'w'</span>)<span class="hljs-built_in">file</span>.<span class="hljs-built_in">write</span>(to_s)<span class="hljs-function"><span class="hljs-keyword">end</span></span>
<span class="hljs-function"><span class="hljs-keyword">end</span></span>class Person<span class="hljs-built_in">include</span> ToFileattr_accessor:namedef initialize(name)@name = name<span class="hljs-function"><span class="hljs-keyword">end</span></span>def to_sname<span class="hljs-function"><span class="hljs-keyword">end</span></span>
<span class="hljs-function"><span class="hljs-keyword">end</span></span><span class="hljs-operator">a</span> = Person.<span class="hljs-built_in">new</span>(<span class="hljs-string">'Lee'</span>)
puts <span class="hljs-operator">a</span>.name,<span class="hljs-operator">a</span>.to_s,<span class="hljs-operator">a</span>.to_f</code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li></ul>

我们定义了一个ToFile的module模块,而在类Person里面include了它(感觉有点像是C++中的include)。

  • 通过这样的include,Person类直接具有了ToFile的能力,而且他们建立了某种关系,使得在ToFile中也可以调用Person类里面的方法。通过先定义类的主要部分,然后通过模块添加额外的能力,这种即插即用式的做法显然相当灵活,这种做法我们称之为mixin。

3.2 集合操作

集合用法并无什么特别之处,无非就是一些方法的使用:

<code class="hljs livecodeserver has-numbering">puts <span class="hljs-string">'begin'</span><=><span class="hljs-string">'end'</span>
<span class="hljs-operator">a</span>=[<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">2</span>,<span class="hljs-number">1</span>]
<span class="hljs-operator">a</span>= <span class="hljs-operator">a</span>.<span class="hljs-built_in">sort</span>
puts <span class="hljs-operator">a</span>
puts <span class="hljs-operator">a</span>.<span class="hljs-keyword">any</span>? { |i| i><span class="hljs-number">6</span>  }
puts <span class="hljs-operator">a</span>.all? { |i| i><span class="hljs-number">0</span> }
puts <span class="hljs-operator">a</span>.collect { |i| i*<span class="hljs-number">2</span> }
puts <span class="hljs-operator">a</span>.select { |i|i%<span class="hljs-number">2</span>==<span class="hljs-number">0</span>  }
puts <span class="hljs-operator">a</span>.<span class="hljs-built_in">max</span>
puts <span class="hljs-operator">a</span>.member?(<span class="hljs-number">2</span>)</code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li></ul>
  • <=>被人们叫做太空船操作符,它比较a、b两操作数,b较大返回-1,a较大返回1,相等返回0。
  • 只要集合中任一元素条件为真,any?就返回true;只有集合所有元素条件为真,all?才返回true。
  • 只要对应的类实现了太空船操作符,就可以调用sort方法排序
  • collect和map方法把函数应用到每个元素上,并返回结果数组
  • find方法找到一个符合条件的元素,而select和find_all方法均返回所有符合条件的元素。

3.3 文件操作

我们在3.1中已经使用了文件的写入,文件的读取也很简单:

<code class="hljs livecodeserver has-numbering">File.<span class="hljs-built_in">open</span>(<span class="hljs-string">"tree.rb"</span>, <span class="hljs-string">"r"</span>) <span class="hljs-built_in">do</span> |<span class="hljs-built_in">file</span>|<span class="hljs-built_in">file</span>.each_line(<span class="hljs-string">"\r\n"</span>){|<span class="hljs-built_in">line</span>| puts <span class="hljs-string">"#{line.to_s}"</span>}
<span class="hljs-function"><span class="hljs-keyword">end</span></span></code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li></ul>

上面代码使用了代码块,而如果不使用代码块,需要手动close文件。

<code class="hljs livecodeserver has-numbering"><span class="hljs-built_in">file</span> = File.<span class="hljs-built_in">open</span>(<span class="hljs-string">"tree.rb"</span>, <span class="hljs-string">"r"</span>) 
<span class="hljs-built_in">file</span>.each_line(<span class="hljs-string">"\r\n"</span>){|<span class="hljs-built_in">line</span>| puts <span class="hljs-string">"#{line.to_s}"</span>}
<span class="hljs-built_in">file</span>.<span class="hljs-built_in">close</span></code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li></ul>

我们也可以使用更简单的IO类:

<code class="hljs livecodeserver has-numbering">IO.foreach(<span class="hljs-string">"tree.rb"</span>){|<span class="hljs-built_in">line</span>| puts <span class="hljs-built_in">line</span>}</code><ul style="display: block;" class="pre-numbering"><li>1</li></ul>

3.4 今日实践

写一个简单的grep程序,把文件中出现某词组的行全都打印出来,并输出行号。

<code class="hljs livecodeserver has-numbering">def grep(filename,match)line_num=<span class="hljs-number">0</span>IO.foreach(filename)<span class="hljs-built_in">do</span>|<span class="hljs-built_in">line</span>| line_num=line_num+<span class="hljs-number">1</span>puts line_num.to_s+<span class="hljs-string">" "</span>+<span class="hljs-built_in">line</span> <span class="hljs-keyword">if</span> <span class="hljs-built_in">line</span>.match(match)<span class="hljs-function"><span class="hljs-keyword">end</span> </span>
<span class="hljs-function"><span class="hljs-keyword">end</span></span>grep(<span class="hljs-string">"tree.rb"</span>,/[Tt]ree/)</code>