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

Everyday Scripting with Ruby 读书笔记(1)

    博客分类:
  • Ruby
阅读更多
---
第一个脚本:比较文件清单
A First Script: Comparing File Inventories
---


#---
# 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.
#---
def check_usage
  unless ARGV.length == 2  # 注释②
    puts "Usage: differences.rb old-inventory new-inventory"
    exit
  end
end

def boring?(line)
  line.chomp.split('/').include?('temp') or
    line.chomp.split('/').include?('recycler')
  end
  
def contains?(line, key)
  line.chomp.split('/').include?(key)  # 注释⑥
end

def boring?(line, boring_words)
  boring_words.any? do | a_boring_word |  # 注释⑤
    contains?(line, a_boring_word)
  end
end

def inventory_from(filename)
  inventory = File.open(filename)
  downcased = inventory.collect do | line |  # 注释③
    line.downcase
  end
  downcased.reject do | line |  # 注释④
    # contains?(line, 'temp') or contains?(line, 'recycler')
    boring?(line, ['temp','recycler'])
  end
end

def compare_inventory_files(old_file, new_file)
  old_inventory = inventory_from(old_file)
  new_inventory = inventory_from(new_file)

  puts "The following files have been added:"
  puts new_inventory - old_inventory

  puts ""
  puts "The following files have been deleted:"
  puts old_inventory - new_inventory
end

if $0 == __FILE__  # 注释①
  check_usage 
  compare_inventory_files(ARGV[0], ARGV[1]) 
end


① $0表示在命令行里出现的运行脚本的名字, __FILE__表示脚本的名字, 如果是在命令行里运行的脚本则执行,否则什么也不做..
irb(main):001:0> __FILE__
=> "(irb)"
irb(main):002:0> $0
=> "D:/ruby/bin/fxri.rbw"

② unless意为"除非", 相当于if not..
irb(main):012:0> def check_unless(x)
irb(main):013:1>   unless x==2
irb(main):014:2>     puts "x does not equal 2"
irb(main):015:2>     end
irb(main):016:1>   end
=> nil
irb(main):017:0> check_unless(2)
=> nil
irb(main):018:0> check_unless(3)
x does not equal 2
=> nil

③ 迭代器collect: 收集代码块每次执行的值, 然后放入一个新的数组作为返回值..
irb(main):019:0> [1, 2, 3].collect do | ele |
irb(main):020:1*   ele * 2
irb(main):021:1>   end
=> [2, 4, 6]

④ 迭代器reject: 过滤代码块返回为真的所有元素, 然后放入一个新的数组作为返回值..
irb(main):022:0> [1, 2, 3].reject do | ele |
irb(main):023:1*   ele == 2
irb(main):024:1>   end
=> [1, 3]

⑤ 迭代器any: 数组中任意一个元素使得附带的代码块返回true, 则返回true..
irb(main):028:0> [1, 2, 3].any? do | ele |
irb(main):029:1*   ele > 2
irb(main):030:1>   end
=> true

⑥ chomp用来去掉"\n"字符..
irb(main):031:0> "end\n".chomp
=> "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 1 Dreams of Build...............................1 CHAPTER 2 Raven Takes off...............................5 CHAPTER 3 Wait, I Have Dependencies!....................19 CHAPTER 4 Divide and ...

    Advanced Bash-Scripting Guide 读书笔记

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

    Linux Shell Scripting with Bash

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

    Microsoft - Windows Scripting With Wmi(2007)

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

    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