How to refactor each function with map in Ruby?

tmartin314

I have a loop building a hash for use in a select field. The intention is to end up with a hash:

{ object.id => "object name", object.id => "object name" }

Using:

@hash = {}
loop_over.each do |ac|
  @hash[ac.name] = ac.id
end

I think that the map method is meant for this type of situation but just need some help understanding it and how it works. Is map the right method to refactor this each loop?

mu is too short

Data transformations like this are better suited to each_with_object:

@hash = loop_over.each_with_object({}) { |ac, h| h[ac.name] = ac.id }

If your brain is telling you to use map but you don't want an array as the result, then you usually want to use each_with_object. If you want to feed the block's return value back into itself, then you want inject but in cases like this, inject requires a funny looking and artificial ;h in the block:

@hash = loop_over.inject({}) { |h, ac| h[ac.name] = ac.id; h }
# -------------------- yuck -----------------------------^^^

The presence of the artificial return value is the signal that you want to use each_with_object instead.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to refactor each function with map in Ruby?

From Dev

Refactor to smaller function, how?

From Dev

Ruby2.3; refactor each_with_object

From Dev

How to understand Ruby's .each and .map

From Dev

Ruby - map vs each?

From Dev

How to refactor these if else statements to a function

From Dev

How do you refactor nested callbacks and pass parameters to each function in node?

From Dev

How to refactor long if/return statements in Ruby?

From Dev

How to refactor in Sublime Text? (Ruby, Rails, JavaScript)

From Dev

How to refactor method with multiple boolean variables in Ruby

From Dev

How to refactor in Sublime Text? (Ruby, Rails, JavaScript)

From Dev

How to refactor long if/return statements in Ruby?

From Dev

How to refactor already simple ruby code

From Dev

How to refactor these classes to interact each other?

From Dev

ruby puts - how to avoid getting nil for each map iteration

From Dev

Ruby map function in PHP?

From Dev

map with function for each element?

From Dev

How to refactor a function that throws exceptions with Scalaz or Cats

From Dev

How to refactor a function replacing an argument pattern match

From Dev

How to refactor JS function according to module pattern?

From Dev

What's the best way to refactor redundant each/reverse_each code in Ruby?

From Dev

How to refactor this Ruby sanitize hash method to make it more idiomatic?

From Dev

How to refactor nested loops in a Ruby method that exports to CSV?

From Dev

How can I refactor this Ruby method to run faster?

From Dev

Idiomatic ruby refactor of if statement

From Dev

How to refactor <List> contents using the map() method in React JSX?

From Dev

Ruby map function for "pig latin"

From Java

How to map with index in Ruby?

From Dev

Understanding each_pair and map in ruby

Related Related

  1. 1

    How to refactor each function with map in Ruby?

  2. 2

    Refactor to smaller function, how?

  3. 3

    Ruby2.3; refactor each_with_object

  4. 4

    How to understand Ruby's .each and .map

  5. 5

    Ruby - map vs each?

  6. 6

    How to refactor these if else statements to a function

  7. 7

    How do you refactor nested callbacks and pass parameters to each function in node?

  8. 8

    How to refactor long if/return statements in Ruby?

  9. 9

    How to refactor in Sublime Text? (Ruby, Rails, JavaScript)

  10. 10

    How to refactor method with multiple boolean variables in Ruby

  11. 11

    How to refactor in Sublime Text? (Ruby, Rails, JavaScript)

  12. 12

    How to refactor long if/return statements in Ruby?

  13. 13

    How to refactor already simple ruby code

  14. 14

    How to refactor these classes to interact each other?

  15. 15

    ruby puts - how to avoid getting nil for each map iteration

  16. 16

    Ruby map function in PHP?

  17. 17

    map with function for each element?

  18. 18

    How to refactor a function that throws exceptions with Scalaz or Cats

  19. 19

    How to refactor a function replacing an argument pattern match

  20. 20

    How to refactor JS function according to module pattern?

  21. 21

    What's the best way to refactor redundant each/reverse_each code in Ruby?

  22. 22

    How to refactor this Ruby sanitize hash method to make it more idiomatic?

  23. 23

    How to refactor nested loops in a Ruby method that exports to CSV?

  24. 24

    How can I refactor this Ruby method to run faster?

  25. 25

    Idiomatic ruby refactor of if statement

  26. 26

    How to refactor <List> contents using the map() method in React JSX?

  27. 27

    Ruby map function for "pig latin"

  28. 28

    How to map with index in Ruby?

  29. 29

    Understanding each_pair and map in ruby

HotTag

Archive