A
A
AlexWinner2011-08-26 11:40:10
ruby
AlexWinner, 2011-08-26 11:40:10

Ruby/ERB/puppet after comparison in if line becomes nil?

Greetings!
The following situation: I am writing an erb-template for pappet. The view is like this:

# XMX &lt;%= xmx%&gt;<br/>
# XMX.class &lt;%= xmx.class%&gt;<br/>
&lt;%<br/>
mem = 4096.0<br/>
%&gt;<br/>
# MEM &lt;%= mem%&gt;<br/>
# MEM.classs &lt;%= mem.class%&gt;<br/>
&lt;%<br/>
<br/>
if xmx == &quot;NONE&quot;<br/>
 xmx = (mem * 0.7).to_s<br/>
 %&gt;XMX in if &lt;%= xmx %&gt; &lt;%<br/>
else<br/>
 %&gt;XMX in else &lt;%= xmx %&gt; &lt;%<br/>
end<br/>
%&gt;<br/>
# MEM &lt;%= mem%&gt;<br/>
# MEM.class &lt;%= mem.class%&gt;<br/>
# XMX &lt;%= xmx%&gt;<br/>
# XMX.class &lt;%= xmx.class%&gt;<br/>

If, where the template is used, define xmx = "NONE", then everything is OK, at the output we get:
# XMX NONE<br/>
# XMX.class String<br/>
<br/>
# MEM 4096.0<br/>
# MEM.classs Float<br/>
XMX in if 2867.2<br/>
# MEM 4096.0<br/>
# MEM.class Float<br/>
# XMX 2867.2<br/>
# XMX.class String

If we define xmx = "1024", for example, then we get as a result:
# XMX 1024<br/>
# XMX.class String<br/>
<br/>
# MEM 4096.0<br/>
# MEM.classs Float<br/>
XMX in else<br/>
# MEM 4096.0<br/>
# MEM.class Float<br/>
# XMX <br/>
# XMX.class NilClass<br/>

That is, the xmx variable after the if gets the type nil. If you try without if, just write somewhere xmx == "NONE", then everything is fine, it remains the same line.
Please tell me why xmx becomes nil?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Keyn Din, 2011-08-29
@Lure_of_Chaos

you have xmx calculated in one branch, not in the other

<%
if xmx == "NONE"
xmx = (mem * 0.7).to_s
else
end
%>

U
UseRifle, 2011-08-31
@UseRifle

<%
if xmx == "NONE" %>
<%
xmx = (mem * 0.7).to_s
%>
try this, the only thing that comes to mind is probably erb parsing awry.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question