Hi everybody, In prototype1.4 $H([1,2,3]) would yield #<Hash:{''0'':1, ''1'':2, ''2'':3}> which to me makes a lot of sense. It means you can basically treat structured Objects, containing a mixture of Hashes and Arrays the same: Cast them (recursivley) to Hash, iterate and (if necessary) judge by the key, if the thing was an array or hash. In prototype 1.5 $H([1,2,3]) yields [] That is: no Hash, not even the original array. That is quite counterintuitive and breaks a lot of code for us, for no obvious reason. Please consider, that even $H() gives me #<Hash:{}> an empty Hash that is. Can somebody explain if this is just an error or wanted behaviour. We are facing big problems because of that. Sincerely yours Alexander Presber --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi Alexander, Sorry to hear that you are encountering issues with your update to 1.5. Apart from the fact that it breaks some of your code - I know how painful that is - could you maybe give a bit more info on when such behavior is useful ? Enumerable is mixed it to Array just like its mixed in to Hash ($H). So I''m not sure I understand in which case you''d need to use $H([...]). Regards, Tobie --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> Can somebody explain if this is just an error or wanted behaviour. We > are facing big problems because of that.Prototype (in some ways) follows conventions of the Ruby language where Array and Hash aren''t interchangeable. This is unlike PHP where arrays are used as both. In other words, your data should be either an array or hash. We will still consider your issue, but in the meantime use the following function in your scripts to override the original: function $H(object) { if (object) { switch (object.constructor) { case Hash: return object; case Array: var h = new Hash; object.each(function(value, i){ h[i] = value }); return h; } } return new Hash(object); }; --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hello Tobie, I know $H() is a convenience function, mainly to extend objects to prototype hashes. But it accepts any argument, raising the question what sensible return value either input should yield. For the input {a:''foo'', b:''bar''} the output #<Hash:{a:''foo'', b:''bar''}> is obviously what we want. For [''foo'', ''bar''] things are not as clear. IMHO, #<Hash:{''0'':''foo'', ''1'':''bar''}> (as returned by prototype 1.4) is perfectly ok. Another possible result might be #<Hash:{}>, null, ''undefined'' or an exception. But the actual result is [], that is neither consistent, nor, in any way I can imagine, useful. $H() should be expected to return Hashes or undefined, not Arrays. I can give you an example for usage: Imagine an arbitrarily deep nested structure of js-objects and arrays called params. I could traverse that completely in 1.4, using $H(params).each(function(param){ name = param[0]; value = param[1]; if (Number(name) != name) { // array case, if needed } if (isObject(paramValue)){ // recursively call function } else { // scalar, do something } }); Now that won''t work anymore, I would have to do typechecking first. I don''t say the behaviour in prototype 1.4 is the only way to go, for me it was perfect. But the newer behaviour is definitely worth reconsidering. Kind regards, Alex On Feb 3, 11:15 am, "tobie" <tobie.lan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi Alexander, > > Sorry to hear that you are encountering issues with your update to > 1.5. > > Apart from the fact that it breaks some of your code - I know how > painful that is - could you maybe give a bit more info on when such > behavior is useful ? > > Enumerable is mixed it to Array just like its mixed in to Hash ($H). > So I''m not sure I understand in which case you''d need to use > $H([...]). > > Regards, > > Tobie--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Feb 3, 7:00 pm, "Mislav" <mislav.maroh...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Can somebody explain if this is just an error or wanted behaviour. We > > are facing big problems because of that. > > Prototype (in some ways) follows conventions of the Ruby language > where Array and Hash aren''t interchangeable. This is unlike PHP where > arrays are used as both. > > In other words, your data should be either an array or hash.That goes without mentioning. The question I wanted to raise was, if $H() shouldn''t be expected to reliably return Hash objects or undefined/exception. If the answer is Hashes (as it was), it would still be have to figured out, how to do that for arbitrary input. E.g. $H([''a'',''b'']) => #<Hash{''0'':''a'', ''1'':''b''}; $H(1) => #<Hash{''0'':1}, or the like Kind regards Alex> We will still consider your issue, but in the meantime use the > following function in your scripts to override the original: > > function $H(object) { > if (object) { > switch (object.constructor) { > case Hash: return object; > case Array: > var h = new Hash; > object.each(function(value, i){ h[i] = value }); > return h; > } > } > return new Hash(object); > > };--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---