if variable empty do default - ruby-on-rails-3

Currently, I've got this:
self.profile_pic = user_info['image']
self.birthday = user_extra['birthday']
self.city = user_extra['location']['name']
Sometimes the user_extra or user_info var is blank. I would like to have a default value then. How to do this?
Thanks in advance,
Maurice

It really depends on how specific you want to get. Is it blank or nil? Because there's a big difference there. In Ruby, a blank String is not evaluated to false. Assuming that the specific hash of that variable is nil, this would work...
self.profile_pic = user_info['image'] || "default"
self.birthday = user_extra['birthday'] || "default"
self.city = user_extra['location']['name'] || "default"
Or you could have it be a more general check to make sure the variable isn't nil itself, using the ternary operator...
self.profile_pic = user_info ? user_info['image'] : "default"
self.birthday = user_extra ? user_extra['birthday'] : "default"
self.city = user_extra ? user_extra['location']['name'] : "default"
Or, if it actually is blank and not nil, something more like this...
self.profile_pic = user_info.empty? ? "default" : user_info['image']
self.birthday = user_extra.empty? ? "default" : user_extra['birthday']
self.city = user_extra.empty? ? "default" : user_extra['location']['name']
It depends on the exact conditions you have, and how far down the rabbit hole you want to go. :) Hope that helps!

Related

Terraform function lookup within lookup

I am trying to use Terraform function lookup and then lookup to fetch the values and then add into the conditional loop based on the value like below - For creating s3 bucket server side encryption
Below is var.tf
variable "encryption" {
type = map
default = {
"keyMap" = "SSE-S3"
"kmsType" = "aws-kms"
"keyNull" = null
}
}
Now I want to use local.tf with below code to get "SSE-S3" value like below
encryption_type = lookup(var.encryption, "default", null) == null ? null : lookup(var.encryption.default, "keyMap", null)
Just wonder above my logic will fetch the value for encryption_type is "SSE-S3"
Any help is appreciated. Thanks in advance.
You don't have to lookup "default". The default inside a variable definitions is just the default value of that variable. Your current code is actually invalid because a lookup on "default" is never going to work. It's also not clear what your "keyMap" lookup is doing, since there is no property in your example named "keyMap".
Your code could be corrected and shortened to the following:
encryption_type = lookup(var.encryption, "keyType", null)
or just
encryption_type = var.encryption["keyType"]

Terraform: Set optional resource attribute if condition is met, otherwise don't declare it

Some resources on Terraform support optional attributes. I'm interested in declaring and setting a value for the optional attribute only if a condition is met. Otherwise, don't declare it at all.
All of the suggestions I found was based on declaring the attribute and setting its value to null if the condition isn't satisfied, instead of not declaring the attribute at all.
Is there a way for me to do something like the following? In pseudo-code:
resource "some_resource" "this" {
name = var.name
if var.name == "some_name":
some_optional_attribute = "some_value"
else:
pass # do nothing, don't even declare the optional attribute
}
Let me know, thanks in advance!
I don't believe there is a better method than simply doing the following:
resource "some_resource" "this" {
some_optional_attribute = var.name == "some_name" ? var.name : null
}
When you declare attribute as null that basically means that it is not being used. The above in my opinion is equivalent to your if statement.

Getting the name of the variable as a string in GD Script

I have been looking for a solution everywhere on the internet but nowhere I can see a single script which lets me read the name of a variable as a string in Godot 3.1
What I want to do:
Save path names as variables.
Compare the name of the path variable as a string to the value of another string and print the path value.
Eg -
var Apple = "mypath/folder/apple.png"
var myArray = ["Apple", "Pear"]
Function that compares the Variable name as String to the String -
if (myArray[myposition] == **the required function that outputs variable name as String**(Apple) :
print (Apple) #this prints out the path.
Thanks in advance!
I think your approach here might be a little oversimplified for what you're trying to accomplish. It basically seems to work out to if (array[apple]) == apple then apple, which doesn't really solve a programmatic problem. More complexity seems required.
First, you might have a function to return all of your icon names, something like this.
func get_avatar_names():
var avatar_names = []
var folder_path = "res://my/path"
var avatar_dir = Directory.new()
avatar_dir.open(folder_path)
avatar_dir.list_dir_begin(true, true)
while true:
var avatar_file = avatar_dir.get_next()
if avatar_file == "":
break
else:
var avatar_name = avatar_file.trim_suffix(".png")
avatar_names.append(avatar_name)
return avatar_names
Then something like this back in the main function, where you have your list of names you care about at the moment, and for each name, check the list of avatar names, and if you have a match, reconstruct the path and do other work:
var some_names = ["Jim","Apple","Sally"]
var avatar_names = get_avatar_names()
for name in some_names:
if avatar_names.has(name):
var img_path = "res://my/path/" + name + ".png"
# load images, additional work, etc...
That's the approach I would take here, hope this makes sense and helps.
I think the current answer is best for the approach you desire, but the performance is pretty bad with string comparisons.
I would suggest adding an enumeration for efficient comparisons. unfortunately Godot does enums differently then this, it seems like your position is an int so we can define a dictionary like this to search for the index and print it out with the int value.
var fruits = {0:"Apple",1:"Pear"}
func myfunc():
var myposition = 0
if fruits.has(myposition):
print(fruits[myposition])
output: Apple
If your position was string based then an enum could be used with slightly less typing and different considerations.
reference: https://docs.godotengine.org/en/latest/tutorials/scripting/gdscript/gdscript_basics.html#enums
Can't you just use the str() function to convert any data type to stirng?
var = str(var)

Eager Loading by Setting Variable

Sorry in advance for this incredibly simple question, but what is the best way to set a variable while also checking a condition. For example, I have:
#friends = []
#user.facebook_friends.each do |key,value|
if test = Authorization.includes(:user).find_by_uid(key) != nil
#friends << {"name" => test.user.name, "facebook_image_url" => test.user.facebook_image_url}
end
end
I am trying to pull in the user records when I pull in the authorization record, so as to minimize my database queries. I know that I can't write
test = Authorization.includes(:user).find_by_uid(key) != nil
in order to set the test variable. What is the best way to write this code so that it is functional?
You just need parens:
(test = Authorization.includes(:user).find_by_uid(key)) != nil
Also here is a more rails way to do it:
unless (test = Authorization.includes(:user).find_by_uid(key)).nil?
#friends << {"name" => test.user.name, "facebook_image_url" => test.user.facebook_image_url}
end

Recognizing multiple string values in a single action

Thanks again for the help.
I have a simple action that checks the stringValue of a textField, and if it matches - a status message prints in a second textField:
if
(textField.stringValue == (#"Whatever" )){
[outputDisplay setStringValue:#"Success"];
My question is how do I implement multiple input stringValue options in this method? For example "Whatever" "Whatever1, Whatever2" all return "Success" in the outputDisplay.
thanks.
Paul
Create a set of answers you're looking for and test if the string in question is in there.
NSSet *successStrings = [NSSet setWithObjects:#"Whatever1",
#"Whatever2",
#"Whatever3",
nil];
if ([successStrings containsObject:st]) {
[outputDisplay setStringValue:#"Success"];
}
(An array would also work, but a set is specialized for testing membership, so it's a better fit for what we're doing here.)
Firstly, to check for equality of NSString-s you should use -isEqualToString:. == compares the pointer values which often returns NO even if the two strings' contents are the same.
To check if the text field match multiple strings, you connect them with the || (or) operator, so you get
NSString* st = textField.stringValue;
if ([st isEqualToString:#"Whatever"] || [st isEqualToString:#"Whatever1"] || [st isEqualToString:#"Whatever2"]) {
[outputDisplay setStringValue:#"Success"];