How to properly evaluate an evaluated ERB template that contains Ruby code? -


say example have file erb template containing ruby code:

module <%= @name %>   class client     # ...   end end 

i want evaluate erb code, , want evaluate back.

@name = 'jimi' erb = erb.new('module <%= @name %> end') eval(erb.result) jimi.class   # => module 

i want have multiple template of such code in separate files (which guess have .rb.erb extension) not know how approach problem in way follows practice , works well. want use alternative complicated metaprogramming have done otherwise. alternative? of course problems lot more complicated, not simple dynamic module creation. there better way of evaluating result of evaluated erb file?

just sidenote, developing project relies heavily on metaprogramming @ moment has create modules, classes, , methods dynamically. named hendrix , working on version 1.0.0 want improve way doing generations , think templates such these way go.

erb strikes me bit of hack in situation. ruby has built-in string formatting if need simple substitution:

template = "module %{name} end" eval(template % {name: jimi}) jimi.class # => module 

see string#%.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -