I have a hash where the values are nested arrays. I’d like to be able to
iterate over the hash and have 3 vars available in the block: key,
value[0], and value[1]. As you can see, h.each {|a,b,c|…} doesn’t
work. Is there a better way to do it than the last line below? Thanks.
I have a hash where the values are nested arrays. I’d like to be able to
iterate over the hash and have 3 vars available in the block: key,
value[0], and value[1]. As you can see, h.each {|a,b,c|…} doesn’t
work.
I have a hash where the values are nested arrays. I’d like to be able to
iterate over the hash and have 3 vars available in the block: key,
value[0], and value[1]. As you can see, h.each {|a,b,c|…} doesn’t
work. Is there a better way to do it than the last line below? Thanks.
Earle
Use parentheses.
h = [0 => [[1],[2]]}
h.each{|k,(a,b)| p k, a, b}
0
[1]
[2]
=> {0=>[[1], [2]]}
I have a hash where the values are nested arrays. I’d like to be able to
iterate over the hash and have 3 vars available in the block: key,
value[0], and value[1]. As you can see, h.each {|a,b,c|…} doesn’t
work. Is there a better way to do it than the last line below? Thanks.
Earle
Use parentheses.
h = [0 => [[1],[2]]}
h.each{|k,(a,b)| p k, a, b}
0
[1]
[2]
=> {0=>[[1], [2]]}
Regards,
Gordon
Perfect. Thanks, Gordon.
Earle
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.