Global array

Hi,

Im trying to make an array a global array, but I cannot make it work.

Here is what I have:

hostname=server1
server = Hash.new { |hash, key| hash[key] = [] }
service_name=“service name”
service=service1

if server.key?(hostname)
server[hostname]=Hash.new { |hash, key| hash[key] = [] }
server[hostname][“service_name”]=Array.new
server[hostname][“service_name”]=“service#{service_counter}”
server[hostname][“service_name”]=[x,x,x]
end

I have tried with
$server[hostname][“service_name”]=[x,x,x]
$server[hostname][“service_name”]=Array.new

but that does not work… Any help is appreciated.

Kind Regards
Emil Enemkre

On Mon, Jan 16, 2012 at 3:17 PM, Emil E. [email protected]
wrote:

Hi,

Im trying to make an array a global array, but I cannot make it work.

“server” references a Hash.

Here is what I have:

hostname=server1
server = Hash.new { |hash, key| hash[key] = [] }
service_name=“service name”
service=service1

if server.key?(hostname)

Will likely return false because the Hash is empty initially.

server[hostname]=Hash.new { |hash, key| hash[key] = [] }
server[hostname][“service_name”]=Array.new
server[hostname][“service_name”]=“service#{service_counter}”
server[hostname][“service_name”]=[x,x,x]

The three lines above all overwrite the same entry. That’s almost
certainly not what you want.

end

I have tried with
$server[hostname][“service_name”]=[x,x,x]
$server[hostname][“service_name”]=Array.new

but that does not work… Any help is appreciated.

I am not 100% sure what you intend but I think you might just want

server = Hash.new { |hash, key| hash[key] = {} }
server[hostname][“service_name”] = [“service#{service_counter}”,
[x,x,x]]

Kind regards

robert

Hi,

On 16 Jan, 2012, at 16:19 , Robert K. wrote:

server = Hash.new { |hash, key| hash[key] = [] }
service_name=“service name”
service=service1

if server.key?(hostname)

Will likely return false because the Hash is empty initially.
That was the intention, if the hash is empty create it.


server[hostname][“service_name”] = [“service#{service_counter}”, [x,x,x]]

That is a much more elegant solution than mine, thanks.

But this does not solve my problem, I have the hash created within a
loop but are not able to access to array from outside that loop, due the
hash not being global. So how can I make the
server[hostname][“service_name”] = [“service#{service_counter}”,
[x,x,x]]
a global variable?

Kind Regards
Emil

On Wed, Jan 18, 2012 at 10:46 AM, Emil E. [email protected]
wrote:

That was the intention, if the hash is empty create it.
But this won’t work as intended: you create the Hash stored in
“server” with a block which creates Arrays internally. For that
mechanism to work you just access server[some_key] and work with what
you get. If you ask whether the key exists you always get false:

irb(main):001:0> server = Hash.new { |hash, key| hash[key] = [] }
=> {}
irb(main):002:0> key = “foo”
=> “foo”
irb(main):003:0> server.key? key
=> false
irb(main):004:0> server
=> {}
irb(main):005:0> server[key]
=> []
irb(main):006:0> server
=> {“foo”=>[]}
irb(main):007:0> server.key? key
=> true

And if you ask for the key then you need to take action if it is NOT
there but your code assigns only to server[hostname] if server.key?
hostname returned true. You need to at leas negate the condition.

That is a much more elegant solution than mine, thanks.

But this does not solve my problem, I have the hash created within a loop but
are not able to access to array from outside that loop, due the hash not being
global. So how can I make the
server[hostname][“service_name”] = [“service#{service_counter}”, [x,x,x]]
a global variable?

Prepend $.

Regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs