How to avoid NoMethodError for missing elements in nested hashes, without repeated nil checks?

Ruby 2.3.0 introduced a method called dig on both Hash and Array. name = params.dig(:company, :owner, :name) It returns nil if the key is missing at any level. If you are using a version of Ruby older than 2.3, you can install a gem such as ruby_dig or hash_dig_and_collect, or implement the functionality yourself: module … Read more

Ruby – getting value of hash

Convert the key from a string to a symbol, and do a lookup in the hash. hash = {:key1 => “value1”, :key2 => “value2”} k = ‘key1′ hash[k.to_sym] # or iow, hash[:key1], which will return “value1” Rails uses this class called HashWithIndifferentAccess that proves to be very useful in such cases. I know that you’ve … Read more

Concise way to combine field hashcodes?

EDIT: System.HashCode has now been released. The recommended way of creating hashcodes is now this: public override int GetHashCode() { return HashCode.Combine(fieldA, fieldB, fieldC); } System.HashCode.Combine() will internally call .GetHashCode() on each field, and do the right thing automatically. For very many fields (more than 8), you can create an instance of HashCode and then … Read more

Git is moving to new hashing algorithm SHA-256 but why git community settled on SHA‑256

I have presented that move in “Why doesn’t Git use more modern SHA?” in Aug. 2018 The reasons were discussed here by Brian M. Carlson: I’ve implemented and tested the following algorithms, all of which are 256-bit (in alphabetical order): BLAKE2b (libb2) BLAKE2bp (libb2) KangarooTwelve (imported from the Keccak Code Package) SHA-256 (OpenSSL) SHA-512/256 (OpenSSL) … Read more

Iterate over a deeply nested level of hashes in Ruby

If I understand the goal, then you should be able to pass in the parent to your save method. For the top level, it will be nil. The following shows the idea where puts is used as a place holder for the “save”. def save_pair(parent, myHash) myHash.each {|key, value| value.is_a?(Hash) ? save_pair(key, value) : puts(“parent=#{parent.nil? … Read more

What are options hashes?

Options hash is a nice concept enabled by a feature of ruby parser. Say, you have a method with some required arguments. Also you may pass some optional arguments. Over time you may add more optional arguments or remove old ones. To keep method declaration clean and stable, you can pass all those optional arguments … Read more

Cleanest way to create a Hash from an Array

There is already a method in ActiveSupport that does this. [‘an array’, ‘of active record’, ‘objects’].index_by(&:id) And just for the record, here’s the implementation: def index_by inject({}) do |accum, elem| accum[yield(elem)] = elem accum end end Which could have been refactored into (if you’re desperate for one-liners): def index_by inject({}) {|hash, elem| hash.merge!(yield(elem) => elem) … Read more

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