String.new what is default encoding?

Hello.

When preparing for immutable strings by default, I changed my code from

    buffer = ""

to

    buffer = String.new

However, an unintended side effect of this was the encoding:

2.3.1 :001 > "".encoding
=> #<Encoding:UTF-8>
2.3.1 :002 > String.new.encoding
=> #<Encoding:ASCII-8BIT>

I'm replacing `""` with `String.new(encoding: Encoding::UTF_8)` but
wondering what exactly is the best practice here going forward?

Kind regards,
Samuel

I'd go with

buffer = "".dup

Kind regards

robert

···

On Fri, Nov 25, 2016 at 1:49 AM, Samuel Williams <space.ship.traveller@gmail.com> wrote:

When preparing for immutable strings by default, I changed my code from

    buffer = ""

to

    buffer = String.new

However, an unintended side effect of this was the encoding:

2.3.1 :001 > "".encoding
=> #<Encoding:UTF-8>
2.3.1 :002 > String.new.encoding
=> #<Encoding:ASCII-8BIT>

I'm replacing `""` with `String.new(encoding: Encoding::UTF_8)` but
wondering what exactly is the best practice here going forward?

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

Hello.

Isn't it mode convenient to use
str = ''.freeze?

Rubocop gives advises with #freeze

Just if you want to keep the string empty. Otherwise, you will not be able
to modify it

···

On Sat, Nov 26, 2016, 11:45 AM Aleksey Ivanov <ialexxei@gmail.com> wrote:

Hello.

Isn't it mode convenient to use
str = ''.freeze?

Rubocop gives advises with #freeze

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

--
--

Sébastien Puyet