Ruby Metaprogamming Cheat Sheet (By Example)

Reviews
Shared by: armani11
Stats
views:
49
rating:
8(1)
reviews:
0
posted:
11/9/2008
language:
English
pages:
0
Ruby Metaprogamming Cheat Sheet (By Example) Introspection Listing Methods List the String type’s class methods, sorted: ‣ String.methods.sort ["<", "<=", "<=>", ..., "to_a", "to_s", ...] List the String type’s public/protected/private instance methods, but not the inherited methods: ‣ (“string”.public_methods Object.public_instance_methods).sort ["%", "*", "+", "<", "<<", "<=", "<=>", ...] List the String type’s public/private class methods whose name contains the word “method”: ‣ String.public_methods.grep(/method/) ["private_class_method", "method", ...] ‣ (“string”.protected_methods - Object. protected_instance_methods).sort [] ‣ (“string”.private_methods Object.private_instance_methods).sort [] ‣ String.private_methods.grep(/method/) ["singleton_method_added", ...] Finding Methods Get the String type’s class method named ancestors: ‣ String.method(:ancestors) # Three different ways to list the String type’s instance methods that start with “to”: ‣ String.instance_methods.grep(/^to/) ‣ String.new.methods.grep(/^to/) ‣ “string”.methods.grep(/^to/) ["to_str", "to_i", "to_f", "to_a", "to_s", ...] Get the String type’s instance method named gsub: ‣ “a string”.method(:gsub) # List the String type’s public instance methods, but not the inherited methods 1: ‣ (String.instance_methods Object.instance_methods).sort ["%", "*", "+", "<", "<<", "<=", "<=>", ...] Getting and Setting Attribute Values Get and set a class attribute cattr in class Foo. ‣ class Foo ‣ @@cattr = “cattr” ‣ end 1 Actually, the list includes Comparable and Enumerable methods, modules which String includes. 1/5 (c) 2007, Object Mentor, Inc., Creative Commons License Ruby Metaprogamming Cheat Sheet (By Example) ‣ Foo.class_eval do ‣ ‣ v = class_variable_get(:@@cattr) class_variable_set(:@@cattr, “foo”) “foo” Print all modules in the current runtime: ‣ ObjectSpace.each_object(Module) {|c| p c} IRB::Context ... ‣ end Get and set an instance attribute attr in class Foo. ‣ class Foo ‣ ‣ ‣ ‣ end ‣ Foo.new.instance_eval do ‣ ‣ v = instance_variable_get(:@attr) instance_variable_set(:@attr, “bar”) “bar” Get the String type’s parent types (classes and modules): ‣ String.method(:ancestors) # def initialize @attr = “attr” end Finding Objects Print all instances of type Integer in the current runtime: ‣ ObjectSpace.each_object(Integer) {|i| p i} 9223372036854775807 ... Manipulating “Stuff” Introducing New Elements Include the Enumerable module in type ThreeIntegers: ‣ class ThreeIntegers ‣ ‣ include Enumerable def each; ...; end ‣ end Finding Types Get the list of top-level classes: ‣ Class.constants.find_all do |x| ‣ Class.const_get(x).class==Class ["TrueClass", "SecurityError", "Array", ...] ‣ end ‣ ThreeIntegers.new.each {|i| i*2} [0, 2, 4] ‣ end Print all classes in the current runtime: ‣ ObjectSpace.each_object(Class) {|c| p c} IRB::Context ... Add a new instance method hello to type Foo: ‣ class Foo ‣ ‣ def hello *args “Hello world: #{args.inspect}” (c) 2007, Object Mentor, Inc., Creative Commons License 2/5 Ruby Metaprogamming Cheat Sheet (By Example) ‣ end ‣ Foo.good_night :d1, :d2 “Good night: [:d1, :d2]” ‣ end ‣ Foo.new.hello :a1, :a2 “Hello world: [:a1, :a2]” Method Wrapping Alias an existing instance method hello, redefine the method, and delegate to the old method in type Foo: ‣ class Foo ‣ ‣ ‣ ‣ ‣ end ‣ Foo.new.hello :e1, :e2 “{{{Hello world: [:e1, :e2]}}}” Add a new instance method good_bye to an instance foo1 of Foo: ‣ foo1=Foo.new ‣ def foo1.good_bye *args ‣ “Good bye: #{args.inspect}” ‣ end ‣ foo1.good_bye :b1, :b2 “Good bye: [:b1, :b2]” alias_method :hello2, :hello def hello *args “{{{” + hello2(*args) + “}}}” end Add a new instance method greetings to an instance foo1 of Foo, using the singleton class for foo1. ‣ foo1=Foo.new ‣ class << foo1 ‣ ‣ ‣ ‣ end ‣ foo1.greetings :c1, :c2 “Greetings: [:c1, :c2]” Alias an existing class method doit, redefine the method, and delegate to the old method in type Foo: ‣ Foo.class_eval do ‣ ‣ ‣ ‣ ‣ ‣ ‣ end ‣ Foo.good_night :f1, :f2 “<<>>” def greetings *args “Greetings: #{args.inspect}” end class << self alias_method :good_night2, :good_night def good_night *args “<<<” + good_night2(*args) + “>>>” end end Add a new class method good_night to type Foo: ‣ def Foo.good_night *args ‣ “Good night: #{args.inspect}” ‣ end (c) 2007, Object Mentor, Inc., Creative Commons License 3/5 Ruby Metaprogamming Cheat Sheet (By Example) Interpreting Messages to Objects Using method_missing to Handle “Missing” Methods Dynamically handle any unknown message sent to Echo; print the message name followed by the argument list. ‣ class Echo ‣ ‣ ‣ ‣ end ‣ Echo.new.yell “Hello”, “world!” ‣ Echo.new.say “Good”, “bye!” “yell: [\”Hello\”, \”world!\”]” “yell: [\”Good\”, \”Bye!\”]” ‣ ‣ ‣ ‣ ‣ end end EOF send(method_sym, *args) end ‣ Echo.new.yell "Hello", "world!" ‣ Echo.new.yell "good", "bye" “defining method yell” “yell: [\”Hello\”, \”world!\”]” “yell: [\”Good\”, \”bye!\”]” def method_missing method_sym, *args p “#{method_sym}: #{args.inspect}” end Add an instance method for a single object of type Echo to the singleton class of the object. The name of the method is the value of method_name. ‣ echo = Echo.new ‣ method_name = “new_method” ‣ sing = class << echo; self; end ‣ sing.class_eval <<-EOF ‣ ‣ ‣ ‣ end nil Evaluating Strings as Code Add New Methods on Demand Dynamically add a method for any unknown message sent to Echo. ‣ class Echo ‣ ‣ ‣ ‣ ‣ ‣ def method_missing method_sym, *args p "defining method #{method_sym}" eval <<-EOF def #{method_sym.to_s} *args p "#{method_sym}: " + args.inspect def #{method_name} *args p "#{method_name}: " + args.inspect ‣ EOF Add an instance method for a single object of type Echo to the singleton class of the object. The name of the method is the value of method_name. (Alternative approach) (c) 2007, Object Mentor, Inc., Creative Commons License 4/5 Ruby Metaprogamming Cheat Sheet (By Example) ‣ echo = Echo.new ‣ method_name = “new_method” ‣ echo.instance_eval <<-EOF ‣ ‣ ‣ ‣ end nil def #{method_name} *args p "#{method_name}: " + args.inspect ‣ EOF Add an instance method to type Echo whose name is the value of method_name. It can be invoked by any instance of Echo. ‣ Echo.class_eval <<-EOF ‣ ‣ ‣ ‣ end nil def #{method_name} *args p "#{method_name}: " + args.inspect ‣ EOF Assorted References • Ruby for Rails, David Black, Manning. • http://ola-bini.blogspot.com/2006/09/ruby-metaprogramming-techni ques.html • http://www.vanderburg.org/Speaking/Stuff/oscon05.pdf • http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html • http://poignantguide.net/dwemthy/ • http://weblog.jamisbuck.org/2006/4/20/writing-domain-specific-lang uages • http://rubyquiz.com/metakoans.rb (c) 2007, Object Mentor, Inc., Creative Commons License 5/5

Related docs
Ruby on Rails Cheat Sheet
Views: 711  |  Downloads: 59
Ruby Rails Cheat Sheet and Links
Views: 284  |  Downloads: 10
Ruby on Rails Cheat Sheet
Views: 65  |  Downloads: 6
ruby-on-rails-cheat-sheet-v1
Views: 29  |  Downloads: 2
The Cheat Sheet
Views: 6  |  Downloads: 0
CHEAT SHEET
Views: 6  |  Downloads: 0
Ruby on Rails : active record cheat sheet
Views: 51  |  Downloads: 4
Washington DC Metro Cheat Sheet
Views: 68  |  Downloads: 0
Quick SQL Server Cheat Sheet
Views: 0  |  Downloads: 0
TEXTMATE cheat sheet
Views: 0  |  Downloads: 0
premium docs
Other docs by armani11
Corio Inc Ammendments and By laws
Views: 240  |  Downloads: 0
Form 4972 Tax on Lump-Sum Distributions
Views: 310  |  Downloads: 2
Ziddo Factsheet
Views: 517  |  Downloads: 0
pro-vehicle-mileage
Views: 235  |  Downloads: 14
Batmobile Front
Views: 545  |  Downloads: 6
Sample Nondisclosure agreement
Views: 619  |  Downloads: 19
schaefer-all
Views: 229  |  Downloads: 1
Employment Offer Letter Non Exempt Employee
Views: 497  |  Downloads: 10