A Better Way to Format Long Strings

Sometimes you just need long strings.

In tests, I sometimes like to include tiny CSVs as a part of the test code instead of a fixture file. However, that looks real gross real fast with all the linebreaks.

Heredocs to the rescue!

In Ruby, instead of:

sample_csv = "foo,bar,baz\na,b,c\nd,e,f\n,g,h,i"

You can do something like:

sample_csv = <<~EOS
   foo,bar,baz
   a,b,c
   d,e,f
   g,h,i
EOS

Isn't that much better?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: