Inject a two-dimensional array to set : to String « Array « Ruby






Inject a two-dimensional array to set


require 'set'
games = [["Alice", "Bob"], ["Carol", "Ted"],
         ["Alice", "Mallory"], ["Ted", "Bob"]]
p players = games.inject(Set.new) { |set, game| game.each { |p| set << p }; set }
# => #<Set: {"Alice", "Mallory", "Ted", "Carol", "Bob"}>

p players << "Ted"
# => #<Set: {"Alice", "Mallory", "Ted", "Carol", "Bob"}>

 








Related examples in the same category

1.Convert array to a set: it removes duplicate elements for you