Skip to content

Instantly share code, notes, and snippets.

@boxmein
Created March 6, 2014 13:21
Show Gist options
  • Save boxmein/9389559 to your computer and use it in GitHub Desktop.
Save boxmein/9389559 to your computer and use it in GitHub Desktop.
A small Javascript function that extrapolates in-string code like Ruby does.
/*
extrapolate.js
==============
A small utility function that allows Ruby-like string extrapolation
via #{expr}. Relies on ES5's Array#forEach but is relatively easy
to convert not to use it.
...that's it, folks!
*/
String.prototype.extrapolate = function() {
var str = this;
this.match(/#\{(.+?)\}/g).forEach(function(each) {
str = str.replace(each, eval(each.slice(2, -1)));
});
return str.valueOf();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment