Robert Klemme
9/20/2008 2:53:00 PM
On 19.09.2008 20:21, Ruby Student wrote:
> [Note: parts of this message were removed to make it a legal post.]
>
> Team,
> Is there a quick (perhaps one liner) way to initialize the following hashes
> into just one hash?
>
> def xwing(ia)
>
> p ia
>
> puts " "
> p @xwing
>
> r1 = {"1" => 0, "2" => 0, "3" => 0, "4" => 0, "5" => 0, "6" => 0, "7" =>
> 0, "8" => 0, "9" => 0}
> r2 = {"1" => 0, "2" => 0, "3" => 0, "4" => 0, "5" => 0, "6" => 0, "7" =>
> 0, "8" => 0, "9" => 0}
> r3 = {"1" => 0, "2" => 0, "3" => 0, "4" => 0, "5" => 0, "6" => 0, "7" =>
> 0, "8" => 0, "9" => 0}
> r4 = {"1" => 0, "2" => 0, "3" => 0, "4" => 0, "5" => 0, "6" => 0, "7" =>
> 0, "8" => 0, "9" => 0}
> r5 = {"1" => 0, "2" => 0, "3" => 0, "4" => 0, "5" => 0, "6" => 0, "7" =>
> 0, "8" => 0, "9" => 0}
> r6 = {"1" => 0, "2" => 0, "3" => 0, "4" => 0, "5" => 0, "6" => 0, "7" =>
> 0, "8" => 0, "9" => 0}
> r7 = {"1" => 0, "2" => 0, "3" => 0, "4" => 0, "5" => 0, "6" => 0, "7" =>
> 0, "8" => 0, "9" => 0}
> r8 = {"1" => 0, "2" => 0, "3" => 0, "4" => 0, "5" => 0, "6" => 0, "7" =>
> 0, "8" => 0, "9" => 0}
> r9 = {"1" => 0, "2" => 0, "3" => 0, "4" => 0, "5" => 0, "6" => 0, "7" =>
> 0, "8" => 0, "9" => 0}
>
> end
>
>
> (0..8).each do |r|
> xwing(@xwing[[r][0]]) # @xwing is defined previously
> end
>
>
> Actually, I wanted just one hash, @frequency, but I did not know how to do
> it.
Err, what? You know how to create nine hashes but you do not know how
to create one? I'm suspecting that there is something missing from your
posting.
Maybe you wanted something like this:
irb(main):004:0> require 'pp'
=> true
irb(main):005:0> pp Array.new(9) { Hash[*(1..9).map
{|i|[i.to_s,0]}.flatten] }
[{"6"=>0, "7"=>0, "8"=>0, "9"=>0, "1"=>0, "2"=>0, "3"=>0, "4"=>0, "5"=>0},
{"6"=>0, "7"=>0, "8"=>0, "9"=>0, "1"=>0, "2"=>0, "3"=>0, "4"=>0, "5"=>0},
{"6"=>0, "7"=>0, "8"=>0, "9"=>0, "1"=>0, "2"=>0, "3"=>0, "4"=>0, "5"=>0},
{"6"=>0, "7"=>0, "8"=>0, "9"=>0, "1"=>0, "2"=>0, "3"=>0, "4"=>0, "5"=>0},
{"6"=>0, "7"=>0, "8"=>0, "9"=>0, "1"=>0, "2"=>0, "3"=>0, "4"=>0, "5"=>0},
{"6"=>0, "7"=>0, "8"=>0, "9"=>0, "1"=>0, "2"=>0, "3"=>0, "4"=>0, "5"=>0},
{"6"=>0, "7"=>0, "8"=>0, "9"=>0, "1"=>0, "2"=>0, "3"=>0, "4"=>0, "5"=>0},
{"6"=>0, "7"=>0, "8"=>0, "9"=>0, "1"=>0, "2"=>0, "3"=>0, "4"=>0, "5"=>0},
{"6"=>0, "7"=>0, "8"=>0, "9"=>0, "1"=>0, "2"=>0, "3"=>0, "4"=>0, "5"=>0}]
=> nil
irb(main):006:0>
However, given the structure of your Hashes I wonder why you do not want
to use Arrays instead. E.g.
irb(main):001:0> pp Array.new(9) { Array.new(9, 0) }
[[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]]
=> nil
Or even use a Matrix.
irb(main):006:0> require 'matrix'
=> true
irb(main):007:0> pp Matrix.zero 9
Matrix[[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0,
0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0
], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0,
0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [
0, 0, 0, 0, 0, 0, 0, 0, 0]]
=> nil
irb(main):008:0> pp Matrix.zero(9).to_a
[[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]]
=> nil
irb(main):009:0>
Kind regards
robert