Hi,
If do use the option universal_newline with simple string object it works :
"foo \r \r\n".encode(universal_newline: true)
# => "foo \n \nNow, File::new documentation says it supports all options that String#encode does. That's why I tired the below code :-File.open("#{__dir__}/out.txt", universal_newline: true) do |file|
file.each_line do |line|
p line
end
end
# gives output
# "I am a good boy \\r \\r\\n\n"
# "I am a good girl \\r\\n \\r\n"
As you can see, it didn't convert all the \r \r\n to \n only. It seems, I am using the option incorrect way. Can anyone just give a pointer how to use the option as I said above while dealing with File _encoding_ ? I applied the code to the text file :
I am a good boy \r \r\n
I am a good girl \r\n \r
Regards,
Arup Rakshit
Also, `opt` can have same keys in `String#encode` for controlling conversion between the external encoding and the internal encoding.
···
On Tuesday, January 27, 2015 04:24:21 PM you wrote:
On Tue, Jan 27, 2015 at 9:30 AM, Arup Rakshit <aruprakshit@rocketmail.com> > wrote:
> "foo \r \r\n".encode(universal_newline: true)
> # => "foo \n \n
>
> Now, File::new documentation says it supports all options that String#encode does. That's why I tired the below code :-
>
>
Where exactly do you see that? When looking at
It does not mention universal_newline. What am I missing?
Kind regards
robert
--
Regards,
Arup Rakshit
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
Ohh! That's how I need to use. Then it seems I wouldn't be able to take the advantage of it. Actually I am trying to replace such characters for a big file:
text = File.open('file/to/path/foo.txt').read
text.gsub!(/\r\n?/, "\n")
What is the most efficient way to do this, without reading the whole file ?
···
On Wednesday, January 28, 2015 10:49:35 AM Robert Klemme wrote:
On Wed, Jan 28, 2015 at 2:56 AM, Arup Rakshit <aruprakshit@rocketmail.com> > wrote:
> Please look the doc :
> Class: IO (Ruby 2.2.0)
> and the end line :-
>
> Also, `opt` can have same keys in `String#encode` for controlling
> conversion between the external encoding and the internal encoding.
>
Thanks for helping my feeble eyes!
The explanation is pretty simple: the option is only effective if there is
an encoding conversion going on:
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
> text = File.open('file/to/path/foo.txt').read
> text.gsub!(/\r\n?/, "\n")
>
> What is the most efficient way to do this, without reading the whole
file ?
Dear Robert Klemme in person has told us some time ago!