<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>patnakajima.com: Snippets</title>
    <description>Aggregating and aggravating since 1986.</description>
    <link>http://blog.patnakajima.com/posts.rss</link>
    <item>
      <title>Manic Meta Madness</title>
      <description>&lt;pre&gt;class Object
  # Search an object's metaclass when the method doesn't exist.
  def method_missing(m, *args); metaclass.send(m, *args) end
  def metaclass; class &lt;&lt; self; self end end
  def meta_eval(&amp;b); metaclass.instance_eval(&amp;b) end
  def meta_depth(i, start=0)
    return -1 if (self === Object)
    return start if i === self
    meta_depth(i.superclass, start + 1)
  end
end

class Dude
  def greet; puts "Hello from the instance\n\n"; end
end

d = Dude.new

d.greet # =&gt; Hello from the instance

Dude.class_eval { remove_method :greet }

d.instance_eval do
  puts "Meta depth: #{meta_depth(d)}" # =&gt; Meta depth: 0
  def greet; puts "Hello from the metaclass\n\n"; end
end

d.greet # =&gt; Hello from the metaclass

d.instance_eval do
  meta_eval do
    puts "Meta depth: #{meta_depth(d)}" # =&gt; Meta depth: 1
    remove_method(:greet)
    def greet; puts "Hello from the metaclass' metaclass\n\n"; end
  end
end

d.greet # =&gt; Hello from the metaclass' metaclass

d.instance_eval do
  meta_eval do
    meta_eval do
      puts "Meta depth: #{meta_depth(d)}" # =&gt; Meta depth: 2
      remove_method(:greet)
      def greet
        puts "Hello from the metaclass' metaclass' metaclass\n\n"
      end
    end
  end
end

d.greet # =&gt; Hello from the metaclass' metaclass' metaclass

d.instance_eval do
  meta_eval do
    meta_eval do
      meta_eval do
        puts "Meta depth: #{meta_depth(d)}" # =&gt; Meta depth: 2
        remove_method(:greet)
        def greet
          puts "Hello from the metaclass' metaclass' metaclass' metaclass"
        end
      end
    end
  end
end

d.greet # =&gt; Hello from the metaclass' metaclass' metaclass' metaclass&lt;/pre&gt;</description>
      <author>Pat Nakajima</author>
      <guid>/snippets/manic-meta-madness</guid>
      <pubDate>Thu, 17 Jul 2008 20:53:34 +0000</pubDate>
      <link>http://blog.patnakajima.com/snippets/manic-meta-madness</link>
    </item>
    <item>
      <title>Scheme Spirit</title>
      <description>&lt;pre&gt;(define (accumulate proc initial sequence)
  (cond
    ((null? sequence) initial)
    ((proc 
      (car sequence)
      (accumulate proc initial (cdr sequence))))))

(define (map proc sequence)
  (accumulate 
    (lambda (x y) (cons (proc x) y)) 
    '()
    sequence))

(define (filter predicate sequence)
  (cond
    ((null? sequence) '())
    ((predicate (car sequence)) 
      (cons (car sequence)
        (filter predicate (cdr sequence))))
    (else
      (filter predicate (cdr sequence)))))&lt;/pre&gt;</description>
      <author>Pat Nakajima</author>
      <guid>/snippets/scheme-spirit</guid>
      <pubDate>Thu, 17 Jul 2008 05:02:52 +0000</pubDate>
      <link>http://blog.patnakajima.com/snippets/scheme-spirit</link>
    </item>
    <item>
      <title>methodize_keys! with recursion</title>
      <description>&lt;pre&gt;class Object
  def try(m, *a, &amp;b); respond_to?(m) ? send(m, *a, &amp;b) : nil; end
  def metaclass; class &lt;&lt; self; self; end; end
  def meta_eval &amp;b; metaclass.instance_eval &amp;b; end
  def meta_def name, &amp;b; meta_eval { define_method name, &amp;b }; end
end

module Enumerable
  def methodize_keys!    
    case self
    when Hash
      each do |key, value|
        meta_def(key) { self[key] }
        value.try(:methodize_keys!)
      end
    when Array
      each { |n| n.try(:methodize_keys!) }
    end
  end
end&lt;/pre&gt;</description>
      <author>Pat Nakajima</author>
      <guid>/snippets/methodize_keys-with-recursion</guid>
      <pubDate>Mon, 02 Jun 2008 16:41:29 +0000</pubDate>
      <link>http://blog.patnakajima.com/snippets/methodize_keys-with-recursion</link>
    </item>
    <item>
      <title>ruby-snippet</title>
      <description>&lt;pre&gt;# Create getter methods for every key-value in a hash. Example:

=begin # Example:

some_hash = {
  :name =&gt; 'Pat',
  :age =&gt; 21,
  :tired? =&gt; true
}

puts some_hash.respond_to?(:name) # =&gt; false
puts some_hash.respond_to?(:age) # =&gt; false
puts some_hash.respond_to?(:tired?) # =&gt; false

some_hash.methodize_keys!

puts some_hash.name # =&gt; Pat
puts some_hash.age # =&gt; 21
puts some_hash.tired? # =&gt; true

# This is what my way has over the method_missing approach:
puts some_hash.respond_to?(:name) # =&gt; true
puts some_hash.respond_to?(:age) # =&gt; true
puts some_hash.respond_to?(:tired?) # =&gt; true
  
=end

# First we need an easy way to grab an object's metaclass.
#
# gotta love why:
# whytheluckystiff.net/articles/seeingMetaclassesClearly.html
class Object
  def metaclass; class &lt;&lt; self; self; end; end
  def meta_eval &amp;blk; metaclass.instance_eval &amp;blk; end
end

# Then comes the easy part. There are probably a thousand other
# ways to do this, but I like this one.
class Hash
  def methodize_keys!
    each do |key, value|
      meta_eval { define_method(key) { return self[key] } }
    end    
  end
end

&lt;/pre&gt;</description>
      <author>Pat Nakajima</author>
      <guid>/snippets/ruby-snippet-2</guid>
      <pubDate>Thu, 22 May 2008 01:16:56 +0000</pubDate>
      <link>http://blog.patnakajima.com/snippets/ruby-snippet-2</link>
    </item>
    <item>
      <title>javascript-snippet</title>
      <description>&lt;pre&gt;// In Javascript
var Greeter = function(name) {
  name = name || 'World';
  alert("Hello, " + name + '!');
};

new Greeter; // =&gt; "Hello, World!&lt;/pre&gt;</description>
      <author>Pat Nakajima</author>
      <guid>/snippets/javascript-snippet</guid>
      <pubDate>Wed, 21 May 2008 04:09:39 +0000</pubDate>
      <link>http://blog.patnakajima.com/snippets/javascript-snippet</link>
    </item>
    <item>
      <title>ruby-snippet</title>
      <description>&lt;pre&gt;# In Ruby...
class Greeter
  def initialize(name='World')
    puts "Hello, #{name}!"
  end
end

Greeter.new # =&gt; Hello, World!&lt;/pre&gt;</description>
      <author>Pat Nakajima</author>
      <guid>/snippets/ruby-snippet</guid>
      <pubDate>Wed, 21 May 2008 03:52:39 +0000</pubDate>
      <link>http://blog.patnakajima.com/snippets/ruby-snippet</link>
    </item>
  </channel>
</rss>
