Answer by Ja͢ck for How to make a default subcommand with clap and derive
The following idea worked out the best for my own project, which uses different arguments for each subcommand; the first step is to declare each subcommand argument list as a separate...
View ArticleAnswer by Ja͢ck for Ranking objects within array immutably with same values...
I am able to achieve the desired functionality using forEach, but it mutates the objects in the original arrayAs soon as you alter item inside your loop, it will modify the original array.Is there any...
View ArticleComment by Ja͢ck on trouble getting sqlx #[derive(sqlx::Type)] to work with a...
Have you tried to add #[sqlx(type_name="VARCHAR")] above your enum?
View ArticleAnswer by Ja͢ck for PHP working with time only, by means of strtotime()
An example of how to do this with DateTime:$d1 = new DateTime('11:00:00');$d2 = new DateTime('04:00:00');$diff = $d1->diff($d2);echo $diff->h, " hours ", $diff->i, " minutes\n";Output:7 hours...
View ArticleAnswer by Ja͢ck for PHP: from string to array: argument => parametr
This should do it, using preg_match_all() to get all the groups and array_combine() to form the final array:if (preg_match_all('/(\w+)="([^"]*)"/', $str, $matches)) { return array_combine($matches[1],...
View ArticleAnswer by Ja͢ck for Swapping two numbers using only two variables
The swap is performed using XOR, which is typically written as a plus within a circle (⊕); for example:a := 5b := 7a := a xor b (2)b := a xor b (5)a := b xor a (7)
View ArticleAnswer by Ja͢ck for Prevent change of one field in Rails model
I recently came across this issue as well, and here's another way:validates :activity_id, comparison: { equal_to: :activity_id_was }, on: :updateAlternatively, you can use activity_id_in_database...
View ArticleComment by Ja͢ck on Disable loading of nested locale (I18n) folders in Rails 7
For those interested, you can find the rails change here: github.com/rails/rails/pull/41872
View ArticleAnswer by Ja͢ck for Fastest way to handle undefined array key
UpdateSince PHP 7 you can accomplish this with the null coalescing operator (keeping in mind that it will bypass existing array element with null value, the same way isset would):return $table[$key] ??...
View ArticleAnswer by Ja͢ck for PHP function to generate v4 UUID
To construct a UUIDv4 you can generate 128 bits worth of random data, patch a few fields to make the data comply with the standard, and then format it as hexadecimal groups.According to RFC 4122 -...
View ArticleAnswer by Ja͢ck for creating a random number using MYSQL
This should give what you want:FLOOR(RAND() * 401) + 100Generically, FLOOR(RAND() * (<max> - <min> + 1)) +<min> generates a number between <min> and <max>...
View ArticleAnswer by Ja͢ck for how to generate unique id in php based on current data...
With PHP 7 you can use this to generate a sufficiently random value based on the current timestamp:$s = time() . bin2hex(random_bytes(10));Do note that after 265 years (starting with 11 digits in 2286)...
View ArticleAnswer by Ja͢ck for join enumerable to produce string in Ruby
The code within your question is already as optimal as it gets, but it's possible to remove the condition inside your block:e = ('a'..'z').to_enumjoined_string = e.reduce { |acc, str| acc << '...
View ArticleAnswer by Ja͢ck for Search for part of an key in array - PHP
You can use a negative filter (with strpos) instead:foreach ($array as $key => $value) { if (strpos($key, 'array_') !== 0) { unset($array[$key]); }}DemoNote that it modifies the array...
View ArticleAnswer by Ja͢ck for Ruby - functional approach to filtering with index
After many years, Ruby 2.7 ships with Enumerable#filter_map() to make this simpler:stuff.filter_map.with_index { |elem, index| index if elem == '' }
View ArticleAnswer by Ja͢ck for Element ID from document.getElementsByClassName
Using in is not how you should iterate over an array of elements. You should use the .length property and use numeric indexing:for (var i = 0; i < divstopop.length; ++i) { // Get id property from...
View ArticleAnswer by Ja͢ck for PHP Regex: match character set OR end of string
To match a string that starts with a slash, followed by six alphanumerics and is then followed by either the end-of-string or something that's not...
View ArticleAnswer by Ja͢ck for Javascript passing arrays to functions by value, leaving...
Inside your function there's this:funcArray = new Array();funcArray = someArray;This won't actually copy someArray but instead reference it, which is why the original array is modified.You can use...
View ArticleAnswer by Ja͢ck for javascript: stop setInterval after element removal
Have you tried storing the intervalId on the #element itself using $.data?$('#element').mouseover(function() { var $this = $(this); $.post('ajax/ajax.php', function(data) { $('<div...
View ArticleAnswer by Ja͢ck for Can you include raw JSON in Guzzle POST Body?
You can send a regular array as JSON via the 'json' request option; this will also automatically set the right headers:$headers = ['NETOAPI_KEY' => env('NETO_API_KEY'),'Accept' =>...
View Article