diff --git a/lib/csv.rb b/lib/csv.rb index 91aeb19a3c..87c3a4be31 100644 --- a/lib/csv.rb +++ b/lib/csv.rb @@ -2650,8 +2650,13 @@ end # c.read.any? { |a| a.include?("zombies") } # } #=> false # -def CSV(*args, &block) - CSV.instance(*args, &block) +# CSV options may also be given. +# +# io = StringIO.new +# CSV(io, col_sep: ";") { |csv| csv << ["a", "b", "c"] } +# +def CSV(*args, **options, &block) + CSV.instance(*args, **options, &block) end require_relative "csv/version" diff --git a/test/csv/interface/test_read_write.rb b/test/csv/interface/test_read_write.rb index 20c9fe317e..c371e9c5fc 100644 --- a/test/csv/interface/test_read_write.rb +++ b/test/csv/interface/test_read_write.rb @@ -112,4 +112,13 @@ a;b;c assert_equal(CSV.instance, CSV {|csv| csv}) end + + def test_instance_shortcut_with_io + io = StringIO.new + from_instance = CSV.instance(io, col_sep: ";") { |csv| csv << ["a", "b", "c"] } + from_shortcut = CSV(io, col_sep: ";") { |csv| csv << ["e", "f", "g"] } + + assert_equal(from_instance, from_shortcut) + assert_equal(from_instance.string, "a;b;c\ne;f;g\n") + end end