I don’t know much Ruby, and probably won’t learn; all that syntax and magic scare me away. But I have to admit it has some darned useful gadgets. Here’s a python function I hacked up to do something much like Ruby’s expression-substitution, using the same #{ } syntax. It doesn’t allow curly braces inside the #{ }; were I a little less lazy I would put in some escaping.
import re import sys def esub(s): """ Perform Ruby-like expression substitution. >>> x=3 >>> y='A' >>> esub('abc#{x}def#{3+5}hij#{"".join([y, y])}') 'abc3def8hijAA' """ restr = r'(?:#{(?P[^{}]*)})|(?:[^#])+|#' fr = sys._getframe(1) def process(m): txt = m.group('exp') if txt is not None: val = eval(txt, fr.f_globals, fr.f_locals) return type(s)(val) else: return m.group() return ''.join(process(m) for m in re.finditer(restr, s))
Tags: Programming, python, ruby
March 1, 2008 at 10:10 am |
What’s about this:
def m(a):
def n():
return esub(‘#{a}’)
return n()
m(42)
your approach cannot deal with free variables in inner functions, unfortunately.
March 1, 2008 at 12:38 pm |
D’oh! Oh well.
A not-great workaround:
def m(a):
def n():
a
return esub(’#{a}’)
return n()
m(42)