🖥

#Ruby / nested Array elements / map and delete / Broken modified origi

2020/01/23に公開
# GOOD
# dup element in map
arr = [[:A, :B], [:C, :D]]; changed_arr = arr.map { |a| x = a.dup; x.delete_at(-1); x }; p changed_arr; p arr
# [[:A], [:C]]
# [[:A, :B], [:C, :D]]

# BAD
# map and delete
arr = [[:A, :B], [:C, :D]]; changed_arr = arr.map { |a| a.delete_at(-1); a }; p changed_arr; p arr
#  [[:A], [:C]]
# [[:A], [:C]]

# BAD
# dup array and map and delete
arr = [[:A, :B], [:C, :D]]; dupped_arr = arr.dup; changed_arr = dupped_arr.map { |a| a.delete_at(-1); a }; p changed_arr; p arr
# [[:A], [:C]]
# [[:A], [:C]]


Original by Github issue

https://github.com/YumaInaura/YumaInaura/issues/2957

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ

Twitter

https://twitter.com/YumaInaura

公開日時

2020-01-23

Discussion