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

Everyday Scripting with Ruby 读书笔记(2)

    博客分类:
  • Ruby
阅读更多
---
Churn项目:轻松编写脚本
The Churn Project: Writing Scripts without Fuss
---


subversion安装包下载:svn-1.4.5-setup.rar

#---
# 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.
#---

# This is the same as churn.v7.rb. It's a different file because
# of the mechanics of book production. 

# 处理时间(过去一个月)
def month_before(a_time)
  a_time - 28 * 24 * 60 * 60
end

# 处理打印标题信息
def header(an_svn_date)
  "Changes since #{an_svn_date}:"
end

# 组成打印主体信息(参数:子项目,修改次数)
def subsystem_line(subsystem_name, change_count)
  asterisks = asterisks_for(change_count)
  # 注释②
  "#{subsystem_name.rjust(14)} #{asterisks} (#{change_count})"
end

# 计算星号
def asterisks_for(an_integer)
  '*' * (an_integer / 5.0).round
end

# 计算修改次数
def change_count_for(name, start_date)
  extract_change_count_from(svn_log(name, start_date))
end

# 根据文本信息(解析字符串)
def extract_change_count_from(log_text)
  lines = log_text.split("\n") 
  dashed_lines = lines.find_all do | line |  # 注释③   
    line.include?('--------')     
  end
  dashed_lines.length - 1         
end

# 使用外部程序获取文本信息
def svn_log(subsystem, start_date)
  timespan = "--revision HEAD:{#{start_date}}"
  root = "svn://rubyforge.org//var/svn/churn-demo"
  
  `svn log #{timespan} #{root}/#{subsystem}`
end

# 格式化时间
def svn_date(a_time)
  a_time.strftime("%Y-%m-%d")  # 注释①
end

if $0 == __FILE__
  subsystem_names = ['audit', 'fulfillment', 'persistence',
                     'ui', 'util', 'inventory']
  start_date = svn_date(month_before(Time.mktime(2005,9,2,0,0,0,0)))

  puts header(start_date)
  subsystem_names.each do | name |          
    puts subsystem_line(name, change_count_for(name, start_date))
  end
end


** 运行结果 **
>ruby churn.v7copy.rb
Changes since 2005-08-05:
         audit * (5)
   fulfillment  (2)
   persistence * (3)
            ui ** (8)
          util * (4)
     inventory  (2)
>Exit code: 0
** **


① Time对象的strftime用于格式化时间..
irb(main):005:0> Time.now.strftime('%m-%d %Y')
=> "10-28 2008"

② rjust用于实现右对齐..
irb(main):007:0> 'right'.rjust(2)
=> "right"
irb(main):008:0> 'right'.rjust(8)
=> "   right"

③ find_all: 获取代码块返回为真的所有元素, 然后放入一个新的数组作为返回值..
irb(main):009:0> ['abxc', 'xx', 'b', 'ycx'].find_all do | ele |
irb(main):010:1*   ele.include?('x')
irb(main):011:1>   end
=> ["abxc", "xx", "ycx"]

require 'test/unit'
require 'churn.v7copy'

class ChurnMyTests < Test::Unit::TestCase
  
  def test_month_before
    assert_equal(Time.local(2008,9,30), month_before(Time.local(2008,10,28)))
  end
  
  def test_svn_date
    assert_equal("2008-10-28",svn_date(Time.now))
  end
  
  def test_header
    assert_equal("Changes since 2008-10-28:", header(svn_date(Time.mktime(2008,10,28,0,0,0,0))))
  end
  
  def test_extract_change_count_from
    assert_equal(2,extract_change_count_from("------------------------------------------------------------------------
r2 | marick | 2005-08-07 14:26:21 -0500 (Mon, 07 Aug 2005) | 1 line

added code to handle merger
------------------------------------------------------------------------
r1 | marick | 2005-08-07 14:21:47 -0500 (Mon, 07 Aug 2005) | 1 line

first touches
No commit for revision 0.
------------------------------------------------------------------------"))
  end
  
  def test_asterisks_for
    assert_equal("*********", asterisks_for(46))
  end
  
end


** 运行结果 **
>ruby churn-mytest-v7copy.rb
Loaded suite churn-mytest-v7copy
Started
.....
Finished in 0.0 seconds.

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

# Exercises

# 处理时间(过去一个月)
def month_before(a_time)
  a_time - 28 * 24 * 60 * 60
end

# 处理打印标题信息
def header(an_svn_start_date, an_svn_today_date)
  "Changes between #{an_svn_start_date} and #{an_svn_today_date}:"
end

# 组成打印主体信息(参数:子项目,修改次数)
def subsystem_line(subsystem_name, change_count)
  asterisks = asterisks_for(change_count)
  change_text = change_text_from(change_count)
  unless change_count == 0
    return "#{subsystem_name.ljust(14)} #{change_text.ljust(14)} #{asterisks}"
  end
  return "#{subsystem_name.ljust(14)} #{'-'.ljust(14)} -"
end

# 计算星号
def asterisks_for(an_integer)
  if(an_integer < 3)
    '*'
  else
    '*' * (an_integer / 5.0).round
  end
end

# 处理修改次数信息
def change_text_from(an_integer)
  return "(#{an_integer} changes)"
end

# 计算修改次数
def change_count_for(name, start_date)
  extract_change_count_from(svn_log(name, start_date))
end

# 根据文本信息(解析字符串)
def extract_change_count_from(log_text)
  lines = log_text.split("\n") 
  dashed_lines = lines.find_all do | line |   
    line.include?('--------')     
  end
  dashed_lines.length - 1         
end

# 使用外部程序获取文本信息
def svn_log(subsystem, start_date)
  timespan = "--revision HEAD:{#{start_date}}"
  root = "svn://rubyforge.org//var/svn/churn-demo"
  
  `svn log #{timespan} #{root}/#{subsystem}`
end

# 格式化时间
def svn_date(a_time)
  a_time.strftime("%Y-%m-%d")
end

if $0 == __FILE__
  subsystem_names = ['audit', 'fulfillment', 'persistence',
                     'ui', 'util', 'inventory']
  start_date = svn_date(month_before(Time.mktime(2005,9,2,0,0,0,0)))
  today_date = svn_date(Time.now)

  puts header(start_date, today_date)
  subsystem_names.each do | name |          
    puts subsystem_line(name, change_count_for(name, start_date))
  end
end


** 运行结果 **
>ruby churn.v7copy.rb
Changes between 2005-08-05 and 2008-10-28:
audit          (5 changes)    *
fulfillment    (2 changes)    *
persistence    (3 changes)    *
ui             (8 changes)    **
util           (4 changes)    *
inventory      (2 changes)    *
>Exit code: 0
** **
分享到:
评论

相关推荐

    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)的形式呈现,为读者提供了许多...

    Programming Pig Dataflow Scripting with Hadoop(2nd) 无水印转化版pdf

    Programming Pig Dataflow Scripting with Hadoop(2nd) 英文无水印转化版pdf 第2版 pdf所有页面使用FoxitReader、PDF-XChangeViewer、SumatraPDF和Firefox测试都可以打开 本资源转载自网络,如有侵权,请联系...

    Raven Scripting Java Builds With Ruby

    CHAPTER 2 Raven Takes off...............................5 CHAPTER 3 Wait, I Have Dependencies!....................19 CHAPTER 4 Divide and Conquer: Multimodule Projects......33 CHAPTER 5 Public or ...

    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 是一个命令解释器,是用户与操作系统之间的交互界面。 - 用户通过输入命令,...

Global site tag (gtag.js) - Google Analytics