1. Trang chủ
  2. » Tất cả

Metaprogramming Ruby_ Program Like the Ruby Pros [Perrotta 2010-02-25]

282 1 0

Đ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

Cấu trúc

  • Contents

  • Foreword

  • Acknowledgments

  • Introduction

    • The ``M'' Word

    • About This Book

    • About You

  • Metaprogramming Ruby

    • Monday: The Object Model

      • Monday with Bill

      • Open Classes

      • The Truth About Classes

      • Quiz: Missing Lines

      • What Happens When You Call a Method?

      • Quiz: Tangle of Modules

      • Object Model Wrap-Up

    • Tuesday: Methods

      • A Duplication Problem

      • Dynamic Methods

      • method_missing()

      • Quiz: Bug Hunt

      • More method_missing()

    • Wednesday: Blocks

      • How to Handle Hump Day

      • Quiz: Ruby#

      • Closures

      • instance_eval()

      • Callable Objects

      • Writing a Domain-Specific Language

      • Quiz: A Better DSL

    • Thursday: Class Definitions

      • Class Definitions Demystified

      • Quiz: Class Taboo

      • Singleton Methods

      • Eigenclasses

      • Quiz: Module Trouble

      • Aliases

      • Quiz: Broken Math

    • Friday: Code That Writes Code

      • Leading the Way

      • Kernel#eval

      • Quiz: Checked Attributes (Step 1)

      • Quiz: Checked Attributes (Step 2)

      • Quiz: Checked Attributes (Step 3)

      • Quiz: Checked Attributes (Step 4)

      • Hook Methods

      • Quiz: Checked Attributes (Step 5)

    • Epilogue

  • Metaprogramming in Rails

    • The Design of ActiveRecord

      • Preparing for the Tour

      • The Design of ActiveRecord

      • Lessons Learned

    • Inside ActiveRecord

      • Dynamic Attributes

      • Dynamic Finders

      • Lessons Learned

    • Metaprogramming Safely

      • Testing Metaprogramming

      • Defusing Monkeypatches

      • Lessons Learned

  • Appendixes

    • Common Idioms

      • Mimic Methods

      • Nil Guards

      • Tricks with Method Arguments

      • Self Yield

      • Symbol#to_proc()

    • Domain-Specific Languages

      • The Case for Domain-Specific Languages

      • Internal and External DSLs

      • DSLs and Metaprogramming

    • Spell Book

      • The Spells

    • Bibliography

  • Index

    • Symbols

    • A

    • B

    • C

    • D

    • E

    • F

    • G

    • H

    • I

    • J

    • K

    • L

    • M

    • N

    • O

    • P

    • Q

    • R

    • S

    • T

    • U

    • V

    • W

    • Y

Nội dung

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 ... I, Metaprogramming Ruby, is the core of the book It tells the story of your week in the office, paired with Bill, an experienced Ruby coder: • Ruby? ??s object model is the land in which metaprogramming. .. is metaprogramming, while this other part is not.” In a sense, metaprogramming is a routine part of every Ruby programmer’s job To be clear, metaprogramming isn’t an obscure art reserved for Ruby. .. 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

Ngày đăng: 17/04/2017, 09:26

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN