Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

much progress on the ruby part of the ruby macros.

  • Loading branch information...
commit ee671ee41933dc5597410b68e9bf016cbe6cedae 1 parent 988e57e
Dave Doolin doolin authored
2  source/content/_introduction.html.md
Source Rendered
@@ -99,6 +99,6 @@ in either the Lisp camp or the C/C++ camp, you will
99 99 find some valuable information here.
100 100
101 101
102   -## To be continued...
  102 +## We're gonna talk about Ruby next... (stay tuned)
103 103
104 104
105 source/content/_ruby.html.md
Source Rendered
... ... @@ -1,5 +1,5 @@
1 1
2   -### The state of the Ruby
  2 +### What Ruby calls macros
3 3
4 4 From the Pickaxe (4th ed., p. 378), we have macros defined as
5 5 Ruby methods which generate code "behind the scenes" by
@@ -8,3 +8,106 @@ turning small bits of code into larger bits of code.
8 8 (Add more bits here, briefly summarizing without going into much
9 9 detail.)
10 10
  11 +The simplest Ruby "macro", a sort of string substitution:
  12 +
  13 +
  14 +<pre class="brush:ruby">
  15 +def string_sub
  16 + "String you want to display"
  17 +end
  18 +
  19 +string_sub # => "String you want to display"
  20 +</pre>
  21 +
  22 +Now, Caleb is giving a me a rash about this, and he's right: this is *not really*
  23 +string substitution, but it *behaves* like string substitution. Is this merely
  24 +a semantic quibble? Yes and no, but resolution will have to wait for later.
  25 +
  26 +
  27 +#### An implementation of `attr_reader` in pure Ruby
  28 +
  29 +The `attr_reader` function should be familiar to everyone with Ruby programming experience.
  30 +Implementing `attr_reader` is a perfect example of what are viewed as macros in
  31 +Ruby, which are implemented using Ruby's metaprogramming capabilities.
  32 +
  33 +<pre class="brush:ruby">
  34 +class Module
  35 +
  36 + # Define our macro here...
  37 + def rm_attr_reader(name)
  38 + define_method(name) do
  39 + instance_variable_get("@#{name}")
  40 + end
  41 + end
  42 +
  43 +end
  44 +
  45 +class Foo
  46 + # And call our macro here...
  47 + rm_attr_reader :bar
  48 + def initialize(arg)
  49 + @bar = arg
  50 + end
  51 +end
  52 +
  53 +f = Foo.new "quux"
  54 +f.bar # => "quux"
  55 +</pre>
  56 +
  57 +You can type all the above into `irb` or put in a file an load it
  58 +into `irb` or however you like to execute your Ruby.
  59 +
  60 +
  61 +#### C Preprocessor (CPP) version
  62 +
  63 +<pre class="brush:ruby">
  64 +#define RM_ATTR_READER(X) def X; @##X end
  65 +
  66 +class Foo
  67 + RM_ATTR_READER(bar)
  68 + def initialize(arg)
  69 + @bar = arg
  70 + end
  71 +end
  72 +
  73 +f = Foo.new "quux"
  74 +p f.bar
  75 +</pre>
  76 +
  77 +One limitation of this technique is that Ruby comments are "preprocessed" as
  78 +well... except that outside of serendipitous use of predefined CPP directives,
  79 +won't parse. But even if it did parse, the odds of having it do what you want
  80 +are pretty low. Like, zero low.
  81 +
  82 +One could, one supposes, use `c/c++` commenting in lieue of Ruby commenting.
  83 +Which smacks of jumping from frying pan to fire, to stretch a metaphor.
  84 +
  85 +<pre class="brush:bash">
  86 +$ gcc -x c -E cpp_attr_reader.rb | ruby
  87 +cpp_attr_reader.rb:5:1: error: pasting "@" and "bar" does not give a valid preprocessing token
  88 +"quux"
  89 +</pre>
  90 +
  91 +#### Eval version
  92 +<pre class="brush:ruby">
  93 +class Module
  94 + def rm_attr_reader name
  95 + eval "
  96 + def #{name}
  97 + @#{name}
  98 + end
  99 + "
  100 + end
  101 +end
  102 +</pre>
  103 +
  104 +#### Parse subs
  105 +<pre class="brush:ruby">
  106 +macro rm_attr_reader name
  107 + :(
  108 + def ^name
  109 + ^(RedParse::VarNode["@"+name])
  110 + end
  111 + )
  112 +end
  113 +</pre>
2  source/index.html.haml
@@ -4,7 +4,7 @@
4 4 %div.subtitle
5 5 (a work in progress)
6 6 = partial 'content/introduction'
7   --#= partial 'content/ruby/'
  7 += partial 'content/ruby/'
8 8 -#= partial 'content/string_substitution'
9 9 -#= partial 'content/parse_tree'
10 10 -#= partial 'content/examples'
3  source/layout.haml
@@ -14,6 +14,9 @@
14 14 %script{:src => "https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js", :type => "text/javascript"}
15 15 %script{:src => "/javascripts/syntaxhighlighter/scripts/shCore.js"}
16 16 %script{:src => "/javascripts/syntaxhighlighter/scripts/shBrushClojure.js"}
  17 + %script{:src => "/javascripts/syntaxhighlighter/scripts/shBrushCpp.js"}
  18 + %script{:src => "/javascripts/syntaxhighlighter/scripts/shBrushJScript.js"}
  19 + %script{:src => "/javascripts/syntaxhighlighter/scripts/shBrushBash.js"}
17 20 <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushRuby.js' type='text/javascript'></script>
18 21 <script>SyntaxHighlighter.all();</script>
19 22 %script{:src => "http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML", :type => "text/javascript"}

0 comments on commit ee671ee

Please sign in to comment.
Something went wrong with that request. Please try again.