`
magixyu
  • 浏览: 79453 次
  • 性别: Icon_minigender_1
  • 来自: 青岛
社区版块
存档分类
最新评论

Advanced Rails -- Ruby 基本技术(3) -- Ruby 基础2

    博客分类:
  • ROR
阅读更多
Metaid

why the lucky stiff 为Ruby的原编程创建了一个名字为metaid.rb的库。这段小程序可以包含在任何需要原编程的项目里,而且非常有用。
	class Object
	  # The hidden singleton lurks behind everyone
	  def metaclass; class << self; self; end; end
	  def meta_eval &blk; metaclass.instance_eval &blk; end

	  # Adds methods to a metaclass
	  def meta_def name, &blk
	    meta_eval { define_method name, &blk }
	  end

	  # Defines an instance method within a class
	  def class_def name, &blk
	    class_eval { define_method name, &blk }
	  end	
	end


上面的库给每一个object定义了四个方法:

metaclass
指向单类的接受者(self).
meta_eval
The equivalent of class_eval for singleton classes. Evaluates the given block in the context of the receiver's singleton class.
meta_def
Defines a method within the receiver's singleton class. If the receiver is a class or module, this will create a class method (instance method of the receiver's singleton class).
class_def
Defines an instance method in the receiver (which must be a class or module).
Metaid's convenience lies in its brevity. By using a shorthand for referring to and augmenting metaclasses, your code will become clearer rather than being littered with constructs like class << self; self; end. The shorter and more readable these techniques are, the more likely you are to use them appropriately in your programs.
This example shows how we can use Metaid to examine and simplify our singleton class hacking:
	class Person
	  def name; "Bob"; end
	  def self.species; "Homo sapiens"; end
	end


Class methods are added as instance methods of the singleton class:
	Person.instance_methods(false)            # => ["name"]
	Person.metaclass.instance_methods -
	  Object.metaclass.instance_methods       # => ["species"]

Using the methods from Metaid, we could have written the method definitions as:
Person.class_def(:name) { "Bob" }
Person.meta_def(:species) { "Homo sapiens" }
分享到:
评论

相关推荐

    Advanced Rails

    "Advanced Rails" 涵盖了Rails开发中的高级主题和技术,是Ruby on Rails学习进阶的重要资源,尤其适合已经对基础Rails有一定了解的开发者。 在Web开发领域,Rails以其高效、简洁的代码和“约定优于配置”的哲学吸引...

    rails2-sample

    从给定的文件信息来看,我们正在探讨的是一本关于Ruby on ...综上所述,《Simply Rails2》不仅为初学者提供了全面的Ruby on Rails入门指南,也为进阶开发者提供了深入的技术细节和实践案例,是一本值得推荐的学习资料。

    Ruby-LockboxRuby和Rails的文件加密

    Ruby-Lockbox是一款针对Ruby和Rails应用的安全库,主要用于实现文件和数据的加密。它提供了简单易用的接口,让开发者能够轻松地在应用程序中集成高级加密标准(AES)和其他密码学算法,确保敏感信息的安全性。 Ruby...

    Advanced Rails Recipes(随书源码)

    (Ruby Recipeswas is written for Rails 1.x,this book for Rails 2.0.) Ruby on Rails continues to build up a tremendous head of steam. Fueled by significant benefits and an impressive portfolio of real...

    Advanced Rails Recipes

    该书面向已经具备一定Rails开发基础的技术人员,提供了深入且实用的技术指导。 **描述:“Advanced Rails Recipes 正式版。不要资源分,免费提供,要下的赶快哦。。。”** 这段描述表明这本书是正式版本,并且作者...

    The Ruby Programming Language

    3. **面向对象**:Ruby是一种纯面向对象的语言,一切皆对象,连基本的数据类型都是对象。 4. **元编程能力**:Ruby具有强大的元编程能力,可以轻松地修改或扩展语言本身的功能。 5. **丰富的标准库**:Ruby拥有一个...

    The Ruby Programming Language 2008 .pdf

    - **Advanced Rails**:专注于Ruby on Rails框架的高级用法和技术细节。 - **Rails Cookbook**:类似于Ruby Cookbook,但专门针对Rails框架。 - **Ruby Pocket Reference**:一本便携式的参考手册,涵盖了Ruby语言的...

    Advanced Rails Recipes(英语清晰文字pdf+源码)

    (Ruby Recipeswas is written for Rails 1.x,this book for Rails 2.0.) Ruby on Rails continues to build up a tremendous head of steam. Fueled by significant benefits and an impressive portfolio of real...

    Ruby-服务器优化的Ruby发行版通过APTYUM实现更少内存更快速易于安装和安全补丁

    Ruby是一种强大的动态编程语言,广泛用于Web开发,尤其是在Ruby on Rails框架中。为了在服务器环境中更好地利用Ruby,一些专门针对服务器优化的发行版被开发出来,以提高性能、减少资源消耗并简化管理。"Ruby-服务器...

    Advanced.Rails(2007)].Brad.Ediger.文字版.pdf

    综上所述,《Advanced Rails》是一本深度解析Rails框架高级特性的书籍,不仅覆盖了技术层面的知识点,还提供了丰富的学习资源和支持。对于希望提升Rails技能的开发者来说,本书具有很高的参考价值。

    advanced rury on rails recipes

    从给定的文件信息来看,我们探讨的主题是“高级Ruby on Rails食谱”(Advanced Rails Recipes),这是一本旨在为专业Ruby on Rails(RoR)开发者提供深入指导的专业参考手册。尽管该书尚处于开发阶段,但其目标是...

    The Rails3 Way, 2nd Edition

    Ruby on Rails strips complexity from the development process, enabling professional developers to focus on what matters most: delivering business value via clean and maintainable code. The Rails™ 3 ...

    Advance RAils Reciples

    2. **高级编程技巧**:除了介绍Rails的基础知识外,《Advanced Rails Reciples》还深入探讨了Rails框架中的高级编程技巧,如性能优化、安全性增强等。 3. **实战案例分析**:书中通过多个实战案例来展示如何将Rails...

    Rails.Angular.Postgres.and.Bootstrap.2nd.Edition

    This book covers Postgres 9.5, Rails 5, and Ruby 2.3. You should have some experience with basic Rails concepts and a cursory understanding of JavaScript, CSS, and SQL, but by no means need to be an ...

Global site tag (gtag.js) - Google Analytics