O
O
Ord EO2018-12-03 17:17:43
ruby
Ord EO, 2018-12-03 17:17:43

Why doesn't the ruby ​​algorithm work?

There is an array

arr = [
{key: 1, value: some_value441},
{key: 2, value: some_value212},
{key: 3, value: some_value33},
{key: 4, value: some_value44},
]

there is an algorithm that translates it into a hash.
arr.inject({}) do |previous, current|
  previous.merge({current[:id] => current[:title]})
end

It works and everything is fine, but I noticed if you add current to the end of the algorithm, it starts to output only one value, who can explain why this happens, or what should I read to understand it?
arr.inject({}) do |previous, current|
  previous.merge({current[:id] => current[:title]})
  current
end

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
longclaps, 2018-12-03
@OrdeO

To understand this, you need to read a book about ruby.
Ifcho - it says that every expression returns a value, and the body of the code block returns the value of the last expression, and the last expression you have is current.
Yes, you need to read the book.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question