Hello,
I''m in the middle of trying to upgrade my application to take
advantage of the new Hash class in Prototype 1.6 and I can''t help but
think that I am missing something about how it''s supposed to work. I
use many multi dimensional Hashes throughout my code and am not sure
how I''m supposed to get() or set() anything below the first level.
Given:
var multilevelHash =
$H({"1":{"name":"Blue","hex":"CCFF00"},"2":
{"name":"Red","hex":"FFCC00"},"3":{"name":"Green","hex":"00FFCC"}});
Setting a value in a multidimensional Hash used to be as simple as:
multilevelHash[''1''][''name''] =
''Red'';
With the new Hash class, this is the only working code I can generate:
multilevelHash.set(''1'',
$H(multilevelHash.get(''1'')).merge({''name'' :
''Red''}));
Is it really that complex now? Or am I missing something? I generated
this simplified example, but my actual working code is much more
complex, so some of my h.set( key, $H(h.get(key)).merge({ })) will be
even deeper nested and complex.
I know I can directly access the h._object but I also know I''m not
"supposed to."
Thanks in advance for any help you can provide!
--~--~---------~--~----~------------~-------~--~----~
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,
Technically, your example isn''t a multi-dimensional hash, it''s
a hash
containing objects.
Furthermore, it looks like you''re using a hash when what you should
really be using is an array (your keys are numbers).
I suggest you try using the following.
var colors = [
{
name: "Blue",
hex: "CCFF00"
},
{
name: "Red",
hex: "FFCC00"
}
];
Should you really need multi-dimensional hashes, here''s how to do it:
var colors = $H({
blue: $H({r: ''CC'', g: ''FF'', b:
''00''}),
red: $H({r: ''FF'', g: ''CC'', b:
''00''})
});
colors.set(''yellow'', $H({r: ''00'', g:
''FF'', b: ''CC''}));
will yield the following:
$H({
blue: $H({r: ''CC'', g: ''FF'', b:
''00''}),
red: $H({r: ''FF'', g: ''CC'', b:
''00''}),
yellow: $H({r: ''00'', g: ''FF'', b:
''CC''})
});
modifying the r value of yellow:
colors.get(''yellow'').set(''r'',
''FF'');
will yield the following:
$H({
blue: $H({r: ''CC'', g: ''FF'', b:
''00''}),
red: $H({r: ''FF'', g: ''CC'', b:
''00''}),
yellow: $H({r: ''FF'', g: ''FF'', b:
''CC''})
});
Finally, if you''re using objects inside of a hash (rather than nested
hashes):
var colors_2 = $H({
blue: {r: ''CC'', g: ''FF'', b:
''00''},
red: {r: ''FF'', g: ''CC'', b:
''00''}
});
colors_2.set(''yellow'', {r: ''00'', g:
''FF'', b: ''CC''});
will yield the following:
$H({
blue: {r: ''CC'', g: ''FF'', b:
''00''},
red: {r: ''FF'', g: ''CC'', b:
''00''},
yellow: {r: ''00'', g: ''FF'', b:
''CC''}
});
modifying the r value of yellow:
colors.get(''yellow'').r = ''FF''; or:
colors.get(''yellow'')[''r''] =
''FF'';
will yield the following:
var colors = $H({
blue: {r: ''CC'', g: ''FF'', b:
''00''},
red: {r: ''FF'', g: ''CC'', b:
''00''},
yellow: {r: ''FF'', g: ''FF'', b:
''CC''}
});
Hope this helps,
Tobie
On Dec 19, 8:07 pm, whoisgregg
<gregg.hilferd...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Hello,
>
> I''m in the middle of trying to upgrade my application to take
> advantage of the new Hash class in Prototype 1.6 and I can''t help
but
> think that I am missing something about how it''s supposed to work.
I
> use many multi dimensional Hashes throughout my code and am not sure
> how I''m supposed to get() or set() anything below the first level.
>
> Given:
> var multilevelHash =
$H({"1":{"name":"Blue","hex":"CCFF00"},"2":
>
{"name":"Red","hex":"FFCC00"},"3":{"name":"Green","hex":"00FFCC"}});
>
> Setting a value in a multidimensional Hash used to be as simple as:
> multilevelHash[''1''][''name''] =
''Red'';
>
> With the new Hash class, this is the only working code I can generate:
> multilevelHash.set(''1'',
$H(multilevelHash.get(''1'')).merge({''name'' :
> ''Red''}));
>
> Is it really that complex now? Or am I missing something? I generated
> this simplified example, but my actual working code is much more
> complex, so some of my h.set( key, $H(h.get(key)).merge({ })) will be
> even deeper nested and complex.
>
> I know I can directly access the h._object but I also know I''m not
> "supposed to."
>
> Thanks in advance for any help you can provide!
--~--~---------~--~----~------------~-------~--~----~
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 Tobie, It does help immensely! Thank you for clarifying how the new Hash class works and for helping me to better understand hash/array/objects. :) Thanks again, Gregg On Dec 19, 2:33 pm, Tobie Langel <tobie.lan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > Technically, your example isn''t a multi-dimensional hash, it''s a hash > containing objects. > > Furthermore, it looks like you''re using a hash when what you should > really be using is an array (your keys are numbers). > > I suggest you try using the following. > > var colors = [ > { > name: "Blue", > hex: "CCFF00" > }, > { > name: "Red", > hex: "FFCC00" > } > ]; > > Should you really need multi-dimensional hashes, here''s how to do it: > > var colors = $H({ > blue: $H({r: ''CC'', g: ''FF'', b: ''00''}), > red: $H({r: ''FF'', g: ''CC'', b: ''00''}) > > }); > > colors.set(''yellow'', $H({r: ''00'', g: ''FF'', b: ''CC''})); > > will yield the following: > > $H({ > blue: $H({r: ''CC'', g: ''FF'', b: ''00''}), > red: $H({r: ''FF'', g: ''CC'', b: ''00''}), > yellow: $H({r: ''00'', g: ''FF'', b: ''CC''}) > > }); > > modifying the r value of yellow: > colors.get(''yellow'').set(''r'', ''FF''); > > will yield the following: > > $H({ > blue: $H({r: ''CC'', g: ''FF'', b: ''00''}), > red: $H({r: ''FF'', g: ''CC'', b: ''00''}), > yellow: $H({r: ''FF'', g: ''FF'', b: ''CC''}) > > }); > > Finally, if you''re using objects inside of a hash (rather than nested > hashes): > > var colors_2 = $H({ > blue: {r: ''CC'', g: ''FF'', b: ''00''}, > red: {r: ''FF'', g: ''CC'', b: ''00''} > > }); > > colors_2.set(''yellow'', {r: ''00'', g: ''FF'', b: ''CC''}); > > will yield the following: > > $H({ > blue: {r: ''CC'', g: ''FF'', b: ''00''}, > red: {r: ''FF'', g: ''CC'', b: ''00''}, > yellow: {r: ''00'', g: ''FF'', b: ''CC''} > > }); > > modifying the r value of yellow: > colors.get(''yellow'').r = ''FF''; or: > colors.get(''yellow'')[''r''] = ''FF''; > > will yield the following: > > var colors = $H({ > blue: {r: ''CC'', g: ''FF'', b: ''00''}, > red: {r: ''FF'', g: ''CC'', b: ''00''}, > yellow: {r: ''FF'', g: ''FF'', b: ''CC''} > > }); > > Hope this helps, > > Tobie > > On Dec 19, 8:07 pm, whoisgregg <gregg.hilferd...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hello, > > > I''m in the middle of trying to upgrade my application to take > > advantage of the new Hash class in Prototype 1.6 and I can''t help but > > think that I am missing something about how it''s supposed to work. I > > use many multi dimensional Hashes throughout my code and am not sure > > how I''m supposed to get() or set() anything below the first level. > > > Given: > > var multilevelHash = $H({"1":{"name":"Blue","hex":"CCFF00"},"2": > > {"name":"Red","hex":"FFCC00"},"3":{"name":"Green","hex":"00FFCC"}}); > > > Setting a value in a multidimensional Hash used to be as simple as: > > multilevelHash[''1''][''name''] = ''Red''; > > > With the new Hash class, this is the only working code I can generate: > > multilevelHash.set(''1'', $H(multilevelHash.get(''1'')).merge({''name'' : > > ''Red''})); > > > Is it really that complex now? Or am I missing something? I generated > > this simplified example, but my actual working code is much more > > complex, so some of my h.set( key, $H(h.get(key)).merge({ })) will be > > even deeper nested and complex. > > > I know I can directly access the h._object but I also know I''m not > > "supposed to." > > > Thanks in advance for any help you can provide!--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
My pleasure! On Dec 19, 9:40 pm, whoisgregg <gregg.hilferd...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi Tobie, > > It does help immensely! > > Thank you for clarifying how the new Hash class works and for helping > me to better understand hash/array/objects. :) > > Thanks again, > Gregg > > On Dec 19, 2:33 pm, Tobie Langel <tobie.lan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > > Technically, your example isn''t a multi-dimensional hash, it''s a hash > > containing objects. > > > Furthermore, it looks like you''re using a hash when what you should > > really be using is an array (your keys are numbers). > > > I suggest you try using the following. > > > var colors = [ > > { > > name: "Blue", > > hex: "CCFF00" > > }, > > { > > name: "Red", > > hex: "FFCC00" > > } > > ]; > > > Should you really need multi-dimensional hashes, here''s how to do it: > > > var colors = $H({ > > blue: $H({r: ''CC'', g: ''FF'', b: ''00''}), > > red: $H({r: ''FF'', g: ''CC'', b: ''00''}) > > > }); > > > colors.set(''yellow'', $H({r: ''00'', g: ''FF'', b: ''CC''})); > > > will yield the following: > > > $H({ > > blue: $H({r: ''CC'', g: ''FF'', b: ''00''}), > > red: $H({r: ''FF'', g: ''CC'', b: ''00''}), > > yellow: $H({r: ''00'', g: ''FF'', b: ''CC''}) > > > }); > > > modifying the r value of yellow: > > colors.get(''yellow'').set(''r'', ''FF''); > > > will yield the following: > > > $H({ > > blue: $H({r: ''CC'', g: ''FF'', b: ''00''}), > > red: $H({r: ''FF'', g: ''CC'', b: ''00''}), > > yellow: $H({r: ''FF'', g: ''FF'', b: ''CC''}) > > > }); > > > Finally, if you''re using objects inside of a hash (rather than nested > > hashes): > > > var colors_2 = $H({ > > blue: {r: ''CC'', g: ''FF'', b: ''00''}, > > red: {r: ''FF'', g: ''CC'', b: ''00''} > > > }); > > > colors_2.set(''yellow'', {r: ''00'', g: ''FF'', b: ''CC''}); > > > will yield the following: > > > $H({ > > blue: {r: ''CC'', g: ''FF'', b: ''00''}, > > red: {r: ''FF'', g: ''CC'', b: ''00''}, > > yellow: {r: ''00'', g: ''FF'', b: ''CC''} > > > }); > > > modifying the r value of yellow: > > colors.get(''yellow'').r = ''FF''; or: > > colors.get(''yellow'')[''r''] = ''FF''; > > > will yield the following: > > > var colors = $H({ > > blue: {r: ''CC'', g: ''FF'', b: ''00''}, > > red: {r: ''FF'', g: ''CC'', b: ''00''}, > > yellow: {r: ''FF'', g: ''FF'', b: ''CC''} > > > }); > > > Hope this helps, > > > Tobie > > > On Dec 19, 8:07 pm, whoisgregg <gregg.hilferd...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Hello, > > > > I''m in the middle of trying to upgrade my application to take > > > advantage of the new Hash class in Prototype 1.6 and I can''t help but > > > think that I am missing something about how it''s supposed to work. I > > > use many multi dimensional Hashes throughout my code and am not sure > > > how I''m supposed to get() or set() anything below the first level. > > > > Given: > > > var multilevelHash = $H({"1":{"name":"Blue","hex":"CCFF00"},"2": > > > {"name":"Red","hex":"FFCC00"},"3":{"name":"Green","hex":"00FFCC"}}); > > > > Setting a value in a multidimensional Hash used to be as simple as: > > > multilevelHash[''1''][''name''] = ''Red''; > > > > With the new Hash class, this is the only working code I can generate: > > > multilevelHash.set(''1'', $H(multilevelHash.get(''1'')).merge({''name'' : > > > ''Red''})); > > > > Is it really that complex now? Or am I missing something? I generated > > > this simplified example, but my actual working code is much more > > > complex, so some of my h.set( key, $H(h.get(key)).merge({ })) will be > > > even deeper nested and complex. > > > > I know I can directly access the h._object but I also know I''m not > > > "supposed to." > > > > Thanks in advance for any help you can provide!--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---