var = #{message}
in my example I get undefined local variable or method message
First, let's review what you seem to know:
- ...
.#{...}
markup to interpolate Ruby code inside a filter.You say you want to run each
, but presumably you want output from this; since the result of #{...}
is turned into a string and put in your code, what you really want (probably) is map
:
%html
%head
:javascript
var foo = [];
#{
limit = rand(4)+3
array = (0..limit).to_a
array.map{ |i| "foo[#{i}] = #{rand(12)};" }.join ' '
}
console.log(foo.length);
%body
Running the above code gives this output:
As you can see, the big #{...}
block (which may span multiple lines) runs arbitrary Ruby code. The result of the last expression (in this case the map{...}.join
) is converted to a string and placed in the output.