monvural
monvural

Reputation: 228

Is the returning function broken in Ruby 1.9.3?

I had a block of code that looked like this:

returning({}) do |hash|
attributes.each { |key, value|
        hash[key.underscore] = value
    }

end

Rewriting this to not use the returning magic fixed this method breaking with Ruby 1.9.3. Did this really fix something, or am I just missing something obvious?

Thanks.

Upvotes: 1

Views: 111

Answers (2)

dbenhur
dbenhur

Reputation: 20398

The method returning was never a standard method in Ruby; it was supplied in Rails' ActiveSupport in earlier versions of Rails. It is an implementation of the K-combinator commonly nicknamed Kestrel. The Ruby core library now supplies an equivalent method Object#tap. Just substitute the word tap for returning in your snippet and it will work as you expected.

However, for what you're trying to do, it is simply unnecessary. Use map and Hash::[], for a much simpler expression:

Hash[ attributes.map { |k, v| [k.underscore, v] } ]

Upvotes: 0

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230481

I personally never heard of returning. But your snippet can be rewritten with more standard methods.

attributes.each_with_object({}) do |(key, value), memo|
  memo[key.underscore] = value
end

Upvotes: 1

Related Questions