ERB as a macro pre-processor?

Discussion in 'Ruby' started by Rich Morin, Oct 11, 2006.

  1. Rich Morin

    Rich Morin Guest

    "The Ruby Way" (pp. 43-45) shows a couple of different ways
    to generate methods dynamically (using eval). Looking at the
    examples, I began to wonder about different Ways To Do It.

    One possibility that occurred to me is to use ERB as a macro
    pre-processor. The book suggests the following code:

    if platform == Windows
    def my_action
    action1
    end
    if platform == Linux
    def my_action
    action2
    end
    else
    def my_action
    default_action
    end
    end

    Although this is approach is direct and simple, it seems like it
    wouldn't scale very well, given multiple variations. However, I
    think that the following approach might:


    % cat erb_ml
    #!/usr/bin/env ruby

    require 'erb'

    def action_D; print "default action\n"; end
    def action_L; print "Linux action\n"; end
    def action_W; print "Windows action\n"; end

    action = { :Linux => 'action_L',
    :Windows => 'action_W' }

    template = ERB.new <<-EOF
    def my_action
    <%= action[platform] || 'action_D' %>
    end
    EOF

    for platform in [ :Linux, :Windows, :plan9 ] do
    eval template.result(binding)
    print "#{platform.to_s}: "
    my_action
    end

    % erb_ml
    Linux: Linux action
    Windows: Windows action
    Plan9: default action


    Obviously, the "action" hash could be replaced by other
    code-generation and/or -retrieval methods. Is anyone
    here using ERB in this manner? Are there any caveats
    or alternative approaches that I should be aware of?

    -r
    --
    http://www.cfcl.com/rdm Rich Morin
    http://www.cfcl.com/rdm/resume
    http://www.cfcl.com/rdm/weblog +1 650-873-7841

    Technical editing and writing, programming, and web development
     
    Rich Morin, Oct 11, 2006
    #1
    1. Advertising

  2. On Wed, Oct 11, 2006 at 02:40:58PM +0900, Rich Morin wrote:
    > "The Ruby Way" (pp. 43-45) shows a couple of different ways
    > to generate methods dynamically (using eval). Looking at the
    > examples, I began to wonder about different Ways To Do It.
    >
    > One possibility that occurred to me is to use ERB as a macro
    > pre-processor. The book suggests the following code:
    >
    > if platform == Windows
    > def my_action
    > action1
    > end
    > if platform == Linux
    > def my_action
    > action2
    > end
    > else
    > def my_action
    > default_action
    > end
    > end
    >
    > Although this is approach is direct and simple, it seems like it
    > wouldn't scale very well, given multiple variations. However, I
    > think that the following approach might:
    >
    >
    > % cat erb_ml
    > #!/usr/bin/env ruby
    >
    > require 'erb'
    >
    > def action_D; print "default action\n"; end
    > def action_L; print "Linux action\n"; end
    > def action_W; print "Windows action\n"; end
    >
    > action = { :Linux => 'action_L',
    > :Windows => 'action_W' }
    >
    > template = ERB.new <<-EOF
    > def my_action
    > <%= action[platform] || 'action_D' %>
    > end
    > EOF
    >
    > for platform in [ :Linux, :Windows, :plan9 ] do
    > eval template.result(binding)
    > print "#{platform.to_s}: "
    > my_action
    > end
    >
    > % erb_ml
    > Linux: Linux action
    > Windows: Windows action
    > Plan9: default action
    >
    >
    > Obviously, the "action" hash could be replaced by other
    > code-generation and/or -retrieval methods. Is anyone
    > here using ERB in this manner? Are there any caveats
    > or alternative approaches that I should be aware of?

    Bah I say! How does that saying go, "Replace conditionals with
    polymorphism"?

    class Platform
    end

    class Linux < Platform
    def define_methods
    def a
    puts "linux"
    end
    end
    end

    class Windows < Platform
    def define_methods
    def a
    puts "windows"
    end
    end
    end

    class Platform
    def self.get_platform
    case RUBY_PLATFORM
    when /linux/
    Linux
    when /win32/
    Windows
    end
    end
    end

    platform = Platform.get_platform.new
    platform.define_methods
     
    Logan Capaldo, Oct 11, 2006
    #2
    1. Advertising

  3. Rich Morin

    Guest

    On Wed, 11 Oct 2006, Rich Morin wrote:

    > "The Ruby Way" (pp. 43-45) shows a couple of different ways
    > to generate methods dynamically (using eval). Looking at the
    > examples, I began to wonder about different Ways To Do It.

    <snip>
    > Obviously, the "action" hash could be replaced by other
    > code-generation and/or -retrieval methods. Is anyone
    > here using ERB in this manner? Are there any caveats
    > or alternative approaches that I should be aware of?


    http://groups.google.com/group/comp...050d88d5?lnk=gst&q=r4&rnum=1#d985a8bf050d88d5

    i did quite a bit more work on it but never released...

    cheers.

    -a
    --
    my religion is very simple. my religion is kindness. -- the dalai lama
     
    , Oct 11, 2006
    #3
  4. Rich Morin

    Rich Morin Guest

    At 11:36 PM +0900 10/11/06, wrote:
    > i did quite a bit more work on it but never released...


    interesting stuff; why not put out a WIP snapshot?

    Both r4 and ERB allow the use of arbitrary Ruby code. It
    would be interesting to see which features of each work
    better for what kinds of problems.

    -r
    --
    http://www.cfcl.com/rdm Rich Morin
    http://www.cfcl.com/rdm/resume
    http://www.cfcl.com/rdm/weblog +1 650-873-7841

    Technical editing and writing, programming, and web development
     
    Rich Morin, Oct 11, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ken Philips
    Replies:
    1
    Views:
    470
    Anthony Borla
    Feb 7, 2005
  2. brahatha
    Replies:
    1
    Views:
    684
  3. Guru
    Replies:
    15
    Views:
    632
    James Kanze
    Jul 4, 2010
  4. Steve V
    Replies:
    6
    Views:
    260
    Steve V
    Apr 20, 2005
  5. Dan Thomas

    link to .erb file, from .erb file

    Dan Thomas, Jan 26, 2011, in forum: Ruby
    Replies:
    4
    Views:
    188
    Dan Thomas
    Jan 26, 2011
Loading...

Share This Page