emma.persky-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-May-20 21:05 UTC
Enumerable.each() and custom object functions?
Hi, I have code for a custom object (this is just a mockup, but the principal applies): var Player = Class.create(); Player.prototype = { initialize: function(_id) { this.id = _id; } something: function() { alert(this.id); } } I also have some code that creates the objects in a for loop (that reads some xml fyi) and stashes them away in a hash. All fine and dandy. I can access these objects from this hash by calling myhash[some_id].something(); and I get a lovely alert!! However, what I would like to do is call the something function for all members of the hash (ok so I don;t want them to display a message box, but again, the principal applies) I assumed i could use myhash.each( function(item) { item.something(); } ); but i can''t, all i get "item.something is not a function" Any ideas? In the meantime i have solved this by doing at keys() then looping over the keys and calling myhash[keys[loop_var]].something() which seems really obtuse!!! Thanks for any help!! esp05 --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
If you want to invoke the same method for all items in an Enumerable you can use invoke: http://www.prototypejs.org/api/enumerable/invoke e.g., myEnum.invoke(''something'') On 5/20/07, emma.persky-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <emma.persky-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi, > > I have code for a custom object (this is just a mockup, but the > principal applies): > > var Player = Class.create(); > Player.prototype = { > initialize: function(_id) { > this.id = _id; > } > > something: function() { > alert(this.id); > } > } > > I also have some code that creates the objects in a for loop (that > reads some xml fyi) and stashes them away in a hash. All fine and > dandy. I can access these objects from this hash by calling > > myhash[some_id].something(); > > and I get a lovely alert!! > > However, what I would like to do is call the something function for > all members of the hash (ok so I don;t want them to display a message > box, but again, the principal applies) > > I assumed i could use > > myhash.each( function(item) { item.something(); } ); > > but i can''t, all i get > > "item.something is not a function" > > Any ideas? In the meantime i have solved this by doing at keys() then > looping over the keys and calling myhash[keys[loop_var]].something() > which seems really obtuse!!! > > Thanks for any help!! > > esp05 > > > > >-- Jesse E.I. Farmer e: jesse-h8Qh2m8E5SHQT0dZR+AlfA@public.gmane.org w: http://20bits.com --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Christophe Porteneuve
2007-May-21 06:41 UTC
Re: Enumerable.each() and custom object functions?
Hey Emma, What exactly do you call a hash? A *Hash* instance? Or just a good ol'' JavaScript object? Or even an Array, perhaps? Now, if you''re indeed using a Hash, look at the docs to see that enumerating a hash gets the *pairs*, not the values alone. So you''d need to access the pair''s 1-indexed property, or "value" property (they''re basically the same, in two notations), and call your method on it. var h = new Hash(); h[''john''] = new Player(''john''); h[''mary''] = new Player(''mary''); h.each(function(pair) { pair[1].something(); }); // or: h.each(function(pair) { pair.value.something(); }); Because of this, invoke won''t work as expected, since it requires the invoked method to be present in the passed object. But you can trick it: h.pluck(''value'').invoke(''something''); Check out the docs for these: http://prototypejs.org/api/hash/each http://prototypejs.org/api/enumerable/invoke http://prototypejs.org/api/enumerable/pluck ''HTH, -- Christophe Porteneuve a.k.a. TDD "[They] did not know it was impossible, so they did it." --Mark Twain Email: tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
emma.persky-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-May-21 23:57 UTC
Re: Enumerable.each() and custom object functions?
Heya, thanks. I think i was having a slow day. I didn''t even think to try value.something()!!!! It seems a little odd that you get back the pair when iterating, and not the value, given that the point of a hash is retreiving the values by key or iteration overall). Atleast its now here and documented a little more clearly for all to read!! Emma xx On May 21, 7:41 am, Christophe Porteneuve <t...-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org> wrote:> Hey Emma, > > What exactly do you call a hash? A *Hash* instance? Or just a good ol'' > JavaScript object? Or even an Array, perhaps? > > Now, if you''re indeed using a Hash, look at the docs to see that > enumerating a hash gets the *pairs*, not the values alone. So you''d > need to access the pair''s 1-indexed property, or "value" property > (they''re basically the same, in two notations), and call your method on it. > > var h = new Hash(); > h[''john''] = new Player(''john''); > h[''mary''] = new Player(''mary''); > > h.each(function(pair) { pair[1].something(); }); > // or: > h.each(function(pair) { pair.value.something(); }); > > Because of this, invoke won''t work as expected, since it requires the > invoked method to be present in the passed object. But you can trick it: > > h.pluck(''value'').invoke(''something''); > > Check out the docs for these: > > http://prototypejs.org/api/hash/each > http://prototypejs.org/api/enumerable/invoke > http://prototypejs.org/api/enumerable/pluck > > ''HTH, > > -- > Christophe Porteneuve a.k.a. TDD > "[They] did not know it was impossible, so they did it." --Mark Twain > Email: t...-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---