Everyday Scripting with Ruby 读书笔记(3)

---
利器在手,正则表达式
Our Friend, the Regular Expression
---


each: 返回初始数组, collect: 收集代码块执行值返回新数组..
irb(main):045:0> [1, 2, 3].each do | ele |
irb(main):046:1*   ele * 100
irb(main):047:1>   end
=> [1, 2, 3]
irb(main):048:0> [1, 2, 3].collect do | ele |
irb(main):049:1*   ele * 100
irb(main):050:1>   end
=> [100, 200, 300]

特殊字符+: 匹配字符一次或多次..
irb(main):054:0> /o+/ =~ "google"
=> 1

特殊字符*: 匹配字符零次或多次..
irb(main):078:0> /go*gle/ =~ "google"
=> 0

\d: 匹配数字..
irb(main):055:0> /\d yuan/ =~ "20 yuan"
=> 1

\w: 匹配单词字符(包括字母, 数字, 下划线)..
irb(main):079:0> /\w+/ =~ "google"
=> 0
irb(main):080:0> /\w+/ =~ "12345"
=> 0
irb(main):081:0> /\w+/ =~ "@"
=> nil

match消息: 若存在匹配返回MatchData对象, 它的第零个元素为第一个匹配的子串..
irb(main):057:0> match = /\d yuan/.match("I have 20 yuan")
=> #<MatchData:0xdd363ac>
irb(main):058:0> match[0]
=> "0 yuan"
irb(main):059:0> match[1]
=> nil

圆括号: 括住部分为分组, 通过MatchData对象访问..
irb(main):061:0> match = /have (\d+) yuan/.match("I have 20 yuan")
=> #<MatchData:0xdd2af20>
irb(main):062:0> match[0]
=> "have 20 yuan"
irb(main):063:0> match[1]
=> "20"
irb(main):064:0> match[2]
=> nil

sort方法: 默认按升序排列..
irb(main):067:0> [2, 3, 1].sort
=> [1, 2, 3]
irb(main):068:0> ['dog', 'cat', 'tiger', 'fox'].sort
=> ["cat", "dog", "fox", "tiger"]

太空船操作符<=>: 如果已按正确次序排列返回-1, 否则返回1, 这里代码块进行求反操作以实现降序排列..
irb(main):069:0> [2, 3, 1].sort do | one, another |
irb(main):070:1*   - (one <=> another)
irb(main):071:1>   end
=> [3, 2, 1]

# Exercises

def rearrange(england_name)
  has_middlename = /(\w+), (\w+) (\w+)/.match(england_name)
  hasnot_middlename = /(\w+), (\w+)/.match(england_name)
  
  if(has_middlename)
    lastname = has_middlename[1]
    firstname = has_middlename[2]
    middlename = has_middlename[3]
    "#{firstname} #{middlename[0,1]}. #{lastname}"
  elsif(hasnot_middlename)
    lastname = hasnot_middlename[1]
    firstname = hasnot_middlename[2]
    "#{firstname} #{lastname}" 
  end
end


#---
# Excerpted from "Everyday Scripting in Ruby"
# We make no guarantees that this code is fit for any purpose. 
# Visit http://www.pragmaticprogrammer.com/titles/bmsft for more book information.
#---
require 'test/unit'
require 'myexercise'

class RearrangeTests < Test::Unit::TestCase

  def test_rearrange_with_middle_name
    assert_equal("Dawn E. Marick", rearrange("Marick, Dawn Elaine"))
  end

  def test_rearrange_without_middle_name
    assert_equal("Paul Marick", rearrange("Marick, Paul"))
  end

end


** 运行结果 **
>ruby exercise-4-tests.rb
Loaded suite exercise-4-tests
Started
..
Finished in 0.015 seconds.

2 tests, 2 assertions, 0 failures, 0 errors
>Exit code: 0
** **

# Exercises 另一种写法

def rearrange(england_name)
  has_or_hasnot_middlename = /(\w+), (\w+)( \w+)?/.match(england_name)
  
  lastname = has_or_hasnot_middlename[1]
  firstname = has_or_hasnot_middlename[2]
  
  if(has_or_hasnot_middlename[3])
    middlename = has_or_hasnot_middlename[3]
    middlename_output = middlename[1,1] + '. '  # middlename的首字符是空格
  else
    middlename_output = ''
  end
  
  "#{firstname} #{middlename_output}#{lastname}"
end

你可能感兴趣的:(正则表达式,Google,读书,Ruby,Go)