Normalizing Line Endings in Ruby

Normalizing line endings in Ruby

Best is just to handle the two cases that you want to change specifically and not try to get too clever:

s.gsub /\r\n?/, "\n"

How to write DOS line endings to a file from Unix

There's an option for this, the same one you're using on encode also works with File.open:

File.open('/tmp/foo', mode: 'w', crlf_newline: true) do |file|
file.puts("alpha")
file.puts("beta")
end

This option not only ends lines in \r\n, it also translates any explicit \n into \r\n. So this:

file.puts("alpha\nbar")

will write alpha\r\nbar\r\b

Universal newline support in Ruby that includes \r (CR) line endings

You could use :row_sep => :auto:

:row_sep
The String appended to the end of each row. This can be set to the special :auto setting, which requests that CSV automatically discover this from the data. Auto-discovery reads ahead in the data looking for the next "\r\n", "\n", or "\r" sequence.

There are some caveats of course, see the manual linked to above for details.

You could also manually clean up the EOLs with a bit of gsubing before handing the data to CSV for parsing. I'd probably take this route and manually convert all \r\ns and \rs to single \ns before attempting to parse the CSV. OTOH, this won't work that well if there is embedded binary data in your CSV where \rs mean something. On the gripping hand, this is CSV we're dealing with so who knows what sort of crazy broken nonsense you'll end up dealing with.

Is there a built-in method in Ruby to convert LF to CRLF in a string and vice versa?

Not that I'm aware of.

This prior answer seems to suggest use of gsub (regex replacement)

Normalizing line endings in Ruby

How to detect and handle different EOL in Ruby?

I would slurp the file, normalize the input, and then feed it to CSV:

raw = File.open('my.csv','rb',&:read).gsub("\r\n","\n")
CSV.parse(raw) do |row|
# use row here...
end

The above uses File.open instead of IO.read due to slow file reads on Windows Ruby.

Ruby console overwrites line when printing out lines in a file

In the end I found out that the text file was the cause of my problem. I created a new one with the same content and it started working how I intended.

Ruby - Regex Multiple lines

Check this example:

string = <<EOF
#++
## app_name/dir/dir/filename
## $Id$
##--

foo bar
EOF

puts /#\+\+.*\n##.*\n##.*\n##--/.match(string)

The pattern matches two lines starting with ## between two lines starting with #++ and ending with #-- plus including those boundaries into the match. If I got the question right, this should be what you want.

You can generalize the pattern to match everything between the first #++ and the first #-- (including them) using the following pattern:

puts /#\+\+.*?##--/m.match(string)

Ruby method to remove accents from UTF-8 international characters

I generally use I18n to handle this:

1.9.3p392 :001 > require "i18n"
=> true
1.9.3p392 :002 > I18n.transliterate("Hé les mecs!")
=> "He les mecs!"

RSpec and Capybara: how to let have_css with text: ignore line breaks in text

As of version 3 Capybara attempts to match text as it's displayed - that means if the text being displayed has a <br> in it, or is partially wrapped inside a block element, etc. then the text will have a linefeed in it.

How to deal with the situation depends on what you're trying to do. By default the text option matches substrings so if you don't really care about the full text you can do

expect(page).to have_css('.active a', text: 'current menu item')

you can also specify a regexp for the text option

expect(page).to have_css('.active a', text: /Reports (\d+)/)

or as you've shown you can add the \n to verify the text as displayed



Related Topics



Leave a reply



Submit