How to name a variable which contains a string in either singular or plural form? - naming-conventions

Sometimes, depending on some value, you need to assign a string variable either the singular or plural form to.
Example:
String token = (i > 1) ? "specs" : "spec";
But how to name such a variable? specToken? specOrSpecs? Just token?

I'd use units.
Note that whether you use singular or plural for the zero case can be very language-specific. For example, in English, I'd probably write:
String units = (i == 1) ? "spec" : "specs";
Because you'd want "0 specs" rather than "0 spec".

Related

Why do variables need single quotes in `execute` SQL query?

Given name = "Ben", this does not work:
DB.execute("SELECT id FROM departments WHERE name = #{name}")
but this does (added single quotes):
DB.execute("SELECT id FROM departments WHERE name = '#{name}'")
Why? Why doesn't Ruby call the variable in the first example? It's already a string. I don't see why that should make any difference. Isn't that equivalent to passing "'Ben'"? Wouldn't this be equivalent to passing "Ben"?
It's not a Ruby problem, but an SQL syntax issue.
WHERE name = tom
and
WHERE name = 'tom'
are two different things.
In the first case, it is treated as a schema object name (presumably a column name here), in the second case it is a String literal.
You run into the same issue whenever you use one programming language to create a program for another programming language (here Ruby -> SQL) by direct String manipulation.
The correct way is to use bind variables.
WHERE name = ?
That also deals with the problem of name containing special characters.
name = "Little Bobby Tables, see http://xkcd.com/327/"

Rails query the last of each type

I am using STI and have a table Widget that a bunch of other subclasses inherit from using STI. I want a query that gets the last created of each object with a uniq type.
I have a predefined array of types I want so lets say:
types = ["type1", "type2", "type3"]
So my query would be something like: (this doesnt work)
Widget.where(type: types).uniq.reverse
My goal is to get the last of each object that matches 1 of those types..
Not 100% sure, but something like this might work (untested):
ids = Thing.where(type: types).group(:type).maximum(:id).values
last_per_type = Thing.find(ids)
Thing.select("distinct type")
By the way, type is a special variable in rails and can't be used as a column name.

How to add text plus the text written from a Parameter type C in ABAP?

I am working in an ABAP program and I have a question.
For example in C# when we have a String variable: string name; , and we want this to be filled with some data from a textbox but also add some ohter text.
For example:
string name = "Hello: " + textBox1.text;,
And I want to ask you how can I do this in ABAP ??? How to add text plus the text written from a Parameter type C?
CONCATENATE and the concatenate operator && will do it as answered by Jagger and vwegert. To do it with string expressions, you use the below where name is the screen field or whatever that has the name in it (it doesn't need to be a field-symbol):
greeting = |Hello: { <name> }|.
String expressions are extremely useful as they can be used to build up complex values without declaring extra variables - e.g. they can passed as directly as function module or method parameters without first assigning to a local variable.
You can either use the CONCATENATE keyword or -- in newer releases -- string expressions. Be sure to check the online documentation and sample programs available using the transaction ABAPDOCU, it will save you a ton of seemingly basic questions.
The equivalent operator is &&.
So in your case it would be:
name = 'Hello: ' && textBox1->text.

How do you get the field name, instead of the value, from the model?

Suppose I have a Model Class named Anything, in Yii, and all I want is to get not the field value, but the field name, how could I do that?
Because using something like:
$anything = new Anything;
$anything->field_name;
Returns the value of that field, which is the purpose for that, still, if all you want is the string of the name of the field, how could you do that?
I tried using:
$anything->attributes;
But it just returns an array of field names, I want to try and get a specific value as a defined constant.
What I want to do is use the $_POST with specific and practical use, so I wouldn't need to use:
$_POST["Model_name"];
Instead I could use:
$_POST[Anything::model()->name][Anything::model()->field_name->name]
Which seems a lot better than "" and '' here and there. Mostly because I'm trying to set multiple fieldsets of different Models in the same formulary.
So if I could use:
$_POST[Anything::model()->name][Anything::model()->field_name->name];
and
$_POST[Something::model()->name][Something::model()->field_name->name]
and
$_POST[Godspeed::model()->name][Godspeed::model()->field_name->name]
It would save a lot of problems I might had in the future.
$strModelName = 'ModelName'; //dynamic - whatever model name you put in it
$find_id = 3;
$record = $strModelName::model()->findByPK($find_id); //it's same with ModelName::model()->findByPK(3)
foreach($record->attributes as $key=>$value){
var_dump($_POST[$strModelName][$key]); //get value corresponding to given key
}
Btw, you still need check whether the model is exist or not
http://www.yiiframework.com/forum/index.php/topic/22790-check-if-model-exists/

string and numeric separation

I did post the phone no from form
def update_phone
if params[:phone]
#here I want to check weather params[:phone] is number or string
end
I did use params[:phone].class it is always showing string class. How I can find whether string or integer?
In Ruby you mostly do not operate on types but on behaviour of variable. If you want to chceck that params[:phone] is valid phone number use regexp, i. e.:
phone = params[:phone] if params[:phone] =~ /\A\(?\d{3}\)?-?\d{3}-?\d{3}\Z/
But even better option is to check this in validators in your model.
Your should use some Validators in your model combined with a regex for phone numbers: depending on your country's phone number system only digits and some formatting characters like +-./() and space should be allowed: +23 (0) 322/242324-12 or 234.1432.123 are both valid numbers.
edited, thanks for the comment. I figure this works for most common cases. however, this won't work with leading zeros:
str = params[:phone]
#it is numeric if the following is true:
str.to_f.to_s == str || str.to_i.to_s == str