Thank you Christophe, that clears up a lot of confusion over what I read in
the docs.


Daniel


-----Original Message-----
From: [email protected]
[mailto:[EMAIL PROTECTED] On Behalf Of Christophe
Porteneuve
Sent: Monday, July 02, 2007 12:44 PM
To: [email protected]
Subject: [Rails-spinoffs] Re: Length of a Hash?


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
-~----------~----~----~----~------~----~------~--~---

Reply via email to