`
sco.struts
  • 浏览: 18916 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论

Everyday Scripting with Ruby 读书笔记(3)

    博客分类:
  • Ruby
阅读更多
---
利器在手,正则表达式
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
分享到:
评论

相关推荐

    everyday scripting with ruby

    everyday scripting with ruby

    Everyday Scripting With Ruby

    《Everyday Scripting with Ruby》是一本综合性的Ruby教程,涵盖了从基础知识到高级技术的各个方面。通过实践导向的教学方法,读者不仅可以学习到Ruby的核心概念,还能掌握如何在真实环境中应用这些知识。无论是初学...

    Scripting With AD Scripting With AD

    标题 "Scripting With AD" 暗示了我们即将探讨的是使用脚本语言与Active Directory(AD)进行交互的主题。Active Directory是微软Windows操作系统中的一个关键组件,它用于存储和管理网络资源,如用户账户、计算机...

    Unreal Engine 4 Scripting with C++ Cookbook first edition.pdf

    Unreal Engine 4 Scripting with C++Cookbook Get the best out of your games by scripting them using UE4 William Sherif Stephen Whittle 2016版

    Unreal Engine 4.x scripting with C cookbook

    Unreal Engine 4.x scripting with C cookbook develop quality game components and solve scripting problems with the power of C and UE4 by Stephen Whittle John P. Doran William Sherif (z-lib.org)

    Unreal Engine 4 Scripting with C++.pdf

    标题《Unreal Engine 4 Scripting with C++》指明了这本书是关于如何使用C++语言进行Unreal Engine 4(UE4)游戏引擎的脚本编程。UE4是Epic Games开发的一款先进的游戏引擎,广泛应用于现代游戏开发中,它支持C++...

    Unreal Engine 4 Scripting with C++ Cookbook

    Unreal Engine 4 Scripting with C++ Cookbook 2016 | ISBN-10: 1785885545 | 431 pages | PDF | 7 MB Key Features A straightforward and easy-to-follow format A selection of the most important tasks and ...

    Unreal Engine 4 Scripting with C++ Cookbook - 2016.pdf (带彩色插图)

    《Unreal Engine 4 Scripting with C++ Cookbook》是一本面向游戏开发者的专业书籍,专门介绍了如何使用C++语言结合Unreal Engine 4(UE4)进行游戏脚本编写。本书在2016年10月发布了新版,并且包含彩色插图,使得...

    Unreal Engine 4 Scripting with C++ Cookbook willam

    《Unreal Engine 4 Scripting with C++ Cookbook》是一本专为游戏开发人员设计的实用指南,旨在帮助读者深入理解并掌握使用C++在Unreal Engine 4(UE4)中进行脚本编程的技术。这本书涵盖了从基础概念到高级技巧的...

    Unreal Engine 4 Scripting with C++ Cookbook pdf 0分

    标题和描述中提到的《Unreal Engine 4 Scripting with C++ Cookbook》是一本关于Unreal Engine 4(UE4)游戏引擎中使用C++编程语言进行脚本编程的实用指导书。这本书以食谱(Cookbook)的形式呈现,为读者提供了许多...

    Raven Scripting Java Builds With Ruby

    本书介绍了构建简单及方便...CHAPTER 3 Wait, I Have Dependencies!....................19 CHAPTER 4 Divide and Conquer: Multimodule Projects......33 CHAPTER 5 Public or Private Repository..................47

    Advanced Bash-Scripting Guide 读书笔记

    在阅读《Advanced Bash-Scripting Guide》这本书的过程中,我们能学到许多有关Bash脚本的高级用法和技巧。这本书对于那些想要提升其Bash脚本编写能力的用户来说是一份宝贵的资料。接下来,我将根据给定文件的部分...

    Microsoft - Windows Scripting With Wmi(2007)

    《Microsoft - Windows Scripting With Wmi(2007)》是关于Windows Management Instrumentation(WMI)技术的一本PDF教程,旨在帮助IT专业人士深入理解和掌握利用WMI进行Windows脚本编程的技术。WMI是微软提供的一种...

    Linux Shell Scripting with Bash

    ### Linux Shell Scripting with Bash #### 核心知识点解析 **1. Linux Shell Scripting 基础** - **Shell 的概念与作用** - Shell 是一个命令解释器,是用户与操作系统之间的交互界面。 - 用户通过输入命令,...

    Unreal Engine 4 Scripting with C Cookbook (Will.pdf ) 彩色高清

    《Unreal Engine 4 Scripting with C++ Cookbook》是一本为游戏开发者准备的实用指南,它教授如何利用C++在虚幻引擎4环境下进行高级脚本编写。通过这本书,开发者可以学习到如何优化游戏性能、实现复杂的游戏逻辑...

Global site tag (gtag.js) - Google Analytics