DEV Community

Discussion on: Understanding Ruby - Enumerable - Transformations

Collapse
 
grumbleafrican profile image
grumbleafrican • Edited

Minor comment around the one example...

hands.flat_map(&:cards).map(&:to_s).join(', ')
# => "S2, S3, S4, S3, S4, S5, S4, S5"
Enter fullscreen mode Exit fullscreen mode

#join (doc) calls #to_s on the object, so that extra map is not useful.

irb(main):001:0> [:a, :b, :c].join(',')
=> "a,b,c"
Enter fullscreen mode Exit fullscreen mode
Collapse
 
baweaver profile image
Brandon Weaver

True, and easy to forget it does that. It also flattens collections:

[[1], 2, 3, [[[4, 5], 6], 7]].join(', ')
# => "1, 2, 3, 4, 5, 6, 7"
Enter fullscreen mode Exit fullscreen mode