@m_s__santos/

each_with_object-and-tap-plus-each

Ruby

each_with_object vs. tap + each usage.

fork
loading
Files
  • main.rb
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# each_with_object usage:

('a'..'z').each_with_object({}) do |letter, ascii_table|
  @ascii_number ||= 97
  ascii_table[letter] = @ascii_number
  @ascii_number += 1
end

# tap + each usage:

{}.tap do |ascii_table|
  # Making this example, I realize that
  # I get this area to initialze things,
  # thats is cool!!
  @ascii_number = 97
  ('a'..'z').each do |letter|
    ascii_table[letter] = @ascii_number
    @ascii_number += 1
  end
end
ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-linux]