Significant incompatibilities

Character encoding incompatibility

The default script encoding change.

Default script encoding (when magic comment is not specified) is changed into UTF8[#6679]

In Ruby 1.9, the default script encoding is US-ASCII. We changed it to be UTF-8 after considering the following pros and cons.

UTF-8 default is convenient because the majority of modern application uses UTF-8

This change doe not impact any 1.9 codes if Magic Comments are in place.

The default script encoding in 1.9 without Magic Comment is either US-ASCII or ASCII-8BIT. In UTF-8, then some string manipulation could become slower.

Most codes which written for Ruby 1.8 and not written in UTF-8 don't have a Magic Comment, but they raise an error due to invalid bytes in UTF-8. So in the most of that case, won't be a silent problem.

You may encounter problems if your code does not use Magic Comment, and also contains a string with escaped binary. In this case, you should either specify Magic Comment, or convert to ASCII-8bit with String#b that is added at [#6767].

# Returns the last line
str.lines.to_a.last
# Returns the second last line
str.chars.to_a[1]

Here are the equivalent code in Ruby 2.0

# Returns the last line
str.lines.last
# Returns the second last line
str.chars[1]

As you can see, you do not have to convert with to_a

However, the following code now raises errors.

str.chars.with_index{ ... }

This is because an array does not have with_index method. To work this around, please replace with each_char.

str.each_char.with_index{ ... }

BTW IO, StringIO, ARGF has methods with same names, but methods such as IO#bytes are deprecated in Ruby 2.0 and raises warning if you use the method.

We decided to deprecate IO#bytes *1*2 because no one will want to convert an entire file into an array (It may look strange that IO#bytes and String#bytes returns different result format) {{fn You can use IO#readlines if you want to create an array of rows"}}.

Number (Integer and Float) is frozen

Author: Koichi Sasada

Integer (Fixnum, Bignum) and Float objects are always frozen. If (quite unlikely though) there are any codes that add instance variable or eigen method into these numerical objects, these programs will break. Let's throw away such codes.

new method: Enumerable#size

Author：yhara

Enumerable now contains a method called "size" Enumerable already has a similar method called "count" but the method has a shortcoming of returning all the array element even when you only want to count the number of elements.

size method counts the number of elements without returning entire elements because the method is overwritten in all class that includes Enumerable.

For example, you can write a programme of counting the combination of 1000 elements in Ruby 2.0 as follows.

p [*1..1000].combination(3).size
#=> 166167000

(If you use "count", it takes too long because it tries to secure 166167000 elements of array into memory space) .

The "size" method returns nil if you won't be able to know the size in advance.
For example the following program returns nil because you won't be able to know the total line size until you read the entire file.