What data object allows limiting possible values in variable? - data-structures

I remember learning about certain best practices around this, but I blanked completely on the name of what this is called. Basically, instead of saying:
pencil_1.size = 'SHORT';
pencil_2.size = 'LONG';
pencil_3.size = 'SHORT';
pencil_4.size = 'SHORT';
pencil_5.size = 'MEDIUM';
We would say:
pencil_1.size = Length.SHORT;
pencil_2.size = Length.LONG;
pencil_3.size = Length.SHORT;
pencil_4.size = Length.SHORT;
pencil_5.size = Length.MEDIUM;

I just found the answer: Enum. That's the term I was looking for.

Related

Access an JSON object value - used path contains an dynamic value

I like to access a specific JSON object value, where the access path contains a dynamic value. The dynamic value is calculated beforehand during the dialog process.
How can I do this in a "set property" node? I did not find a good working approach.
Here my approach, which leads to no useful result.
=user.api_content.prediction.intents.${virtualagent.intent}.score
Thanks
Which language are you using? You should be able to just address it directly without a string designator. For example, here is how I get the score for the top intent, where intent is a variable property. You would just want to get it via the [] notation instead of the . notation.
var intent = LuisRecognizer.topIntent(recognizerResult);
if (recognizerResult.intents[intent].score < 0.5) {
intent = 'None';
}

Laravel store request all better way

I have following code:
//Create Assignment
$customer['projects_id'] = $Project->id;
$customer['user_id'] = $customeruser['user_id'];
$customer['user_name'] = $SalesRepresentative->user_name;
$customer['user_role'] = "Admin";
ProjectUser::firstOrCreate($customer);
Is there a way to write this code cleaner and don't have 4 lines? Maybe one instead? Thanks. request has a lot more values.
If you are using a validating request class you can simply call ProjectUser::firstOrCreate($request->validated()); in that case, only validated variables are passed to store method.

Ruby variable = variable

I'm taking a bootcamp course and I know line 4 (zip_code = zip_code) isn't necessarily needed but I've been told it's useful for a simple reason, but I'm not sure what that is. Anyone know why? Thanks so much.
class AdoptADog::Scraper
def self.scrape_dogs(zip_code)
base_url = "https://www.petsmartcharities.org/find-a-pet-results?city_or_zip="
zip_code = zip_code
last_url = "&species=dog&color_id&geo_range=50&pet_size_range_id&sex&age=&breed_id=69"
full_url = base_url + zip_code + last_url
html = open(full_url)
doc = Nokogiri::HTML(html)
doc.css(".pet-result").each do |dog|
name = dog.css(".pet-name").text
breed = dog.css(".pet-breed").text
sex = dog.css(".pet-sex").text
location = dog.css(".pet-addr-city-state").text
url = dog.css("a").attribute("href").value
AdoptADog::Dogs.new(name, breed, sex, location, url)
end
end
end
No, and the initial premise that it is useful is incorrect.
There is no functional reason for this, and I would argue against even the loose case one could make that it "increases readability".
This is pretty much bad practice in EVERY language.
The one and only possible reason for this would be to demonstrate variables to someone who is just starting to learn the core fundamentals of programming. Even that would be a bad example though, as it could be misunderstood to be good practice, when it most definitely is not, and there are FAR better ways to illustrate that without any risk of misconception.
maybe zip_code = zip_code.dup ?, you should not change the passed params in your function.
Could it be that you missed .dup or .clone ?
something = something.dup can be useful if you work with mutable object and don't wanna mess with original one.
Anyway, if you have been told that it is useful for some reason, why don't you just ask that person to elaborate?

Wakanda query with dynamic attribute name in a variable

Please, take a look at following code:
var platNom = "apero"; // I set the value of my attribute in one variable
var monAttribut = ds.Convives.attributes[platNom]; //this works
var invitants = ds.Convives.query("$(this[platNom] == 'oui') ", {allowJavascript: true});// This does not work
Is there a problem in my syntax?
If I understand correctly you would like to use attribute variable to construct the query string. Then you can do this by simply reference platNom directly.
The correct syntax should be:
var invitants = ds.Convives.query(platNom + " == 'oui'")

More concise way of writing this array inclusion / default fallback code?

I find that I've been doing this a fair enough number of times in my Rails controllers that I'm interested in finding a better way of writing it out (if possible). Essentially, I'm validating the input to a few options, and falling back on a default value if the input doesn't match any of the options.
valid_options = %w(most_active most_recent most_popular)
#my_param = valid_options.include?(params[:my_param]) ? params[:my_param] : 'most_recent'
If you use a hash instead of an array, it would be faster and cleaner. And, since your default is "most_recent", having "most_recent" in valid_options is redundant. You better remove it.
filter_options =
Hash.new("most_recent")
.merge("most_popular" => "most_popular", "most_active" => "most_active")
#my_param = filter_options[params[:my_param]]
I too would go the Hash route.
This could be imaginable:
Hash[valid_options.zip valid_options].fetch(params[:my_param], "most_recent")
A bit farfetched.
valid_options = %w(most_active most_recent most_popular)
(valid_options & [params[:my_param]]).first || 'most_recent'
How is the below:
valid_options = %w(most_active most_recent most_popular)
valid_options.detect(proc{'default_value'}){|i| i == params[:my_param] }
Another one:
valid_options = %w(most_active most_recent most_popular)
valid_options.dup.delete(params[:my_param]) { "default" }

Resources