Ruby – Access multidimensional hash and avoid access nil object [duplicate]

There are many approaches to this. If you use Ruby 2.3 or above, you can use dig my_hash.dig(‘key1’, ‘key2’, ‘key3’) Plenty of folks stick to plain ruby and chain the && guard tests. You could use stdlib Hash#fetch too: my_hash.fetch(‘key1’, {}).fetch(‘key2’, {}).fetch(‘key3′, nil) Some like chaining ActiveSupport’s #try method. my_hash.try(:[], ‘key1’).try(:[], ‘key2’).try(:[], ‘key3’) Others use … Read more

Swift inline conditional?

If you’re looking for a one-liner to do that, you can pull the ?: operation out of the string interpolation and concatenate with + instead: let fileExists = false // for example println(“something ” + (fileExists ? “exists” : “does not exist”)) Outputs: something does not exist

Conditional build based on environment using Webpack

You can use the define plugin. I use it by doing something as simple as this in your webpack build file where env is the path to a file that exports an object of settings: // Webpack build config plugins: [ new webpack.DefinePlugin({ ENV: require(path.join(__dirname, ‘./path-to-env-files/’, env)) }) ] // Settings file located at `path-to-env-files/dev.js` … Read more

MySQL Conditional Insert

If your DBMS does not impose limitations on which table you select from when you execute an insert, try: INSERT INTO x_table(instance, user, item) SELECT 919191, 123, 456 FROM dual WHERE NOT EXISTS (SELECT * FROM x_table WHERE user = 123 AND item = 456) In this, dual is a table with one row only … Read more