In this post, I will summarise things you should be aware when start using Ruby 2.0.0 including some of new features.
The official summary is in the NEWS file
Author:naruse
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.
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].
iconv, a Ruby extension library, is deleted from the core[#6322]
Please use iconv.gem if you need to use iconv related methods such as String#encode and Encoding::Converter.
If you still use -K option (as used in 1.8) it will raise warning.
Author:yhara (Yutaka Hara)
In Ruby 1.9, String has methods that iterate string , such as each_line, each_char, each_byte, and each_codepoint, as well as their identical methods such as lines,chars, bytes, code points
In, Ruby 2.0.0, lines, chars, bytes, and code points returns an array rather than Enumerator.
Here are some Ruby 1.9 examples.
Here are the equivalent code in Ruby 2.0
As you can see, you do not have to convert with to_a
However, the following code now raises errors.
This is because an array does not have with_index method. To work this around, please replace with each_char.
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) 3.
each_byte is recommended over each in this situation.
The associated ticket:http://bugs.ruby-lang.org/issues/6670
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.
The associated ticket(Japanese):http://bugs.ruby-lang.org/issues/3222, http://bugs.ruby-lang.org/issues/6936
Author: Koichi Sasada
You can now shorten [:a, :b, :c] to %i(a b c)
Like %W expands characters, %I expands characters.
Author:Koichi Sasada
Module#prepend is similar to Module#include Unlike Module#include, the method prepends the specified class(module) into the method lookup order rather than appends.
The following article has more detail (Japanese)
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.
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.
The related method:https://bugs.ruby-lang.org/issues/6636
Author:yhara (Yutaka Hara)
Ruby has methods that converts an object into array or string, such as to_a and to_s. In Ruby 2.0, there is a new method called to_h that converts an object into a hash.
Struct, OpenStruct, and ENV cow can be converted into a hash with to_h. When a hash is passed to to_h, it just returns self. When nil is passed to to_h, it returns an empty hash ({}) .4
Going forward, I would suggest that you name to_h to methods that returns hash representation of an object.
Author: Koichi Sasada
Kernel#dir method is added. This method is same as File.dirname(FILE) You can obtain the directory name of a file with dir
Author: nari
Array#bsearch 5 takes a sorted array 6 and matches a condition using binary search. It’s nice that you don’t have to build your own bsearch for programming competitions :-)
Here is the example. The following examples are all from Ruby’s RDoc
bsearch passes an array into an argument inside block, and the block has to return either true or false.
It will be easier to understand if you read the both example and the descriptions together.
bsearch has two different mode, and I have explained find-minimum so far. I will now explain the second mode, find-any
You can switch the mode by the returning value of the block.
find-any finds a first element that matches a range of condition(i…j). The behavior depends on its returning value.
It will be easier to understand if you read the both example and the descriptions together. By the way, find-any mode is based on libc の bsearch(2) API
Author:Koichi Sasada
Thread#[] allows you to use thread local variables (variables that can be shared among threads), but these variables become invalid when switched by Fiber (Let’s call this as Fiber local variables).
To avoid this problem, we careated a new API for handling thread local variables that are not affected by Fiber local variables.
In most cases, you can just use Thread#[] to control Fiber local variables per fiber, though you may occasionally need this API. Use it with caution.
Author:Koichi Sasada
You can now specify VM and machine stack sizes (used by threads and fibers) in environment variables.
The default stack size gets bigger, though you don’t have to worry about it for normal usages.
The associated article (in Japanese):Ruby VM Advent calendar #21 VM stack size tuning
Author:Koichi Sasada
Ruby 2.0.0 added some debugging functions
The more detail is in Ruby VM advent calendar referred in YARV Maniacs 【第 11 回】 最近の YARV の事情 (sorry, they are written in Japanese).
This may get reverted in 2.0.1, if no one likes it :-p ↩
knu (Akinori Musha) seems thinking about returning a class such as LazyArray that has both Enumerable and index access ↩
You can use IO#readlines if you want to create an array of rows ↩
this feature lets you write hash = opts[:foo].to_h without worrying whether opts[:foo] is nil or not ↩
There is also a similar function called Range#bsearch ↩
binary search assumes that the given array is sorted, and an un-sorted array is not taken into account ↩