What exactly is the difference between {?} and {#}?
--
After a little test, listing all truthy/falsy values for {?}, and comparing them to {#}:
context:
{
values: [
// false
'',
"",
false,
null,
undefined,
[],
// true
0,
"0",
"null",
"undefined",
"false",
{},
{a: 'a'}
]
}
template:
{#values}
{?.}true{:else}false{/.}
{/values}
{~n}
{#values}
{#.}true{:else}false{/.}
{/values}
it outputs EXACTLY the same result:
falsefalsefalsefalsefalsefalsetruetruetruetruetruetruetrue
falsefalsefalsefalsefalsefalsetruetruetruetruetruetruetrue
--
Is really there any difference between them ?
There is a difference between the # and ?, though it is somewhat subtle and doesn't reveal itself in your example.
? (exists): Checks for the truthiness of the given key. If the key is truthy, execute the body, else execute the :else body if there is one.
# (section): Checks for the truthiness of the given key. If the key is truthy, set the context to the key, then execute the body. If the context is an array, execute the body once for each element in the array. If the key is not truthy, do not change contexts, and execute the :else body if it exists.
So, if your template looked like this instead:
template:
{?values}
{?.}true{:else}false{/.}
{/values}
{~n}
{#values}
{#.}true{:else}false{/.}
{/values}
Then your output would be:
true
falsefalsefalsefalsefalsefalsetruetruetruetruetruetruetrue
The first line checks that values exists, but does not change the context. The second line checks that the current context (which in this case is the root context) exists, and it prints true. Since ? doesn't step into the context and loop through the array, true is only printed once.
Related
HTTP Live Streaming, draft-pantos-http-live-streaming-23 (Version 23) defines EXT-X-DATERANGE tag. Which has multiple attributes also defined by the specification: ID, START-DATE, etc.
Among defined attributes are SCTE35-OUT, SCTE35-IN. The specification mentions the following about them:
Used to carry SCTE-35 data; see Section 4.3.2.7.1 for more information. These attributes are OPTIONAL.
... value is the big-endian binary representation of the splice_info_section(), expressed as a hexadecimal-sequence.
However, the specification does not define how critical the value of this attributes is.
For example in the documentation of AWS Elemental the values for SCTE35-OUT and SCTE35-IN are identical:
#EXT-X-DATERANGE:ID="999",START-DATE="2018-08-22T21:54:00.079Z",PLANNED-DURATION=30.000,
SCTE35-OUT=0xFC302500000000000000FFF01405000003E77FEFFE0011FB9EFE002932E00001010100004D192A59
.
.
.
#EXT-X-DATERANGE:ID="999",END-DATE="2018-08-22T21:54:30.109Z",DURATION=30.030,
SCTE35-IN=0xFC302500000000000000FFF01405000003E77FEFFE0011FB9EFE002932E00001010100004D192A59
Although HLS specification states:
For splice_insert
The "in" splice_info_section() (with out_of_network_indicator set to 0) MUST be placed in a SCTE35-IN attribute, with the same formatting as SCTE35-CMD.
For time_signal
The "out" splice_info_section() MUST be placed in a SCTE35-OUT attribute; the "in" splice_info_section() MUST be placed in a SCTE35-IN attribute.
Hence, I would expect that the value of these attributes should be always different. Am I missing something? Or this values are completely irrelevant to the clients?
Note: the hex string can be parsed and compared by using: https://openidconnectweb.azurewebsites.net/Cue
The same hex is used twice because break auto return is true,
and the break duration is specified.
"command": {
"command_length": 20,
"command_type": 5,
"name": "Splice Insert",
"time_specified_flag": true,
"pts_time": 13.094733, # The CUE-OUT is at this time
"pts_time_ticks": 1178526,
"break_auto_return": true, # Automatically return from the break
"break_duration": 30.0, # The CUE-IN is 30 seconds later.
"break_duration_ticks": 2700000,
"splice_event_id": 999,
"splice_event_cancel_indicator": false,
"out_of_network_indicator": true,
"program_splice_flag": true,
"duration_flag": true,
"splice_immediate_flag": false,
"unique_program_id": 1,
"avail_num": 1,
"avail_expected": 1
},
"descriptors": []
}
Check out threefive
When I have a function that return multiple values I can either store them in seperat values or use the {} operator to get an array.
To access the values I can either define a variable to store the value or access the array via array[index]. When using a temp var to print the value I code:
function myTest()
return "abc", "def", "geh";
end
a = {myTest()};
v = a[2];
print(v);
which works very well. But when printing the "indexed array converted return value" from the function with
function myTest2()
return "abc", "def", "geh";
end
print({myFunction2()}[2]);
nothing gets printed.
Can someone explain me why?
The form:
{myFunction2()}[2]
is not syntactically valid. I get an unexpected symbol error for that.
You can write it like:
({myFunction2()})[2]
and then it works as expected.
Just don't. When you want to immediately access the Nth return value of a function, use (select(N, ...)), which does not create a new table (and thus creates less work for the GC)
function myTest2()
return "abc", "def", "geh";
end
print( (select(2, myFunction2())) );
Note that enclosing a list of values in () truncates it to the first value; this is necessary because select(N, ...) returns the Nth and all following values. (select(N, ...)) returns only the Nth value.
Basically I want to update each table column for a Model in Rails 5.
str = "abc---def"
str.split('---').map do |a|
Foo.where(product_id:1).update_all(bar: a)
end
Old object would be like:
[
[0] { product_id: 1,
...,
bar: "xxx",
...
},
[1] { product_id: 1,
...,
bar: "xxx",
...
}
]
New should be like:
[
[0] { product_id: 1,
...,
bar: "abc",
...
},
[1] { product_id: 1,
...,
bar: "def",
...
}
]
But what I got is bar: "def" for each. Is there a clean method in rails to achieve what I want? update_attributes gives an error.
Is the title name correct?
First of all let's get started from some basics.
You want to update multiple rows and want to set different value for each row. So it cannot be done in single query like you are doing. So you need to loop through the Foo objects and set each one separately.
So let's assume
str = "abc---def---ghi---jkl"
tokens = str.split('---')
foos_to_update = Foo.where(product_id: 1) #Let's assume it will return 4 or lesser records. (otherwise you need to tell what do you wanna do if it returns more then `tokens`)
foos_to_update.each_with_index {|foo,i| foo.update(bar: tokens[i])}
The last line is looping through returned objects and setting the bar value for each object.
First of all, using Foo.where(id:1).update_all to update a single record may work, but is non-idiomatic. It's better to use Foo.find_by(id: 1).update. For getting single records, I prefer to use find_by instead of find because it returns nil instead of raising NotFound errors, but that's a personal preference.
Second, the way you're using update_all(bar: a) is giving you unexpected results. In a map block, the returned value becomes part of the resulting array. update_all doesn't return the record which were changed. It returns an integer showing the count of records which were changed. Similarly, update doesn't return the record. It returns true or false` depending on if the validations passed.
Tying together these concepts, the following code can be written:
str = "abc---def"
str.split('---').map do |a|
foo = Foo.find_by(id:1)
foo&.update(bar: a)
foo
end
# note that you could instead write `foo.update(bar: a)` if you
# don't want to use the safe navigation operator
Or another way to write it which does the same thing:
str = "abc---def"
str.split('---').map do |a|
Foo.find_by(id:1)&.tap { |foo| foo.update(bar: a) }
end
Note that in these examples I'm using the safe navigation operator which is in Ruby versions newer than 2.3. It helps prevent NoMethodError on nil objects, but isn't really necessary.
I am trying to run code dynamically in groovy. I have someNode[0], which is the value, in variable var1
I then added double quotes to it like this
var2 = "\""+var1+"\""
then I tried to run this
request.abc."$var2"=Value
I saw here that something of this sort can be done on properties and methods. But the above code is not working. Giving me error
An error occurred [Cannot set property '"someNode[0]"' on null object], see error log for details
Any help is appreciated. Thanks.
Edit
Heres a snippet of my request
{
"app":{
"bundle":"531323947",
"cat":[
"IAB1",
"IAB9",
"IAB9-30",
"entertainment",
"games"
],
"id":"agltb3B1Yi1pbmNyDAsSA0FwcBitsL4UDA",
.
.
The field I am trying to manipulate is cat[0], which is IAB1 (I just used abc and someNode[0] in the code that i wrote above but actually they are app and cat[0])
Also, I parsed the request with jsonslurper befor running the above code
Thank you for your help
One way to do this, is by Eval
def request =[
"app":[
"bundle":"531323947",
"cat":[
"IAB1",
"IAB9",
"IAB9-30",
"entertainment",
"games"
],
]
]
assert request.app.cat[0]=='IAB1'
def var = 'request.app.cat[0]'
Eval.me('request', request, "$var = 'new value'")
assert request.app.cat[0]=='new value'
You are accessing/updating values from a map and a list. The request.app node will be a map, the request.app.cat node will be a list. Getting and setting the values in a map can be done in many different ways:
Use the put & get methods directly.
Use brackets [].
Use missing properties as map keys (i.e. the way you are using it).
For what you want to achieve, i.e. to access values from variable keys, it is much easier to use method 1 or 2 instead of method 3 with a variable inside a GString.
Example using brackets:
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
def request = new JsonSlurper().parseText '''{
"app":{
"bundle":"531323947",
"cat":[
"IAB1",
"IAB9",
"IAB9-30",
"entertainment",
"games"
],
"id":"agltb3B1Yi1pbmNyDAsSA0FwcBitsL4UDA"
}
}'''
def level0 = 'app'
def level1 = 'cat'
def node = request[level0][level1]
assert request instanceof Map
assert node instanceof List
assert node[0] == 'IAB1'
node[0] = 'new value'
assert node[0] == 'new value'
println new JsonBuilder(request).toPrettyString()
Output:
{
"app": {
"cat": [
"new value",
"IAB9",
"IAB9-30",
"entertainment",
"games"
],
"id": "agltb3B1Yi1pbmNyDAsSA0FwcBitsL4UDA",
"bundle": "531323947"
}
}
Instead of using long lists of arguments in my function definitions, I prefer to pass a few fixed parameters and a table of 'additional params' like this:
function:doit( text, params )
end
This is nice as it allows me to add new named parameters later without breaking old calls.
The problem I am experiencing occurs when I try to force default values for some of the params:
function:doit( text, params )
local font = params.font or native.systemBold
local fontSize = params.fontSize or 24
local emboss = params.emboss or true
-- ...
end
The above code works fine in all cases, except where I have passed in 'false' for emboss:
doit( "Test text", { fontSize = 32, emboss = false } )
The above code will result in emboss being set to true when I really wanted false.
To be clear, what I want is for the first non-NIL value to be assigned to emboss, instead I'm getting a first non-false and non-NIL.
To combat this problem I wrote a small piece of code to find the first non-NIL value in a table and to return that:
function firstNotNil( ... )
for i = 1, #arg do
local theArg = arg[i]
if(theArg ~= nil) then return theArg end
end
return nil
end
Using this function I would re-write the emboss assignment as follows:
local emboss = firstNotNil(params.emboss, true)
Now, this certainly works, but it seems so inefficient and over the top. I am hoping there is a more compact way of doing this.
Please note: I found this ruby construct which looked promising and I am hoping lua has something like it:
[c,b,a].detect { |i| i > 0 } -- Assign first non-zero in order: c,b,a
Lua's relational operators evaluate to the value of one of the operands (i.e. the value is not coerced to boolean) so you can get the equivalent of C's ternary operator by saying a and b or c. In your case, you want to use a if it's not nil and b otherwise, so a == nil and b or a:
local emboss = (params.emboss == nil) and true or params.emboss
Not as pretty as before, but you'd only need to do it for boolean parameters.
[snip - Lua code]
Now, this certainly works, but it seems so inefficient and over the top.
Please note: I found this ruby construct which looked promising and I am hoping lua has
something like it:
[c,b,a].detect { |i| i > 0 } -- Assign first non-zero in order: c,b,a
Your Lua function is no more over-the-top or inefficient. The Ruby construct is more succinct, in terms of source text, but the semantics are not really different from firstNotNil(c,b,a). Both constructs end up creating a list object, initialize it with a set of values, running that through a function that searches the list linearly.
In Lua you could skip the creation of the list object by using vararg expression with select:
function firstNotNil(...)
for i = 1, select('#',...) do
local theArg = select(i,...)
if theArg ~= nil then return theArg end
end
return nil
end
I am hoping there is a more compact way of doing this.
About the only way to do that would be to shorten the function name. ;)
If you really want to do it in a single line, you'll need something like this for a default value of true:
local emboss = params.emboss or (params.emboss == nil)
It's not very readable, but it works. (params.emboss == nil) evaluates to true when params.emboss is not set (when you would need a default value), otherwise it's false. So when params.emboss is false, the statement is false, and when it's true, the statement is true (true or false = true).
For a default of false, what you tried originally would work:
local emboss = params.emboss or false