How do I search within an array of hashes by hash values in ruby?

You’re looking for Enumerable#select (also called find_all): @fathers.select {|father| father[“age”] > 35 } # => [ { “age” => 40, “father” => “Bob” }, # { “age” => 50, “father” => “Batman” } ] Per the documentation, it “returns an array containing all elements of [the enumerable, in this case @fathers] for which block is … Read more

Java List.contains(Object with field value equal to x)

Streams If you are using Java 8, perhaps you could try something like this: public boolean containsName(final List<MyObject> list, final String name){ return list.stream().filter(o -> o.getName().equals(name)).findFirst().isPresent(); } Or alternatively, you could try something like this: public boolean containsName(final List<MyObject> list, final String name){ return list.stream().map(MyObject::getName).filter(name::equals).findFirst().isPresent(); } This method will return true if the List<MyObject> contains … Read more

Exclude folders from Eclipse search

The quick and dirty way: Right click on a folder, go to properties, and mark a folder as derived. Derived entities are excluded from searching by default. The problem with this approach is that Eclipse “forgets” that the folder is derived if you delete it (either from within Eclipse or externally, followed by refreshing the … Read more

Case-insensitive search

Yeah, use .match, rather than .search. The result from the .match call will return the actual string that was matched itself, but it can still be used as a boolean value. var string = “Stackoverflow is the BEST”; var result = string.match(/best/i); // result == ‘BEST’; if (result){ alert(‘Matched’); } Using a regular expression like … Read more

Finding all possible combinations of numbers to reach a given sum

This problem can be solved with a recursive combinations of all possible sums filtering out those that reach the target. Here is the algorithm in Python: def subset_sum(numbers, target, partial=[]): s = sum(partial) # check if the partial sum is equals to target if s == target: print “sum(%s)=%s” % (partial, target) if s >= … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)