File.open("foo.csv", "w") { |fh| fh.puts "Hello world" }
File.open takes 3 arguments.
- A string for the filename: "foo.csv"
- A string for the mode: "w"
- A function taking one argument, named "fh"
This syntax would have been more honest looking like this:
File.open("foo.csv", "w", -> { |fh| fh.puts "Hello world" })
Or using separate variables:
handler = -> { |fh| fh.puts "Hello world" }
filename = "foo.csv"
mode = "w"
File.open(filename, mode, handler)
Ruby does let you do this kind of, if you use an annoying little & in there.
handler = -> { |fh| fh.puts "Hello world" }
filename = "foo.csv"
mode = "w"
File.open(filename, mode, &handler)