Almost Sinatra in Six Lines of Ruby - A Breakdown

A collection of awesome links. A single README page on GitHub. Contributions welcome. Send in a pull request etc.

Sinatra refactored, only six lines now. A hack by Konstantin Haase.

Performance. It’s just amazing. No code is faster than no code.

Until programmers stop acting like obfuscation is morally hazardous, they’re not artists, just kids who don’t want their food to touch. – _why

Almost Sinatra - A Breakdown - Line 4/6

Line 4:

Tilt.mappings.map{|k,v|D.(k){|n,*o|$t||=(h=$u._jisx0301("hash, please");File.read(caller[0][/^[^:]+/]).scan(S){|a,b|h[a]=b};h);v[0].new(*o){n=="#{n}"?n:$t[n.to_s]}.render(a,o[0].try(:[],:locals)||{})}}

Breakdown:

Tilt.mappings.each do |ext, engines| # rename k to ext and v to engines define_method ext do |text, *args| # rename n to text and o to args template = engines[0].new(*args) do text end locals = (args[0].respond_to?(:[]) ? args[0][:locals] : nil) || {} # was o[0].try(:[],:locals)||{} template.render( app, locals ) end end

Commentary: Almost Sinatra will define a method for every format so you can use, for example:

markdown "Strong emphasis, aka bold, with **asterisks** or __underscores__."

or