Proper Syntax When Using dot Operator in String Interpolation in Dart - variables

In Dart/Flutter, suppose you have an instance a of Class Y.
Class Y has a property, property1.
You want to print that property using string interpolation like so:
print('the thing I want to see in the console is: $a.property1');
But you can't even finish typing that in without getting an error.
The only way I can get it to work is by doing this:
var temp = a.property1;
print ('the thing I want to see in the console is: $temp');
I haven't found the answer online... and me thinks there must be a way to just do it directly without having to create a variable first.

You need to enclose the property in curly braces:
print('the thing I want to see in the console is: ${a.property}');
That will then print the value of a.property.

It seems you can also do this, but it doesn't seem to be documented anywhere:
print('..... $a.$property1');

Related

Can't quote array - rails sql

Im trying to create a sql query dynamically with the following syntax:
Company.joins(:founder_persons)
.where("people.first_name like people[:first_name]", {people: {first_name: 'm%'}})
But running this on the rails console gives me TypeError: can't quote Array. Im guessing this is not how we use the where string? What's the right way to fix this error? Thanks.
One reason this error can occur is with a nested array used as SQL value.
Example:
Article.where(author: ['Jane', 'Bob'])
works, but:
Article.where(author: ['Jane', ['Bob']])
would give the error. A quick fix would be to run flatten on the array.
(Mentioning this since this page comes up when searching for the confusing error "Can't quote array".)
You could bind any value and then assign it, this way they should coincide in numbers, like:
Model.joins(:join_table)
.where('models.first_attribute LIKE ? AND models.second_attribute LIKE ?', value_for_first_attr, value_for_second_attr)
If using an array you should access each index you want to compare, or you can precede a splat *, and specify just one value, like:
Model.joins(:join_table)
.where('models.first_attribute LIKE ? AND models.second_attribute LIKE ?', *array_of_values)
Note although this way you're passing the "whole" array it should also coincide in size or numbers, otherwise it'd raise an ActiveRecord::PreparedStatementInvalid error depending if there are more or less elements than needed.

Evaluating Variables in Load Script

Is there any reason that this syntax shouldn't work in Qlikview load script??
Let v_myNumber = year(today());
Let v_myString = '2017-08';
If left($(v_myString),4) = text($(v_myNumber)) Then
'do something
Else
'do something else
End If;
I've tried both ways where I convert variable string to number and evaluate against the number variable directly and this way. They won't evaluate to equivalence when they should..
Left function is expecting a string as is getting something else as a parameter. As you are currently doing, the function will be called as Left(2017-08, 4) which is unhandle by QlikView.
If you use Left('$(v_myString)',4), it will evaluate as Left('2017-08', 4) as work as expected. Just adding quotes around the variable it should work.
Although QlikView calls them variables, they should really be seen as "stuff to replaced (at sometimes evaluated) at runtime", which is slightly different from a standard "variable" behaviour.
Dollar sign expansion is a big subject, but in short:
if you are setting a variable - no need for $().
if you are using a variable - you can use $(). depends on its context.
if you are using a variable that needs to be evaluated - you have to use $().
for example in a load script: let var1 = 'if(a=1,1,2)' - here later on the script you will probably want to use this variable as $(var1) so it will be evaluated on the fly...
I hope its a little more clear now. variable can be used in many ways at even can take parameters!
for example:
var2 = $1*$2
and then you can use like this: $(var2(2,3)) which will yield 6
For further exploration of this, I would suggest reading this

Rails 3 : Use model method while using sum()

I am new to rails. I am trying to figure out how to use model method inside the sum() sql function. I tried searching for the solution but couldn't find one. Here's the code snippet :
SUM(indents.total_payable_amount_paid) AS sum_comm_t_amount_payable_paid
I want to use method called total_payable_amount_paid defined inside indent model. But it always gives an error:
PGError: ERROR: column indents.total_payable_amount_paid does not exist.
So what's the solution for this problem? Thanks in advance!
The problem is that your SUM (which i assume it's in a query string) takes indents.total_payable_amount_paid as part of that string.
To avoid that you could use string interpolation to set the value you want, like this:
"SUM(#{indents.total_payable_amount_paid}) AS sum_comm_t_amount_payable_paid"
So, lets say indents.total_payable_amount_paid returns 250, then the above code will generate this string:
"SUM(250) AS sum_comm_t_amount_payable_paid"
But, using SUM will make no effect, since you are giving one value, so you can accomplish the same thing without it:
"#{indents.total_payable_amount_paid AS sum_comm_t_amount_payable_paid"
#=> "250 AS sum_comm_t_amount_payable_paid"

How to use _root and this[] in same line?

Here's a chunk of code that WORKS when it's on the main timeline:
var DysonTarget:String = "S"+(random(40)+1);
this[DysonTarget].MyType = "Dyson Sphere";
this[DysonTarget].gotoAndStop(this[DysonTarget].MyType);
It's choosing a number between 1 and 40, adding an S before it, and going into one of forty movie clips on the main stage with instance name S1, S2. . . S40 etc. Then it will display an image in the chosen clip. But to make this truly work the way I want to, I have to put the above code inside a movie clip. So I tried this, after declaring my variable on the main stage:
_root.this[DysonTarget].MyType = "Dyson Sphere";
_root.this[DysonTarget].gotoAndStop(this[DysonTarget].MyType);
It didn't compile, the error message said "Expected a field name after the '.' operator. Trying it with _parent returned the same message. With _level0 didn't work at all, and placing the _root and _parent inside the bracket didn't work either. I haven't been able to find any answer online because trying to type "this" into a search is too vague to return an answer about the actual command.
. . .help me :(
A friend of mine who is a software developer helped me on this one. Here's what we figured out:
First you declare variable DysonTarget on the main timeline:
var DysonTarget:String = "S" + (random(40)+1);
Then inside the movie clip, use this:
_level0[DysonTarget].MyType = "Dyson Sphere';
_level0[_level0.DysonTarget].gotoAndStop(_level0[_level0.DysonTarget].MyType);
I've tried this a few other ways, and the above method is the only one that works the way it's supposed to. But it works! My impression is the brackets tell it to look for an object named what the variable is set to, rather than an object with the same name as the variable.

Velocity, how to retrieve a hashmap value using another velocity variable

I have a HashMap in my bean:
HashMap<String, SomeObject> someHashMap;
Then in the velocity template I need to access the HashMap with a value that I have in velocity from other source (in fact I have many keys not only one that's why I need to get the values this way):
$key
How can I access the hashmap with this key? I'm trying:
$someHashMap.get($key)
and
${someHashMap.get($key)}
But those two only write the same thing to the output, meaning that with the first line I literally get:
$someHashMap.get($key)
In the webpage.
Which is the correct way/syntaxis to do this?
Thanks!
Both are correct syntax, and they should work.
Does $key have the right value? Print it.
Does $someHashMap indeed point to the map? Print it. If not, perhaps you forgot to put in the VelocityContext being used.
Is the value stored under that key null? The default behavior of Velocity is to print out the code that was called when the outcome is null. To make it not do that, use the silent notation: $!{someHashMap.get($key)}
I had this exact same problem. In my case I tried to do this:
$map.get($locale)
where $locale is e.g. "fi_FI". I solved it by adding quotes inside the brackets:
$map.get("$locale")
I'm not sure, but I think the rationale goes like this:
$map.get( $locale ) -> $map.get( fi_FI ) -> Velocity gets confused
$map.get("$locale") -> $map.get("fi_FI") -> Velocity retrieves correct value