Why will ES6 WeakMap’s not be enumerable?

Finally found the real answer: http://tc39wiki.calculist.org/es6/weak-map/ A key property of Weak Maps is the inability to enumerate their keys. This is necessary to prevent attackers observing the internal behavior of other systems in the environment which share weakly-mapped objects. Should the number or names of items in the collection be discoverable from the API, even … Read more

What’s the fastest way in Ruby to get the first enumerable element for which a block returns true?

Several core ruby classes, including Array and Hash include the Enumerable module which provides many useful methods to work with these enumerations. This module provides the find or detect methods which do exactly what you want to achieve: arr = [12, 88, 107, 500] arr.find { |num| num > 100 } # => 107 Both … Read more

Transform a DataTable into Dictionary C#

The generic method ToDictionary has 3 parameters. You left one off, so it doesn’t know what to do. If you want to specify all of the parameters, it would be <DataRow, string, object>. internal Dictionary<string,object> GetDict(DataTable dt) { return dt.AsEnumerable() .ToDictionary<DataRow, string, object>(row => row.Field<string>(0), row => row.Field<object>(1)); } Of course, if you leave them … Read more

Group hashes by keys and sum the values

ar = [{“Vegetable”=>10}, {“Vegetable”=>5}, {“Dry Goods”=>3}, {“Dry Goods”=>2}] p ar.inject{|memo, el| memo.merge( el ){|k, old_v, new_v| old_v + new_v}} #=> {“Vegetable”=>15, “Dry Goods”=>5} Hash.merge with a block runs the block when it finds a duplicate; inject without a initial memo treats the first element of the array as memo, which is fine here.

What are the benefits of making properties non-enumerable?

I think the main benefit is to be able to control what shows up when enumerating an object’s properties, such as for in or Object.keys(). MDN explains it well with Object.defineProperty: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/defineProperty So normally, when people want to add a method to Object, such as a polyfill for some method not supported in old browsers, … Read more

Skip over iteration in Enumerable#collect

There is method Enumerable#reject which serves just the purpose: (1..4).reject{|x| x == 3}.collect{|x| x + 1} The practice of directly using an output of one method as an input of another is called method chaining and is very common in Ruby. BTW, map (or collect) is used for direct mapping of input enumerable to the … Read more

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