This is a summary of the changes in Ruby 1.9 between Feb. and Oct. 07.
As usual, refer to the full list for further details.
try_convert
The Array, Hash, String, IO and Regexp classes have a "try_convert" class
method that either returns the converted value, as returned by to_ary,
to_hash, to_string, to_io and to_regexp, or nil if the object cannot be
converted for any reason.
Enumerable
each_with_index
Now forwards arguments to #each:
[RUBY_VERSION, RUBY_RELEASE_DATE] # => ["1.9.0", "2007-08-03"]
class X
include Enumerable
def each(*args); yield args.inspect end
end
z = nil
X.new.each_with_index(42){|x| z = x}
z # => ["[42]", 1]
cycle
Calls the given block for each element of the enumerable in a never-ending
cycle. It saves the elements in an internal array.
take
Returns either the first n elements from the enumeration or all elements
until the given block returns true (ruby-dev:30407):
Without a block, returns an array with all but the first n elements from the
enumeration. Otherwise drops elements while the block returns true (and
returns all the elements after it returns a false value) (ruby-dev:30407):
If no block is given, the first argument to #inject is the name of a two-argument
method that will be called; the optional second argument is the initial value:
Doesn't convert the arguments to arrays; enumerators are used instead.
minmax and minmax_by
Returns both the minimun and the maximum at once as a two-element array.
rewind
Rewinds the enumeration sequence.
Array
combination
ary.combination(n){|c| ...}
yields all the combinations of length n of the elements in the array to the
given block. If no block is passed, it returns an enumerator instead.
The order of the combinations is unspecified.
a = [1, 2, 3, 4]
a.combination(1).to_a #=> [[1],[2],[3],[4]]
a.combination(2).to_a #=> [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
a.combination(3).to_a #=> [[1,2,3],[1,2,4],[1,3,4],[2,3,4]]
a.combination(4).to_a #=> [[1,2,3,4]]
a.combination(0).to_a #=> [[]]: one combination of length 0
a.combination(5).to_a #=> [] : no combinations of length 5