出版時(shí)間:2010-1 出版社:東南大學(xué)出版社 作者:布朗 頁(yè)數(shù):309
Tag標(biāo)簽:無(wú)
前言
In 1993, when Ruby was born, Ruby had nothing. No user base except for me and a few close friends. No tradition. No idioms except for a few inherited from Perl, though I regretted most of them afterward. But the language forms the community. The community nourishes the culture. In the last decade, users increased——hundreds of thousands of programmers fell in love with Ruby. They put great effort into the language and its community. Projects were born. Idioms tailored for Ruby were invented and introduced. Ruby was influenced by Lisp and other functional programming languages. Ruby formed relationships between technologies and methodologies such as test-driven development and duck typing. This book introduces a map of best practices of the language as of 2009. I've known Greg Brown for years, and he is an experienced Rub)/developer who has contributed a lot of projects to the language, such as Ruport and Prawn. I am glad he compiled his knowledge into this book.
內(nèi)容概要
你能夠編寫真正優(yōu)雅的Ruby代碼嗎?《Ruby最佳實(shí)踐》正是為想要像專家那樣使用Ruby的程序員所準(zhǔn)備的。《Ruby 最佳實(shí)踐(影印版)》由Ruby項(xiàng)目Prawn的開發(fā)者所著,簡(jiǎn)潔地向你闡釋如何使用Ruby編寫優(yōu)美的應(yīng)用程序接口和領(lǐng)域特定語(yǔ)言。此外,還包括如何應(yīng)用函數(shù)式編程的思想和技術(shù),使代碼更簡(jiǎn)潔,使你更有效率。通過(guò)《Ruby 最佳實(shí)踐(影印版)》,你將會(huì)學(xué)到如何編寫可讀性更高,表達(dá)能力更強(qiáng)的代碼,以及許多其他方面的內(nèi)容?!禦uby最佳實(shí)踐》將會(huì)幫助你:* 理解Ruby代碼塊所蘊(yùn)含的神秘力量* 學(xué)習(xí)如何在不破壞原有Ruby代碼的情況下進(jìn)行調(diào)整,例如運(yùn)行時(shí)在模塊內(nèi)糅合* 探究測(cè)試與調(diào)試中的細(xì)節(jié),以及如何從易測(cè)性出發(fā)進(jìn)行設(shè)計(jì)* 學(xué)習(xí)通過(guò)讓事情保持簡(jiǎn)單來(lái)編寫快速代碼* 用于文本處理和文件管理的開發(fā)策略,包括正則表達(dá)式* 理解為什么會(huì)發(fā)生錯(cuò)誤以及錯(cuò)誤是如何發(fā)生的* 利用Ruby的多語(yǔ)言特性減少文化障礙《Ruby 最佳實(shí)踐(影印版)》還包含多個(gè)章節(jié)對(duì)測(cè)試代碼、設(shè)計(jì)應(yīng)用程序接口以及項(xiàng)目維護(hù)做了全面介紹。《Ruby最佳實(shí)踐》將陪伴你學(xué)習(xí)如何將這門豐富、優(yōu)美的語(yǔ)言發(fā)揮到極致。
作者簡(jiǎn)介
Gregory T. Brown是康涅狄格州紐黑文市的一位Ruby愛(ài)好者,他的大多數(shù)時(shí)間都花在了與Ruby語(yǔ)言相關(guān)的自由軟件項(xiàng)目上。他是Ruport的原作者,并且是Prawn的作者,該Ruby庫(kù)被用來(lái)生成PDF文檔。
書籍目錄
Foreword Preface 1. Driving Code Through Tests A Quick Note on Testing Frameworks Designing for Testability Testing Fundamentals Well-Focused Examples Testing Exceptions Run the Whole Suite at Once Advanced Testing Techniques Using Mocks and Stubs Testing Complex Output Keeping Things Organized Embedding Tests in Library Files Test Helpers Custom Assertions Conclusions 2. Designing Beautiful APIs Designing for Convenience: Ruport’s Table( ) feature Ruby’s Secret Power: Flexible Argument Processing Standard Ordinal Arguments Ordinal Arguments with Optional Parameters Pseudo-Keyword Arguments Treating Arguments As an Array Ruby’s Other Secret Power: Code Blocks Working with Enumerable Using Blocks to Abstract Pre- and Postprocessing Blocks As Dynamic Callbacks Blocks for Interface Simplification Avoiding Surprises Use attr_reader, attr_writer, and attr_accessor Understand What method? and method! Mean Make Use of Custom Operators Conclusions 3. Mastering the Dynamic Toolkit BlankSlate: A BasicObject on Steroids Building Flexible Interfaces Making instance_eval( ) Optional Handling Messages with method_missing( ) and send( ) Dual-Purpose Accessors Implementing Per-Object Behavior Extending and Modifying Preexisting Code Adding New Functionality Modification via Aliasing Per-Object Modification Building Classes and Modules Programmatically Registering Hooks and Callbacks Detecting Newly Added Functionality Tracking Inheritance Tracking Mixins Conclusions 4. Text Processing and File Management Line-Based File Processing with State Tracking Regular Expressions Don’t Work Too Hard Anchors Are Your Friends Use Caution When Working with Quantifiers Working with Files Using Pathname and FileUtils The tempfile Standard Library Automatic Temporary Directory Handling Collision Avoidance Same Old I/O Operations Automatic Unlinking Text-Processing Strategies Advanced Line Processing Atomic Saves Conclusions 5. Functional Programming Techniques Laziness Can Be a Virtue (A Look at lazy.rb) Minimizing Mutable State and Reducing Side Effects Modular Code Organization Memoization Infinite Lists Higher-Order Procedures Conclusions 6. When Things Go Wrong A Process for Debugging Ruby Code Capturing the Essence of a Defect Scrutinizing Your Code Utilizing Reflection Improving inspect Output Finding Needles in a Haystack Working with Logger Conclusions 7. Reducing Cultural Barriers m17n by Example: A Look at Ruby’s CSV Standard Library Portable m17n Through UTF-8 Transcoding Source Encodings Working with Files Transcoding User Input in an Organized Fashion m17n in Standalone Scripts Inferring Encodings from Locale Customizing Encoding Defaults m17n-Safe Low-Level Text Processing Localizing Your Code Conclusions 8. Skillful Project Maintenance Exploring a Well-Organized Ruby Project (Haml) Conventions to Know About What Goes in a README Laying Out Your Library Executables Tests Examples API Documentation via RDoc Basic Documentation Techniques and Guidelines Controlling Output with RDoc Directives The RubyGems Package Manager Writing a Gem::Specification Working with Dependencies Rake: Ruby’s Built-in Build Utility Conclusions A. Writing Backward-Compatible Code B. Leveraging Ruby’s Standard Library C. Ruby Worst Practices Index
章節(jié)摘錄
插圖:Rake is a very powerful tool that deserves its own chapter or even its own cookbook.There are a ton of useful recipes out there in the wild, so be sure to make the Rakefileone of your first stops in any new codebase you need to review. Understanding andusing Rake effectively is key to successfully managing any moderately complex Rubyproject, so be sure not to overlook its significance and practical utility.If you want to make the most out of this tool, there are just a few things to keep in mind:Rake provides custom tasks for common needs such as generating RDoc, runningunit tests and packaging up a project for distribution. Because these tasks are highlyconfigurable, it is better to use them than to reinvent the wheel.Any other repetitive action that is necessary for maintaining your project can bewrapped in a task to simplify things. Typically, any lengthy command that needsto be run in the shell is fair game for this sort of simplification.Any task that has a preceding desc() call will be listed with a meaningful messagein the rake ——tasks output for your project.Rake's ability to define prerequisite tasks allows you to build dependency-basedworkflows that allow you to model multiple-step tasks as needed.Namespaces can be used to segment off tasks into their own subspaces, minimizingthe risk of naming clashes.I've tried to stick mainly to the easily overlooked aspects of Rake here, but there is awhole lot that we could have covered and didn't. Be sure to consult the Rake docu-mentation if you're interested in finding out more.Depending on what you were looking for, this chapter may have been a bit differentfrom what you were expecting based on the title. However, what you will find is thatthe things we've discussed here will really take you far when it comes to improving themaintainability of your projects. Though far from glamorous, things like good docu-mentation, well-organized folders and files, and a way to automate as much of the gruntwork as possible does a whole lot for your projects.Poorly maintained projects can be a huge drain on developer productivity and morale,yet nicely curated code can be downright enjoyable to work with, even if you're brand-new to a project. The tools and techniques we've discussed so far aren't going to makemaintenance completely painless, but will still provide a solid foundation to work offof that will grow over time.
媒體關(guān)注與評(píng)論
“這是一本極為務(wù)實(shí)的著作,各層次的開發(fā)人員都能從中借鑒?!? ——Brad Ediger,Madriska Media Group的領(lǐng)袖開發(fā)者,同時(shí)也是《Advanced Rails》(O'Reilly)的作者 “終于有這樣一本書問(wèn)世了,它不僅教我如何使用Ruby,更教會(huì)我如何正確地使用它。每位Ruby愛(ài)好者的書架上都該擺上一本《Ruby最佳實(shí)踐》?!? ——Jeremy McAnally,ENTP開發(fā)者,同時(shí)還是《Ruby in Practice》(Manning)一書的作者 “我敢打賭,通過(guò)閱讀這本書,你一定學(xué)到了可以提高Ruby編程能力的新技巧?!? ——James Edward Gray II,代碼忍者及Ruby 1.9 的CSV標(biāo)準(zhǔn)庫(kù)的作者
編輯推薦
《Ruby 最佳實(shí)踐(影印版)》是由東南大學(xué)出版社出版的。
圖書封面
圖書標(biāo)簽Tags
無(wú)
評(píng)論、評(píng)分、閱讀與下載