1934356476 {619DC685} metaprogramming ruby program like the ruby pros perrotta 2010 02 25

282 350 0
1934356476 {619DC685} metaprogramming ruby  program like the ruby pros perrotta 2010 02 25

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

What Readers Are Saying About Metaprogramming Ruby Reading this book was like diving into a new world of thinking I tried a mix of Java and JRuby metaprogramming on a recent project Using Java alone would now feel like entering a sword fight carrying only a banana, when my opponent is wielding a one-meter-long Samurai blade Sebastian Hennebrüder Java Consultant and Trainer, laliluna.de This Ruby book fills a gap between language reference manuals and programming cookbooks Not only does it explain various metaprogramming facilities, but it also shows a pragmatic way of making software smaller and better There’s a caveat, though; when the new knowledge sinks in, programming in more mainstream languages will start feeling like a chore Jurek Husakowski Software Designer, Philips Applied Technologies Before this book, I’d never found a clear organization and explanation of concepts like the Ruby object model, closures, DSLs definition, and eigenclasses all spiced with real-life examples taken from the gems we usually use every day This book is definitely worth reading Carlo Pecchia Software Engineer I’ve had a lot of trouble finding a good way to pick up these metaprogramming techniques, and this book is bar none the best way to it Paolo Perrotta makes it painless to learn Ruby’s most complex secrets and use them in practical applications Chris Bunch Software Engineer Metaprogramming Ruby Program Like the Ruby Pros Paolo Perrotta The Pragmatic Bookshelf Raleigh, North Carolina Dallas, Texas Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC Every precaution was taken in the preparation of this book However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun For more information, as well as the latest Pragmatic titles, please visit us at http://www.pragprog.com Copyright © 2010 Paolo Perrotta All rights reserved No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher Printed in the United States of America ISBN-10: 1-934356-47-6 ISBN-13: 978-1-934356-47-0 Printed on acid-free paper P1.0 printing, January 2010 Version: 2010-1-29 To Carlo Contents Foreword 10 Acknowledgments 11 Introduction The “M” Word About This Book About You 13 14 21 24 I Metaprogramming Ruby 26 Monday: The Object Model 1.1 Monday with Bill 1.2 Open Classes 1.3 The Truth About Classes 1.4 Quiz: Missing Lines 1.5 What Happens When You Call a Method? 1.6 Quiz: Tangle of Modules 1.7 Object Model Wrap-Up 27 27 28 33 45 46 56 59 Tuesday: Methods 2.1 A Duplication Problem 2.2 Dynamic Methods 2.3 method_missing() 2.4 Quiz: Bug Hunt 2.5 More method_missing() Wednesday: Blocks 3.1 How to Handle 3.2 Quiz: Ruby# 3.3 Closures 3.4 instance_eval() 60 61 63 71 82 84 Hump Day 91 92 93 96 105 CONTENTS 3.5 3.6 3.7 Callable Objects Writing a Domain-Specific Language Quiz: A Better DSL Thursday: Class Definitions 4.1 Class Definitions Demystified 4.2 Quiz: Class Taboo 4.3 Singleton Methods 4.4 Eigenclasses 4.5 Quiz: Module Trouble 4.6 Aliases 4.7 Quiz: Broken Math Friday: Code That Writes Code 5.1 Leading the Way 5.2 Kernel#eval 5.3 Quiz: Checked Attributes (Step 1) 5.4 Quiz: Checked Attributes (Step 2) 5.5 Quiz: Checked Attributes (Step 3) 5.6 Quiz: Checked Attributes (Step 4) 5.7 Hook Methods 5.8 Quiz: Checked Attributes (Step 5) Epilogue II Metaprogramming in Rails 108 116 118 122 123 130 132 137 150 152 157 160 160 163 173 176 178 179 180 186 188 189 The Design of ActiveRecord 190 7.1 Preparing for the Tour 191 7.2 The Design of ActiveRecord 193 7.3 Lessons Learned 202 Inside ActiveRecord 206 8.1 Dynamic Attributes 206 8.2 Dynamic Finders 214 8.3 Lessons Learned 219 Metaprogramming Safely 224 9.1 Testing Metaprogramming 224 9.2 Defusing Monkeypatches 232 9.3 Lessons Learned 237 CONTENTS III Appendixes 239 A 240 240 243 244 248 249 Common Idioms A.1 Mimic Methods A.2 Nil Guards A.3 Tricks with Method Arguments A.4 Self Yield A.5 Symbol#to_proc() B Domain-Specific Languages 252 B.1 The Case for Domain-Specific Languages 252 B.2 Internal and External DSLs 254 B.3 DSLs and Metaprogramming 255 C Spell Book 256 C.1 The Spells 256 D Bibliography 268 Index 269 Foreword Ruby inherits characteristics from various languages — Lisp, Smalltalk, C, and Perl, to name a few Metaprogramming comes from Lisp (and Smalltalk) It’s a bit like magic, which makes something astonishing possible There are two kinds of magic: white magic, which does good things, and black magic, which can nasty things Likewise, there are two aspects to metaprogramming If you discipline yourself, you can good things, such as enhancing the language without tweaking its syntax by macros or enabling internal domain-specific languages But you can fall into the dark side of metaprogramming Metaprogramming can confuse easily Ruby trusts you Ruby treats you as a grown-up programmer It gives you great power such as metaprogramming But you need to remember that with great power comes great responsibility Enjoy programming in Ruby matz October 2009 Appendix D Bibliography [Arm07] Joe Armstrong Programming Erlang: Software for a Concurrent World The Pragmatic Programmers, LLC, Raleigh, NC, and Dallas, TX, 2007 [Gra96] Paul Graham ANSI Common Lisp Prentice Hall, Englewood Cliffs, NJ, 1996 [Hal09] Stuart Halloway Programming Clojure The Pragmatic Programmers, LLC, Raleigh, NC, and Dallas, TX, 2009 [OSV08] Martin Odersky, Lex Spoon, and Bill Venners Programming in Scala 2008 [TFH08] David Thomas, Chad Fowler, and Andrew Hunt Programming Ruby: The Pragmatic Programmers’ Guide The Pragmatic Programmers, LLC, Raleigh, NC, and Dallas, TX, third edition, 2008 [TH05] David Thomas and David Heinemeier Hansson Agile Web Development with Rails The Pragmatic Programmers, LLC, Raleigh, NC, and Dallas, TX, 2005 [Tho99] Simon Thompson Haskell: The Craft of Functional Programming Addison-Wesley, Reading, MA, second edition, 1999 Index Symbols & operator, 109 (stubby lambda) operator, 114 ( ) (parentheses), dropping from method calls, 240 A accessors, 135, 207, 209 ActionController library Filters module, 226 ProcessWithTest module, 230 ActionPack library, 191 actions (controller methods), 225 ActiveRecord library, 206–223 dynamic attributes, 206–214 dynamic finders, 214–219 lessons learned, 219–223 ActiveRecord library, 18, 191, 193–202 Base class, 195–197, 201–202 lessons learned, 202–205 one-page example, 193–195 Validations class, 197–199 ActiveSupport library Callbacks class, 227 ActiveSupport library alias_method_chain() method, 199–201 Callbacks class, 198 ActiveSupport library, 192 after_filter() method, 225 alias keyword, 153 alias_method_chain() method (Module), 199–201 alias_method() method (Module), 153 aliases, 152–157 Around Aliases, 154–156, 199 JCode example, 155 RubyGems example, 154 allocate() method, 37 & operator, 109 ancestors chain, 48

Ngày đăng: 07/01/2017, 20:52

Từ khóa liên quan

Mục lục

  • Metaprogramming Ruby (2010) (Elements)

  • Contents

  • Foreword

  • Acknowledgments

  • Introduction

  • Metaprogramming Ruby

    • Monday: The Object Model

    • Tuesday: Methods

    • Wednesday: Blocks

    • Thursday: Class Definitions

    • Friday: Code That Writes Code

    • Epilogue

    • Metaprogramming in Rails

      • The Design of ActiveRecord

      • Inside ActiveRecord

      • Metaprogramming Safely

      • Appendixes

        • Common Idioms

        • Domain-Specific Languages

        • Spell Book

        • Bibliography

        • Index

        • The Pragmatic Bookshelf

Tài liệu cùng người dùng

Tài liệu liên quan