`
sg552
  • 浏览: 621610 次
  • 性别: Icon_minigender_1
  • 来自: 北京
博客专栏
47437293-94b9-3b04-9152-8971c7580049
白手起家学习使用Flex
浏览量:18747
社区版块
存档分类
最新评论

ruby metaprogramming examples

阅读更多

看附件吧。。。

 

Extracted from:
Metaprogramming Ruby
This PDF file contains pages extracted from Metaprogramming Ruby, published by the
Pragmatic Bookshelf. For more information or to purchase a paperback or PDF copy,
please visit http://www.pragprog.com.
Note: This extract contains some colored text (particularly in code listing). This is
available only in online versions of the books. The printed versions are black and white.
Pagination might vary between the online and printer versions; the content is otherwise
identical.
Copyright © 2009 The Pragmatic Programmers, LLC.
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.
Many of the designations used by manufacturers and sellers to distinguish their prod-
ucts 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 transmit-
ted, 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
Whenever someone says they have “a cool trick,” take them
outside and slap them up.
Jim Weirich
Appendix C
Spell Book
This appendix is a “spell book”—a quick reference to all the “spells” in
the book, in alphabetical order. Most of these spells are metaprogram-
ming related (but the ones from Appendix A, on page 242, are arguably
not that “meta”).
Each spell comes with a short example and a reference to the page
where it’s introduced. Go to the associated pages for extended examples
and the reasoning behind each spell.
C.1 The Spells
Argument Array
Collapse a list of arguments into an array.
def my_method(*args)
args.map {|arg| arg.reverse }
end
my_method('abc' , 'xyz' , '123' ) # => ["cba", "zyx", "321"]
For more information, see page 248.
Around Alias
Call the previous, aliased version of a method from a redefined method.
class String
alias :old_reverse :reverse
def reverse
"x#{old_reverse}x"
end
end
T HE S PELLS
"abc".reverse # => "xcbax"
For more information, see page 157.
Blank Slate
Remove methods from an object to turn them into Ghost Methods (75).
class C
def method_missing(name, *args)
"a Ghost Method"
end
end
obj = C.new
obj.to_s # => "#<C:0x357258>"
class C
instance_methods.each do |m|
undef_method m unless m.to_s =~ /method_missing|respond_to?|^__/
end
end
obj.to_s # => "a Ghost Method"
For more information, see page 86.
Class Extension
Define class methods by mixing a module into a class’s eigenclass (a
special case of Object Extension (153)).
class C; end
module M
def my_method
'a class method'
end
end
class << C
include M
end
C.my_method # => "a class method"
For more information, see page 153.
Class Extension Mixin
Enable a module to extend its includer through a Hook Method (183).
C LICK H ERE to purchase this book now.
259
T HE S PELLS
module M
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def my_method
'a class method'
end
end
end
class C
include M
end
C.my_method # => "a class method"
For more information, see page 187.
Class Instance Variable
Store class-level state in an instance variable of the Class object.
class C
@my_class_instance_variable = "some value"
def self.class_attribute
@my_class_instance_variable
end
end
C.class_attribute # => "some value"
For more information, see page 129.
Class Macro
Use a class method in a class definition.
class C; end
class << C
def my_macro(arg)
"my_macro(#{arg}) called"
end
end
class C
my_macro :x # => "my_macro(x) called"
end
For more information, see page 138.
C LICK H ERE to purchase this book now.
260
T HE S PELLS
Clean Room
Use an object as an environment in which to evaluate a block.
class CleanRoom
def a_useful_method(x); x * 2; end
end
CleanRoom.new.instance_eval { a_useful_method(3) }
# => 6
For more information, see page 109.
Code Processor
Process Strings of Code (165) from an external source.
File.readlines("a_file_containing_lines_of_ruby.txt" ).each do |line|
puts "#{line.chomp} ==> #{eval(line)}"
end
# >> 1 + 1 ==> 2
# >> 3 * 2 ==> 6
# >> Math.log10(100) ==> 2.0
For more information, see page 166.
Context Probe
Execute a block to access information in an object’s context.
class C
def initialize
@x = "a private instance variable"
end
end
obj = C.new
obj.instance_eval { @x } # => "a private instance variable"
For more information, see page 107.
Deferred Evaluation
Store a piece of code and its context in a proc or lambda for evaluation
later.
class C
def store(&block)
@my_code_capsule = block
end
def execute
@my_code_capsule.call
end
end
C LICK H ERE to purchase this book now.
261
T HE S PELLS
obj = C.new
obj.store { $X = 1 }
$X = 0
obj.execute
$X # => 1
For more information, see page 110.
Dynamic Dispatch
Decide which method to call at runtime.
method_to_call = :reverse
obj = "abc"
obj.send(method_to_call) # => "cba"
For more information, see page 66.
Dynamic Method
Decide how to define a method at runtime.
class C
end
C.class_eval do
define_method :my_method do
"a dynamic method"
end
end
obj = C.new
obj.my_method # => "a dynamic method"
For more information, see page 70.
Dynamic Proxy
Forward to another object any messages that don’t match a method.
class MyDynamicProxy
def initialize(target)
@target = target
end
def method_missing(name, *args, &block)
"result: #{@target.send(name, *args, &block)}"
end
end
obj = MyDynamicProxy.new("a string" )
obj.reverse # => "result: gnirts a"
C LICK H ERE to purchase this book now.
262
T HE S PELLS
For more information, see page 80.
Flat Scope
Use a closure to share variables between two scopes.
class C
def an_attribute
@attr
end
end
obj = C.new
a_variable = 100
# flat scope:
obj.instance_eval do
@attr = a_variable
end
obj.an_attribute # => 100
For more information, see page 105.
Ghost Method
Respond to a message that doesn’t have an associated method.
class C
def method_missing(name, *args)
name.to_s.reverse
end
end
obj = C.new
obj.my_ghost_method # => "dohtem_tsohg_ym"
For more information, see page 75.
Hook Method
Override a method to intercept object model events.
$INHERITORS = []
class C
def self.inherited(subclass)
$INHERITORS << subclass
end
end
class D < C
end
C LICK H ERE to purchase this book now.
263
T HE S PELLS
class E < C
end
class F < E
end
$INHERITORS # => [D, E, F]
For more information, see page 183.
Kernel Method
Define a method in module Kernel to make the method available to all
objects.
module Kernel
def a_method
"a kernel method"
end
end
a_method # => "a kernel method"
For more information, see page 53.
Lazy Instance Variable
Wait until the first access to initialize an instance variable.
class C
def attribute
@attribute = @attribute || "some value"
end
end
obj = C.new
obj.attribute # => "some value"
For more information, see page 246.
Mimic Method
Disguise a method as another language construct.
def BaseClass(name)
name == "string" ? String : Object
end
class C < BaseClass "string" # a method that looks like a class
attr_accessor :an_attribute # a method that looks like a keyword
end
obj = C.new
obj.an_attribute = 1
C LICK H ERE to purchase this book now.
# a method that looks like an attribute
264
T HE S PELLS
For more information, see page 243.
Monkeypatch
Change the features of an existing class.
"abc".reverse # => "cba"
class String
def reverse
"override"
end
end
"abc".reverse # => "override"
For more information, see page 35.
Named Arguments
Collect method arguments into a hash to identify them by name.
def my_method(args)
args[:arg2]
end
my_method(:arg1 => "A" , :arg2 => "B" , :arg3 => "C" ) # => "B"
For more information, see page 247.
Namespace
Define constants within a module to avoid name clashes.
module MyNamespace
class Array
def to_s
"my class"
end
end
end
Array.new # => []
MyNamespace::Array.new # => my class
For more information, see page 43.
Nil Guard
Override a reference to nil with an “or.”
x = nil
y = x || "a value" # => "a value"
For more information, see page 246.
C LICK H ERE to purchase this book now.
265
T HE S PELLS
Object Extension
Define Singleton Methods by mixing a module into an object’s eigen-
class.
obj = Object.new
module M
def my_method
'a singleton method'
end
end
class << obj
include M
end
obj.my_method # => "a singleton method"
For more information, see page 153.
Open Class
Modify an existing class.
class String
def my_string_method
"my method"
end
end
"abc".my_string_method # => "my method"
For more information, see page 33.
Pattern Dispatch
Select which methods to call based on their names.
$x = 0
class C
def my_first_method
$x += 1
end
def my_second_method
$x += 2
end
end
obj = C.new
obj.methods.each do |m|
obj.send(m) if m.to_s =~ /^my_/
end
C LICK H ERE to purchase this book now.
266
T HE S PELLS
$x # => 3
For more information, see page 69.
Sandbox
Execute untrusted code in a safe environment.
def sandbox(&code)
proc {
$SAFE = 2
yield
}.call
end
begin
sandbox { File.delete 'a_file' }
rescue Exception => ex
# => #<SecurityError: Insecure operation `delete' at level 2>
ex
end
For more information, see page 174.
Scope Gate
Isolate a scope with the class, module, or def keyword.
a = 1
defined? a # => "local-variable"
module MyModule
b = 1
defined? a # => nil
defined? b # => "local-variable"
end
defined? a
defined? b
# => "local-variable"
# => nil
For more information, see page 102.
Self Yield
Pass self to the current block.
class Person
attr_accessor :name, :surname
def initialize
yield self
end
end
C LICK H ERE to purchase this book now.
267
T HE S PELLS
joe = Person.new do |p|
p.name = 'Joe'
p.surname = 'Smith'
end
For more information, see page 250.
Shared Scope
Share variables among multiple contexts in the same Flat Scope (105).
lambda {
shared = 10
self.class.class_eval do
define_method :counter do
shared
end
define_method :down do
shared -= 1
end
end
}.call
counter
3.times { down }
counter
# => 10
# => 7
For more information, see page 106.
Singleton Method
Define a method on a single object.
obj = "abc"
class << obj
def my_singleton_method
"x"
end
end
obj.my_singleton_method # => "x"
For more information, see page 135.
String of Code
Evaluate a string of Ruby code.
my_string_of_code = "1 + 1"
eval(my_string_of_code) # => 2
C LICK H ERE to purchase this book now.
268
T HE S PELLS
For more information, see page 165.
Symbol To Proc
Convert a symbol to a block that calls a single method.
[1, 2, 3, 4].map(&:even?)
# => [false, true, false, true]
For more information, see page 253.
C LICK H ERE to purchase this book now.
269
The Pragmatic Bookshelf
The Pragmatic Bookshelf features books written by developers for developers. The titles
continue the well-known Pragmatic Programmer style and continue to garner awards and
rave reviews. As development gets more and more difficult, the Pragmatic Programmers
will be there with more titles and products to help you stay on top of your game.
Visit Us Online
Metaprogramming Ruby’s Home Page
http://pragprog.com/titles/ppmetr
Source code from this book, errata, and other resources. Come give us feedback, too!
Register for Updates
http://pragprog.com/updates
Be notified when updates and new books become available.
Join the Community
http://pragprog.com/community
Read our weblogs, join our online discussions, participate in our mailing list, interact
with our wiki, and benefit from the experience of other Pragmatic Programmers.
New and Noteworthy
http://pragprog.com/news
Check out the latest pragmatic developments, new titles and other offerings.
Buy the Book
If you liked this eBook, perhaps you’d like to have a paper copy of the book. It’s available
for purchase at our store: pragprog.com/titles/ppmetr.
Contact Us
Online Orders:
Customer Service:
Non-English Versions:
Pragmatic Teaching:
Author Proposals:
Contact us:
www.pragprog.com/catalog
support@pragprog.com
translations@pragprog.com
academic@pragprog.com
proposals@pragprog.com
1-800-699-PROG (+1 919 847 3884)

分享到:
评论

相关推荐

    Ruby元编程 源代码 Metaprogramming Ruby source code

    这本《Metaprogramming Ruby》书籍深入探讨了如何利用Ruby的特性进行元编程,帮助开发者提升代码的灵活性、可扩展性和复用性。源代码提供了书中各个示例的实践,让读者能够更好地理解元编程的概念。 元编程的核心...

    Metaprogramming.Ruby

    ### Metaprogramming Ruby:掌握如专业人士般的编程技巧 #### 一、引言 《Metaprogramming Ruby: Programming Like the Ruby Pros》是一本深入探讨Ruby元编程技术的专业书籍,作者Paolo Perrotta通过丰富的实例和...

    patch_def_ruby_Metaprogramming_Before_

    "patch_def_ruby_Metaprogramming_Before_" 的标题暗示了我们将在讨论如何利用元编程技术来添加在方法之前的钩子,比如 `def_before`。这种技术常用于在原有方法执行之前插入额外的功能,例如日志记录、验证或其他...

    label_break_ruby_Metaprogramming_

    在Ruby编程语言中,元编程(Metaprogramming)是一种强大的特性,允许程序在运行时检查、修改甚至创建自身的结构和行为。元编程能够增加代码的灵活性和可扩展性,但同时也需要谨慎使用,以避免过度复杂化和降低代码...

    Metaprogramming Ruby 2(Pragmatic,2014)

    Dig under the surface and explore Ruby's most advanced feature: a collection of techniques and tricks known as metaprogramming. In this book, you'll learn metaprogramming as an essential component of ...

    Ruby元编程技术详解(Ruby Metaprogramming techniques)

    Ruby元编程是一种强大的编程技术,它允许程序员在运行时修改和扩展程序的行为。元编程在Ruby中被广泛使用,因为Ruby的语言设计鼓励动态性和灵活性。以下是一些关于Ruby元编程的关键知识点: 1. 单例类(Singleton ...

    Metaprogramming Ruby 2nd Edition ruby元编程

    ### Metaprogramming Ruby 2nd Edition:深入理解Ruby元编程 #### 一、书籍简介与价值 《Metaprogramming Ruby 2nd Edition》是一本深入探讨Ruby语言元编程特性的经典之作。本书不仅适合那些希望深入了解Ruby内部...

    Metaprogramming Ruby

    ### Metaprogramming Ruby:深入理解动态编程的力量 #### 标题解读 “Metaprogramming Ruby”这一标题明确地指出了本书的核心内容——通过元编程技术深入探索Ruby语言的独特魅力。元编程(Metaprogramming)是一种...

    Metaprogramming ruby

    《Metaprogramming Ruby》是一本专注于Ruby编程语言元编程技术的书籍,由Paolo Perrotta撰写。元编程是一种编程范式,它允许在程序运行时修改或创建程序结构和行为。在Ruby中,元编程是其核心特性之一,使得代码能够...

    MetaprogrammingRuby2ndEditionFreePdfBook.pdf 英文原版

    Metaprogramming Ruby 2nd Edition – FreePdfBook

    Metaprogramming Ruby(Second Edition)

    《Metaprogramming Ruby(Second Edition)》是Ruby编程语言领域的一本经典著作,由Peter Eberhardt和Paolo Perrotta共同撰写。这本书详细介绍了如何利用Ruby的强大元编程特性来提升代码的灵活性、可扩展性和简洁性。...

    MetaProgramming in Ruby系列教程的中译版

    MetaProgramming in Ruby系列教程的中译版。 uby是动态的、魔幻而有趣的。而元编程(Metaprogramming)技术在Ruby中的应用也让我大开眼界,虽然以前也有浅显地介绍反射机制(Reflection),但我仍然觉得才疏学浅,不...

    Pragmatic.Metaprogramming.Ruby.Feb.2010.rar

    Pragmatic.Metaprogramming.Ruby.Feb.2010.rar

    MetaprogrammingRubyFreePdfBook.pdf 英文原版

    Metaprogramming Ruby – FreePdfBook

    Ruby Under a Microscope

    ruby interpreter 原理探討 At first glance, learning how to ... using metaprogramming, Ruby programs can inspect and change themselves. Beneath this thin veneer of simplicity, Ruby is a very complex tool.

    Server Metaprogramming Ruby-Pyton-Groovy-Haskell-Erlang.pdf

    总之,《Server Metaprogramming Ruby-Pyton-Groovy-Haskell-Erlang.pdf》这本书不仅介绍了元编程的基本概念,还深入探讨了五种具有代表性的动态语言在服务器端元编程方面的应用,并通过《Beyond Java》一书提供了对...

    Metaprogramming Elixir(Pragmatic,2015)

    Metaprogramming is one of Elixir's greatest features. Maybe you've played with the basics or written a few macros. Now you want to take it to the next level. This book is a guided series of ...

    ruby books

    进阶读者可以阅读《Programming Ruby》(又名"The Pickaxe Book”),这是Ruby的权威参考,或者《Metaprogramming Ruby》来探索Ruby的元编程特性。而对于Web开发,经典之作《Agile Web Development with Rails》是...

Global site tag (gtag.js) - Google Analytics