[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Hash two dimensions

Mario Ruiz

1/22/2008 12:42:00 PM

Hi Everybody!
I'm doing this:
$Config=Hash.new(Hash.new())
$Config["UNO"]["urlX"]="beep/UNO/playdoit.do"
$Config["UNO"]["urlY"]="beep/UNO/dontplayPP.do"
$Config["UNO"]["urlZ"]="beep/UNO/dontplay34jo.do"
$Config["DOS"]["urlX"]="beep/DOS/playad.do"
$Config["DOS"]["urlY"]="beep/DOS/dontplayf.do"
$Config["DOS"]["urlZ"]="beep/DOS/dontplay34aa.do"
$Config["TRES"]["urlX"]="beep/TRES/playcs.do"
$Config["TRES"]["urlY"]="beep/TRES/dontplayff.do"
$Config["TRES"]["urlZ"]="beep/TRES/dontplay34s.do"

when I try to access to the value in $Config["UNO"]["urlY"] the result
is the value in $Config[TRES"]["urlY"]

Why??

Thanks a lot.
--
Posted via http://www.ruby-....

7 Answers

Austin Ziegler

1/22/2008 12:51:00 PM

0

On Jan 22, 2008 7:42 AM, Mario Ruiz <mario@betware.com> wrote:
> Hi Everybody!
> I'm doing this:
> $Config=Hash.new(Hash.new())
> $Config["UNO"]["urlX"]="beep/UNO/playdoit.do"
> $Config["UNO"]["urlY"]="beep/UNO/dontplayPP.do"
> $Config["UNO"]["urlZ"]="beep/UNO/dontplay34jo.do"
> $Config["DOS"]["urlX"]="beep/DOS/playad.do"
> $Config["DOS"]["urlY"]="beep/DOS/dontplayf.do"
> $Config["DOS"]["urlZ"]="beep/DOS/dontplay34aa.do"
> $Config["TRES"]["urlX"]="beep/TRES/playcs.do"
> $Config["TRES"]["urlY"]="beep/TRES/dontplayff.do"
> $Config["TRES"]["urlZ"]="beep/TRES/dontplay34s.do"
>
> when I try to access to the value in $Config["UNO"]["urlY"] the result
> is the value in $Config[TRES"]["urlY"]
>
> Why??

Because it's always the same hash. What you *want* is:

$Config = Hash.new { |h, k| h[k] = Hash.new }

-austin
--
Austin Ziegler * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca

Mario Ruiz

1/22/2008 12:53:00 PM

0

Yeah it's working.

Thank you very much.
--
Posted via http://www.ruby-....

jandot

1/22/2008 1:38:00 PM

0

I had this problem a lot when I started to use ruby, especially coming
from perl with its hashes of arrays of arrays of hashes.

After a while I realized that these nested structures basically
pointed out that I should create a class to contain this type of data.
And just put those objects in an array. So for your example:

class Setting
def initialize(platform, setting, value)
@platform, @setting, @value = platform, setting, value
end
attr_accessor :platform, :setting, :value
end

configuration = Array.new
configuration.push(Setting.new("UNO", "urlX", "beep/UNO/playdoit.do"))
configuration.push(Setting.new("UNO", "urlY", "beep/UNO/
dontplayPP.do"))
....

Thomas Wieczorek

1/22/2008 2:28:00 PM

0

On Jan 22, 2008 2:39 PM, jandot <jan.aerts@gmail.com> wrote:
>
> class Setting
> def initialize(platform, setting, value)
> @platform, @setting, @value = platform, setting, value
> end
> attr_accessor :platform, :setting, :value
> end
>
> configuration = Array.new
> configuration.push(Setting.new("UNO", "urlX", "beep/UNO/playdoit.do"))
> configuration.push(Setting.new("UNO", "urlY", "beep/UNO/
> dontplayPP.do"))
>

You can do the same with Ruby's built-in Structs with less code:

Setting = Struct.new(:platform, :setting, :value)
configuration = Array.new
configuration.push(Setting.new("UNO", "urlX", "beep/UNO/playdoit.do"))
configuration.push(Setting.new("UNO", "urlY", "beep/UNO/dontplayPP.do"))
last = configuration.pop
puts last.platform

I also like that one
http://groups.google.de/group/comp.lang.ruby/msg/d11d68...

abelard

2/20/2009 11:28:00 PM

0

On Fri, 20 Feb 2009 22:43:29 GMT, "aaa" <xxx@aol.com> wrote:

>abelard wrote:
>
>> On Fri, 20 Feb 2009 14:29:08 +0000, Maria
>> <oldwoman@stupidnewsreader.com> wrote:
>>
>> > Brown keeps saying this, but I have a question.
>> > A society seems to function most efficiently when each individual
>> > or orgnasation is doing his best *for himself*. The other thing is
>> > called socialism, where the component parts are trying to act in
>> > concert for the benefit of everyone, as has been proved inefficient
>> > and demoralising time and time again.
>> > So surely the best way for the world to recover is for each country
>> > to do what is best for that country (which obviously they naturally
>> > will), and ultimately they will once again be in a position to
>> > trade and act globally. Isn't it counter-productive for nations to
>> > be forcing themselves to maintain global trade and ties, if that
>> > means that the component nations go down the pan? Why should any
>> > other country be forced to support Brown's vain attempts to prop up
>> > failing organisations, if theirs are ok, and why should we do the
>> > same when we are struggling to maintain ourselves? It just doesn't
>> > make sense to me.
>>
>> the clown is an ignorant fool....at best he's repeating things he
>> hears second hand...
>>
>> re protectionism....
>> if you are running a company...and a piece of necessary
>> productive machinery cost twice as much in the uk as in china....
>> then the company that buys british undermines its own competitive
>> position...
>>
>> that is bad for the company...bad for those who work in that
>> company...and by extension bad for the country...
>>
>> if you want to see the way to break your country...look at the
>> end mess in every socialist protectionist country....
>>
>> a major way the clown has destroyed britain is stealing from
>> industry to indulge his own foolish and narrow vanities....
>> thus taking resources from productivity to waste...

>Protectionism works well if you have a weak industrial base, like the
>UK.
>
>In the example you give, yes the company that buys british will be at a
>disadvantage, because the other company is allowed to buy from China.
>
>Which is why you need laws to restrict all British companies buying
>from China. Pass such a law, and jobs in the UK will increase.

that just means all your companies will be put at an economic
disadvantage...
that gives you n. korea or the union of soviet socialist republics....

*any* country which avoids protectionism wins...

protectionism is self inflicted damage...not only does it raise your
own costs...your subsides go to those who practice free trade...

>It is a bit like banning slavery, until slaves were illegal, companies
>refusing the use slaves were at a disadvantage, so many companies were
>forced to use slaves.

i've no idea what the full economics of slavery would be in a modern
society....it didn't help national socialist german, russia or n.
korea
its modern surrogate, cheap immigrant labour, seems to undermine the
short term situation of the very poorest and have little effect on
the rest...

assessing the full economic/social effects of a policy is not simple

--
web site at www.abelard.org - news comment service, logic, economics
energy, education, politics, etc 1,552,396 document calls in year past
--------------------------------------------------------------------------------
all that is necessary for [] walk quietly and carry
the triumph of evil is that [] a big stick.
good people do nothing [] trust actions not words
only when it's funny -- roger rabbit
--------------------------------------------------------------------------------

aaa

2/21/2009 2:19:00 PM

0

abelard wrote:

> On Fri, 20 Feb 2009 22:43:29 GMT, "aaa" <xxx@aol.com> wrote:
>
> > abelard wrote:
> >
> >> On Fri, 20 Feb 2009 14:29:08 +0000, Maria
> >> <oldwoman@stupidnewsreader.com> wrote:
> >>
> >> > Brown keeps saying this, but I have a question.
> >> > A society seems to function most efficiently when each individual
> >> > or orgnasation is doing his best *for himself*. The other thing
> is >> > called socialism, where the component parts are trying to act
> in >> > concert for the benefit of everyone, as has been proved
> inefficient >> > and demoralising time and time again.
> >> > So surely the best way for the world to recover is for each
> country >> > to do what is best for that country (which obviously
> they naturally >> > will), and ultimately they will once again be in
> a position to >> > trade and act globally. Isn't it
> counter-productive for nations to >> > be forcing themselves to
> maintain global trade and ties, if that >> > means that the component
> nations go down the pan? Why should any >> > other country be forced
> to support Brown's vain attempts to prop up >> > failing
> organisations, if theirs are ok, and why should we do the >> > same
> when we are struggling to maintain ourselves? It just doesn't >> >
> make sense to me. >>
> >> the clown is an ignorant fool....at best he's repeating things he
> >> hears second hand...
> >>
> >> re protectionism....
> >> if you are running a company...and a piece of necessary
> >> productive machinery cost twice as much in the uk as in
> china.... >> then the company that buys british undermines its own
> competitive >> position...
> >>
> >> that is bad for the company...bad for those who work in that
> >> company...and by extension bad for the country...
> >>
> >> if you want to see the way to break your country...look at the
> >> end mess in every socialist protectionist country....
> >>
> >> a major way the clown has destroyed britain is stealing from
> >> industry to indulge his own foolish and narrow vanities....
> >> thus taking resources from productivity to waste...
>
> > Protectionism works well if you have a weak industrial base, like
> > the UK.
> >
> > In the example you give, yes the company that buys british will be
> > at a disadvantage, because the other company is allowed to buy from
> > China.
> >
> > Which is why you need laws to restrict all British companies buying
> > from China. Pass such a law, and jobs in the UK will increase.
>
> that just means all your companies will be put at an economic
> disadvantage...
> that gives you n. korea or the union of soviet socialist republics....

Both of these countries suffered due to a central planning type system,
not because of any protectionism. The USSR had a trade system
throughout the warsaw pact, so the USSR was not even protectionist.

Protectionism is not about central planning, it is merely introducing
new laws which companies have to comply with. Companies are still free
to make there own decisions within the law (just as today).

> *any* country which avoids protectionism wins...
>
> protectionism is self inflicted damage...not only does it raise your
> own costs...your subsides go to those who practice free trade...

Not true. Most countries benefit enormously from protectionism.

The USA operated protectionism during the 19th century.
Had it not been protectionist, it would have almost always been cheaper
to import from Europe for many goods, so its industrial sector /
capacity would not have developed to anything like its strength.

Only when its industrial sector had grown to a certain "critical mass"
(as a result of protectionism), did the USA change to a more free world
trade approach.

If you are strong, then the free world trade approach will help you, if
you are not so strong, the free world trade will destroy you.

Having your industrial base completely destroyed by free world trade
competition will destroy any prospects for your country.


> > It is a bit like banning slavery, until slaves were illegal,
> > companies refusing the use slaves were at a disadvantage, so many
> > companies were forced to use slaves.
>
> i've no idea what the full economics of slavery would be in a modern
> society....it didn't help national socialist german, russia or n.
> korea
> its modern surrogate, cheap immigrant labour, seems to undermine the
> short term situation of the very poorest and have little effect on
> the rest...
>
> assessing the full economic/social effects of a policy is not simple

The point is that we (as a nation) should set the rules, to enable the
market to help our nation/society. We should not allow ourselves to be
slaves to market forces.

abelard

2/21/2009 5:14:00 PM

0

On Sat, 21 Feb 2009 14:18:39 GMT, "aaa" <xxx@aol.com> wrote:

>abelard wrote:
>
>> On Fri, 20 Feb 2009 22:43:29 GMT, "aaa" <xxx@aol.com> wrote:
>>
>> > abelard wrote:
>> >
>> >> On Fri, 20 Feb 2009 14:29:08 +0000, Maria
>> >> <oldwoman@stupidnewsreader.com> wrote:
>> >>
>> >> > Brown keeps saying this, but I have a question.
>> >> > A society seems to function most efficiently when each individual
>> >> > or orgnasation is doing his best *for himself*. The other thing
>> is >> > called socialism, where the component parts are trying to act
>> in >> > concert for the benefit of everyone, as has been proved
>> inefficient >> > and demoralising time and time again.
>> >> > So surely the best way for the world to recover is for each
>> country >> > to do what is best for that country (which obviously
>> they naturally >> > will), and ultimately they will once again be in
>> a position to >> > trade and act globally. Isn't it
>> counter-productive for nations to >> > be forcing themselves to
>> maintain global trade and ties, if that >> > means that the component
>> nations go down the pan? Why should any >> > other country be forced
>> to support Brown's vain attempts to prop up >> > failing
>> organisations, if theirs are ok, and why should we do the >> > same
>> when we are struggling to maintain ourselves? It just doesn't >> >
>> make sense to me. >>
>> >> the clown is an ignorant fool....at best he's repeating things he
>> >> hears second hand...
>> >>
>> >> re protectionism....
>> >> if you are running a company...and a piece of necessary
>> >> productive machinery cost twice as much in the uk as in
>> china.... >> then the company that buys british undermines its own
>> competitive >> position...
>> >>
>> >> that is bad for the company...bad for those who work in that
>> >> company...and by extension bad for the country...
>> >>
>> >> if you want to see the way to break your country...look at the
>> >> end mess in every socialist protectionist country....
>> >>
>> >> a major way the clown has destroyed britain is stealing from
>> >> industry to indulge his own foolish and narrow vanities....
>> >> thus taking resources from productivity to waste...
>>
>> > Protectionism works well if you have a weak industrial base, like
>> > the UK.
>> >
>> > In the example you give, yes the company that buys british will be
>> > at a disadvantage, because the other company is allowed to buy from
>> > China.
>> >
>> > Which is why you need laws to restrict all British companies buying
>> > from China. Pass such a law, and jobs in the UK will increase.
>>
>> that just means all your companies will be put at an economic
>> disadvantage...
>> that gives you n. korea or the union of soviet socialist republics....
>
>Both of these countries suffered due to a central planning type system,
>not because of any protectionism. The USSR had a trade system
>throughout the warsaw pact, so the USSR was not even protectionist.
>
>Protectionism is not about central planning, it is merely introducing
>new laws which companies have to comply with. Companies are still free
>to make there own decisions within the law (just as today).

that is insufficient response.....
'centrally planned' economies tell you with whom you can
or cannot trade...
that is a good part of why they fail

>> *any* country which avoids protectionism wins...
>>
>> protectionism is self inflicted damage...not only does it raise your
>> own costs...your subsides go to those who practice free trade...
>
>Not true. Most countries benefit enormously from protectionism.
>
>The USA operated protectionism during the 19th century.
>Had it not been protectionist, it would have almost always been cheaper
>to import from Europe for many goods, so its industrial sector /
>capacity would not have developed to anything like its strength.
>
>Only when its industrial sector had grown to a certain "critical mass"
>(as a result of protectionism), did the USA change to a more free world
>trade approach.
>
>If you are strong, then the free world trade approach will help you, if
>you are not so strong, the free world trade will destroy you.
>
>Having your industrial base completely destroyed by free world trade
>competition will destroy any prospects for your country.

it may be that some protectionism for weak entities may
offer some short term advantage....

why not try arguing your point in the context of a modern advanced
economy...with real examples....

>> > It is a bit like banning slavery, until slaves were illegal,
>> > companies refusing the use slaves were at a disadvantage, so many
>> > companies were forced to use slaves.
>>
>> i've no idea what the full economics of slavery would be in a modern
>> society....it didn't help national socialist german, russia or n.
>> korea
>> its modern surrogate, cheap immigrant labour, seems to undermine the
>> short term situation of the very poorest and have little effect on
>> the rest...
>>
>> assessing the full economic/social effects of a policy is not simple
>
>The point is that we (as a nation) should set the rules, to enable the
>market to help our nation/society. We should not allow ourselves to be
>slaves to market forces.

i have no objection to limiting trading options when a tragedy of the
commons issue is at stake....

just what 'slavery to market forces' do you imagine would assist the
uk

--
web site at www.abelard.org - news comment service, logic, economics
energy, education, politics, etc 1,552,396 document calls in year past
--------------------------------------------------------------------------------
all that is necessary for [] walk quietly and carry
the triumph of evil is that [] a big stick.
good people do nothing [] trust actions not words
only when it's funny -- roger rabbit
--------------------------------------------------------------------------------