DEV Community

Discussion on: Advent of Ruby 3.0 - Day 01 - Report Repair

Collapse
 
joshcheek profile image
Josh Cheek
File.readlines(ARGV[0]).then { puts product_duals(_1) }
Enter fullscreen mode Exit fullscreen mode

For this one, I've started using ARGF, which allows it to receive the argument on stdin or as a filename. So this works the same and is a bit more flexible: ARGF.then { puts product_duals(_1) }. At this point, I suppose, it's a bit difficult to justify the then, but there's 25 of these things, I'm sure the will be plenty of opportunities 😜


Also, thanks for talking about then, and implicit arg references. I had heard about both but not played with them enough to internalize them. I've been doing tap + break, and explicit arguments 😐

2.tap { |v| break v + 3 }  # => 5
2.then { _1 + 3 }          # => 5
Enter fullscreen mode Exit fullscreen mode
Collapse
 
baweaver profile image
Brandon Weaver

Y'know I always forget about ARGF because of how rarely I write CLI type scripts. Admittedly I put then in there just to demonstrate that it works.

Collapse
 
peterc profile image
Peter Cooper

You can even go a step further with $<.read or $<.readlines - I've been doing this in my "golfed" solutions.