How to check undefined multilevel variables? - nullable

I have this expression in Freemarker:
<#list data.customer.emails as email>
</#list>
How can I construct an <#if> clause to check if data, data.customer or data.customer.emails are undefined?

I found the solution:
<#if (data.customer.emails)??>
<#list data.customer.emails as email>
...
</#list>
</#if>

Related

How do I fix this this prison script lua

TxAdmin Log
Server.lua file
I have try'd to change the nummbers but I think in the Database is something wrong
You're attempting to index result a nil value in line 170 of server.lua.
result is nil in this scope you you cannot do this result[0].
Either find out why result is nil and fix it or avoid indexing it if it is .
Your code suggests that you failed to assign the return value of your SQL query to a variable.
So try
local result = MySQL.Asynch.fetchAll("...
Still you should checker whether your query actually returned a result using a conditional statement or assertion.

Request parameter if struts2 if tag

I have another probably basic problem. happy if u can help.
there is a request parameter 'action'. if I write :
<label><s:property value="%{#parameters.action}"/></label>
the value appears (it is 1)
So itry to test now :
<s:if test="%{#parameters.action == '1'}">YES 1</s:if><s:else>NOT 1</s:else>
NOT 1 appears.
I have tries all the syntaxes I found on the net for the test. Nothing changes, NOT 1 still displays
Thank you
This is because:
the value of %{#parameters.action} is an array, not a single value, and
the value will be type-converted to a number (not sure why; need to look in to that)
The correct expression would be:
<s:if test="%{#parameters.action[0] == 1}">YES 1</s:if><s:else>NOT 1</s:else>
The correct expression would be:
<s:if test="#parameters.action[0] == 1">YES 1</s:if><s:else>NOT 1</s:else>
The request parameters is a map of [Strinf, String[]], So you have to access it like above

Fatal error: Call to undefined method simple_html_dom::first_child()

sometimes when i use simple html dom methods like save(), children(), first_child() i get an error that says Call to undefined method. why i get this error?
I am not sure if this late answer is usable for you. I have got the same error message and find out that for simple_html_dom, the usage is following:
$h = new simple_html_dom;
$h -> load($your_html_string); // for example blah blah blah...
instead of using (very important):
$h -> fist_child();
you need to:
$f ->$h -> find($tag, 0); //here $tag is 'div'
$f -> first_child;
unfortunately if you do not know your tag, you need to use preg_match to get it. hope this help.
Maybe it's not node or null
So, you must check it.
Simple html dom exposes two classes. simple_html_dom_node and simple_html_dom. If you try to use a node method on a dom or vice versa, it will cause this error.

How to parse a yaml file into ruby hashs and/or arrays?

I need to load a yaml file into Hash,
What should I do?
I would use something like:
hash = YAML.load(File.read("file_path"))
A simpler version of venables' answer:
hash = YAML.load_file("file_path")
Use the YAML module:
http://ruby-doc.org/stdlib-1.9.3/libdoc/yaml/rdoc/YAML.html
node = YAML::parse( <<EOY )
one: 1
two: 2
EOY
puts node.type_id
# prints: 'map'
p node.value['one']
# prints key and value nodes:
# [ #<YAML::YamlNode:0x8220278 #type_id="str", #value="one", #kind="scalar">,
# #<YAML::YamlNode:0x821fcd8 #type_id="int", #value="1", #kind="scalar"> ]'
# Mappings can also be accessed for just the value by accessing as a Hash directly
p node['one']
# prints: #<YAML::YamlNode:0x821fcd8 #type_id="int", #value="1", #kind="scalar">
http://yaml4r.sourceforge.net/doc/page/parsing_yaml_documents.htm
You may run into a problem mentioned at this related question, namely, that the YAML file or stream specifies an object into which the YAML loader will attempt to convert the data into. The problem is that you will need a related Gem that knows about the object in question.
My solution was quite trivial and is provided as an answer to that question. Do this:
yamltext = File.read("somefile","r")
yamltext.sub!(/^--- \!.*$/,'---')
hash = YAML.load(yamltext)
In essence, you strip the object-classifier text from the yaml-text. Then you parse/load it.

Ruby on Rails: what does "equals" symbol mean as a parameter?

Some open source I've been using has the below line as a function declaration:
def parse_query(query=nil, options={}, models=nil)
What effect do the "equals" symbols have on the statement? Does it just make the parameters optional?
It sets the default value of the parameter, if the person calling the function does not specify one.
Similar to Python and C++, the equals sign in the parameter list lets you specify a default parameter. For example, in Python:
def hello_world(message="Hello World"):
print "message = "+message
Calling this function like this:
hello_world()
Will result in:
message = Hello World
But calling the function like this:
hello_world("changed default")
results in:
message = changed default

Resources