---
title: "Ruby  学习笔记"
author: "Crabby"
date: "2012-04-04T00:21:29Z"
tags: ["Ruby"]
categories: ["编程语言"]
slug: "ruby"
draft: false
lastmod: "2012-04-07T12:04:32Z"
id: 1473606
url: "https://crabby.iteye.com/blog/1473606"
---

学习笔记 has\_many :line\_items ，一个购物车中回存在多个items,他们都链接到购物车，因为每一行订单都包含了一个到它的购物车的链接（指向cart\_id); :dependent =&gt; :destroy表明，当删除父对象时，所有与被删除的父记录存在外键关联的记录，回逐渐删除～ 匹配时间 /\\d\\d:\\d\\d:\\d\\d/匹配时间 /Perl.\*Python/ Perl然后是零个或是多个字符 /Perl \*Python/ /Perl +Pyhon/一个或者是多个空格 /Perl\\s+Python/ Perl 空格字符 怎么样利用好模式 =～ 利用好模式 ,返回模式开始的位置，否则返回nil ， if line =~ /Perl|Python/ puts "Scri ~~~~ :#{line}" end 2.6块 和 迭代器 单行的Block使用花括号，多行的block使用do end 如果方法有参数，他们出现在block之前 verbose\_greet("Dave","loyal customer") {puts "hi"} yield 语句，可以一次或者多次调用相关联的block 1.9.3-p125 :031 &gt; def call\_block 1.9.3-p125 :032?&gt; puts "start of method" 1.9.3-p125 :033?&gt; yield 1.9.3-p125 :034?&gt; yield 1.9.3-p125 :035?&gt; puts "End of method" 1.9.3-p125 :036?&gt; end =&gt; nil 1.9.3-p125 :037 &gt; call\_block { puts "In the block" } block应用于数组. ================================== 2.7读/写文件 使用gets获取键盘的输入，print输出 line = gets print line ================= 3.类 对象 变量 歌曲 &lt;名，演唱者，时长&gt; 1.9.3-p125 :054 &gt; class Song 1.9.3-p125 :055?&gt; def initialize(name, artist, duration) 1.9.3-p125 :056?&gt; @name =name 1.9.3-p125 :057?&gt; @artist = artist 1.9.3-p125 :058?&gt; @duration = duration 1.9.3-p125 :059?&gt; end 1.9.3-p125 :060?&gt; end =&gt; nil 实例变量 @开头， 使用下面的.to\_s 只是报告了对象ID ，Ruby中的类永远不封闭:你总是可以向一个已有的类中添加方法. 对自己编写的类和标准的内建类 都适用 1.9.3-p125 :061 &gt; song = Song.new("Bicyclops", "Fleck",260) =&gt; # 1.9.3-p125 :062 &gt; song.inspect =&gt; "#" 1.9.3-p125 :063 &gt; song.to\_s =&gt; "#" 将.to\_s的方法重写 1.9.3-p125 :064 &gt; class Song 1.9.3-p125 :065?&gt; def to\_s 1.9.3-p125 :066?&gt; "Song :#@name --#@artist -- #@duration" 1.9.3-p125 :067?&gt; end 1.9.3-p125 :068?&gt; end =================================================================================================== 3.1继承 和 消息 继承允许 一个类 是另一个类的特化,specialization 举例说明的是卡拉OK，卡拉OK 歌曲与其他的歌曲并没有两样（没有主唱的音轨）,外加歌词和时间信息， 新类 KaraokeSong，在Song基础上加歌词 定义如下： 1.9.3-p125 :081 &gt; class KaraokeSong def initialize(name, artist, duration, lyrics) 1.9.3-p125 :083?&gt; super(name, artist, duration) 1.9.3-p125 :084?&gt; @lyrics = lyrics 1.9.3-p125 :085?&gt; end 1.9.3-p125 :086?&gt; end =&gt; nil &lt; Song 子类的描述符(subclass),Song是KaraokeSong的超类(superclass)，父子关系. 当Ruby遇到方法调用时，先查看song所属的类(KaraokeSong)是否有实现消息名称相同的方法，如果有则执行，否则将执行父类的方法 向song ，即KaraokeSong类的一个对象发送消息to\_s~~ 我们通过让每个类处理其自身实现细节的方法来解决此问题.当调用KaraokeSong.to\_s时，调用其父类的to\_s获得歌曲的细节，之后将歌词的信息添加上去，返回结果，细节如下～ ========= 3.2 对象和属性 objects and Attributes 一个对象的外部可见部分被称之为属性attribute 添加如下的方法，可访问到具体的属性 def name @name end def artist @artist end def duration @duration end 另外方法是attr\_reader class Song attr\_reader :name, :artist, :duration end attr\_reader是ruby的一个方法, ：name（Symbol)是参数, 3.2.1可写的属性 访问对象的属性 ，访问其他的变量 = 完成赋值 ================================================== Virtual Attibutes 3.2.3 Attributes,Instance Variables, and Methods ============================================== 3.3.1 Class Variables 类变量 以下面的为例，@@plays是类变量。 重要的一点是，类变量被类的所有对象所共享 ，与类中的方法相关联；一个类，类变量只存在一份拷贝，@@标识；类变量在使用之前必须初始化. 以之前的点唱机为例，要记录每首歌被播放的次数，如果使用实例变量来记录，一首歌被播放时，实例中的值相应的增加。但是如果要统计总共播放了多少首歌?如何实现？ 1.统计所有Song 对象，累加他们各自的播放次数 2.使用全局变量来记录，虽然此方法不太可取 3.使用类变量（记得初始化哦） class Song @@plays = 0 def initialize(name, artist, duration) @name =name @artist = artist @duration = duration @plays = 0 end attr\_reader :name, :artist, :duration, :plays def duration=(new\_duration) @duration = new\_duration end def duratioon\_in\_minutes @duration/60.0 end def duration\_in\_minutes=(new\_duration) @duration = (new\_duration\*60).to\_i end def to\_s "Song :--------- #@name----#@artist ----(#@duration) " end def play @plays += 1 @@plays += 1 p "this song : #@plays plays .Total #@@plays plays" end end class KaraokeSong &lt; Song def initialize(name, artist, duration, lyrics) super(name, artist, duration) @lyrics = lyrics end def to\_s super + " \[ #@lyrics ]" end end ================================================ 3.3.2 Class Methods Class Methods 类方法 ，使用方式是在方法名前 加入"类." 例如 class Example def instance\_method end def Example.class\_method end end 举例说明（还是以处理之前Song变量为例）, 重新定义一个类,SongList用来处理Song对象是否超时,如果超时则返回True,设定时间为300秒 class SongList MAX\_TIME = 5\*60 def SongList.is\_too\_long(song) //形参是Song的对象 return song.duration &gt; MAX\_TIME end end 调用的时候,直接使用SongList.is\_too\_long(song1)，传入了一个Song类的一个对象(是我自己之前的C++底子太薄弱了,还是咋了，感觉这地方很新奇，灰常强大呀) :wink: