--- title: ruby学习笔记 date: 2016-12-08 22:54:49 category: notes tags: - ruby keywords: - ruby --- ## regular expressions `=~`是用于正则表达式的匹配操作符。返回匹配到的字符串位置或nil。 ```ruby "abcdef" =~ /d/ # return 3 "aaaaaa" =~ /d/ # return nil ``` ## !和? The exclamation point (!, sometimes pronounced aloud as "bang!") indicates something potentially destructive, that is to say, something that can change the value of what it touches. ``` ruby> s1 = "forth" "forth" ruby> s1.chop! # This changes s1. "fort" ruby> s2 = s1.chop # This puts a changed copy in s2, "for" ruby> s1 # ... without disturbing s1. "fort" ``` You'll also sometimes see chomp and chomp! used. These are more selective: the end of a string gets bit off only if it happens to be a newline. The other method naming convention is the question mark (?, sometimes pronounced aloud as "huh?") indicates a "predicate" method, one that can return either true or false. ## 四种内部中断循环的方式 * `break` * `next` 等同与continue * `redo` restarts the current iteration * `return` ## 迭代器 iterator Ruby's String type has some useful iterators. `each_byte` is an iterator for each character in the string. ```shell irb(main):001:0> "abc".each_byte{|c| printf "<%c>", c}; print "\n" => nil ``` Another iterator of String is `each_line`. ```shell irb(main):002:0> "a\nb\nc\n".each_line{|l| print l} a b c => "a\nb\nc\n" ``` ruby的 `for in` 也是一种迭代。We can use a control structure `retry` in conjunction with an iterated loop, and it will retry the loop from the beginning. ### yield `yield` occurs sometimes in a definition of an iterator. `yield` moves control to the block of code that is passed to the iterator (this will be explored in more detail in the chapter about procedure objects). The following example defines an iterator repeat, which repeats a block of code the number of times specified in an argument. ```ruby irb(main):003:0> def repeat(num) irb(main):004:1> while num > 0 irb(main):005:2> yield irb(main):006:2> num -= 1 irb(main):007:2> end irb(main):008:1> end => :repeat irb(main):009:0> repeat(3) { puts "foo" } foo foo foo => nil ``` ## class ### 继承 继承的格式为: ```ruby class Superclass def breathe puts "inhale and exhale" end def identify puts "I'm super" end def speak(word) puts word end end class Subclass Wzr=1222 => 1222 irb(main):010:0> Wzr=1223 (irb):10: warning: already initialized constant Wzr (irb):9: warning: previous definition of Wzr was here => 1223 ``` 常量可以在类和模块中定义,并允许外部访问。 ```ruby class ConstClass C1=120 end ConstClass::C1 # return 120 ``` ## 访问器(accessor) 实例属性需要通过属性访问器访问。常规访问器有简化写法: ```ruby class Fruit def kind=(k) @kind = k end def kind @kind end end ``` ### inspect方法 当创建一个对象时解释器会返回一些信息: ```ruby irb(main):009:0> apple = Fruit.new => # irb(main):010:0> ``` 可以通过`inspect`关键字来改变这种默认行为。 ```ruby class Fruit def inspect "a fruit is created" end end ``` `inspect`方法常用来调试,可通过下面两种方式显示调用: ```ruby p anObject puts anObject.inspect ``` ### shortcuts `Ruby`提供了访问器的一些简写形式: |缩写|效果| | :------: | :------: | |attr_reader :v| def v; @v; end| |attr_writer :v| def v=(value); @v=value; end| |attr_accessor :v| attr_reader :v; attr_writer :v| |attr_accessor :v, :w| attr_accessor :v; attr_accessor :w| ## 注释 单行注释以`#`开头。 块注释可使用`=begin` `=end`来标记。 ```ruby =begin 这是一段注释块。This is a comment block. Egg, I dreamed I was old. =end ``` ## Dynamic Dispatch ## Mixins Ruby has no multiple inheritance. But it has mixins. ### Lookup rulres When looking for receiver obj's method m, * look in obj's class * look in mixins the class includes(later includes shadow) * look in obj's superclass * look in mixins the superclass inculdes * ...