Daniel Eben Elmore a écrit :
> What's the best way to get the length of a hash? ie. the number of keys?
There's no really fast way, so you can go short or efficient. Short:
- with an actual Hash: hash.size()
- with a regular object: Object.keys(o).length
Efficient: you'll need to write custom functions, /a la RobG/ ;-):
- with an actual Hash:
// Post Enumerable mix-in:
Hash.prototype.size = function() {
var result = 0;
for (var key in this) {
if (this[key] && this[key] == Hash.prototype[key]) continue;
++result;
}
return result;
};
- with a regular object:
Object.getKeyCount = function(o) {
var result = 0;
for (var key in o)
++resule;
return result;
};
// Then: Object.getKeyCount(o)
'HTH
--
Christophe Porteneuve a.k.a. TDD
"[They] did not know it was impossible, so they did it." --Mark Twain
Email: [EMAIL PROTECTED]
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---