I was trying to create a hash `{ mountain: { river: { tree: 23 } }
}`using the below :
path = [:mountain, :river, :tree,23]
hash = {}
path.each_cons(2).to_a.reverse.inject(hash,:[]=)
# =>
# ~> -:3:in `[]=': wrong number of arguments (1 for 2) (ArgumentError)
# ~> from -:3:in `each'
# ~> from -:3:in `inject'
# ~> from -:3:in `<main>'
I know why the error comes. But my question is - Is there any way to
send 2 arguments to the method `:[]=` ,by breaking the one argument
array using splat or something like that ?
[16] pry(main)> path.reverse.reduce({}){|s,v| v == path.last ? v : {v => s}
}
# => {:mountain=>{:river=>{:tree=>23}}}
···
On Thu, Oct 10, 2013 at 9:58 PM, Love U Ruby <lists@ruby-forum.com> wrote:
I was trying to create a hash `{ mountain: { river: { tree: 23 } }
}`using the below :
path = [:mountain, :river, :tree,23]
hash = {}
path.each_cons(2).to_a.reverse.inject(hash,:=)
# =>
# ~> -:3:in `=': wrong number of arguments (1 for 2) (ArgumentError)
# ~> from -:3:in `each'
# ~> from -:3:in `inject'
# ~> from -:3:in `<main>'
I know why the error comes. But my question is - Is there any way to
send 2 arguments to the method `:=` ,by breaking the one argument
array using splat or something like that ?
If you do not explicitly specify an initial value for memo,
then the first element of collection is used as the initial
value of memo.
Peace,
tiredpixel
···
On 10/10/2013 21:25, Michael Fellinger wrote:
[16] pry(main)> path.reverse.reduce({}){|s,v| v == path.last ? v :
{v => s} } # => {:mountain=>{:river=>{:tree=>23}}}
On Thu, Oct 10, 2013 at 9:58 PM, Love U Ruby <lists@ruby-forum.com > <mailto:lists@ruby-forum.com>> wrote:
I was trying to create a hash `{ mountain: { river: { tree: 23 } }
}`using the below :
path = [:mountain, :river, :tree,23] hash = {}
path.each_cons(2).to_a.reverse.inject(hash,:=) # => # ~> -:3:in
`=': wrong number of arguments (1 for 2) (ArgumentError) # ~>
from -:3:in `each' # ~> from -:3:in `inject' # ~> from -:3:in
`<main>'
I know why the error comes. But my question is - Is there any way
to send 2 arguments to the method `:=` ,by breaking the one
argument array using splat or something like that ?