0.0
No commit activity in last 3 years
No release in over 3 years
A Gem to Rewrite Ruby code from a special syntax into an evented IO form.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 1.0.0.rc.6
~> 2.0.0.beta.22

Runtime

~> 3.0.6
~> 1.2.5
 Project Readme

What is this for?¶ ↑

This gem tries to solve a very specific problem when working with evented I/O code. Ideally, I suppose it would work with something like EventMachine, but I havent worked enough with EventMachine to know if that’s the case. Its meant for cases where blocks are passed to methods solely to sequence a set of steps. Like in this example.

writeFile(path, "hello") do
    File.open(path, "a") do |file|
        write(file, "\nworld") do
            close(file) do
                read(path) do |data|
                    puts data.last
                end
            end
        end
    end
end

Traditionally blocks have been a lightweight strategy pattern. Not so with evented I/O. While each function takes a block, this is merely to sequence the rest of the program to execute after the function is executed. To understand the context a little better read this

Installation¶ ↑

gem install step_rewrite

After installing the gem, the quick and dirty way to use it is to include the StepRewrite module in your code.

include StepRewrite
step do
    path = "fileio.txt"
    writeFile(path, "hello", &_)
    file = File.open(path, "a", &_)
    write(file, "\nworld", &_)
    close(file, &_)
    data = read(path, &_)
    puts data.last
end

The block passed to step is rewritten so that everywhere you have a method that takes &_ as a parameter, the rest of the code is passed as a block to it. So step rewrites this code into the code above and THEN evaluates it.

Rewrite Rules¶ ↑

Any code of the form

foo(&_)
bar
baz

gets rewritten to

foo do
  bar
  baz
end

return values from such functions are converted into arguments to the block

a = foo(&_)
bar(a)
baz

becomes

foo do |a|
  bar(a)
  baz
end

This allows an infinite degree of nesting

foo(&_)
bar(&_)
baz(&_)
qux

becomes

foo { bar { baz { qux } } }

existing blocks or lambda parameters are not touched.

Other execution methods¶ ↑

If the name step clashes with a method in your code, you can instead use the module method

StepRewrite.step do
    path = "fileio.txt"
    writeFile(path, "hello", &_)
    file = File.open(path, "a", &_)
    write(file, "\nworld", &_)
    close(file, &_)
    data = read(path, &_)
    puts data.last
end

if _ is a variable you are already using, you can configure the special callback identifier for rewriting.

StepRewrite.step(:cb) do
    path = "fileio.txt"
    writeFile(path, "hello", &cb)
    file = File.open(path, "a", &cb)
    write(file, "\nworld", &cb)
    close(file, &cb)
    data = read(path, &cb)
    puts data.last
end

Define Method¶ ↑

The problem with the above approach is that it does not cache the result of rewriting the code. If what you are writing is part of a long running server, the recommended style is to define a method instead.

require 'step_rewrite/module'

class IO
    define_step_method :hello_world do
        path = "fileio.txt"
        writeFile(path, "hello", &_)
        file = File.open(path, "a", &_)
        write(file, "\nworld", &_)
        close(file, &_)
        data = read(path, &_)
        puts data.last
    end
end

IO.new.hello_world

How does this work?¶ ↑

This gem makes use of the ParseTree gem to extract the sexp out of any block and then rewrites it using various rules. This code is converted back into Ruby using Ruby2Ruby and then evaled into a context

Copyright © 2010 Vishnu Iyengar. See LICENSE for details.