The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Changes in Ruby 1.9, Oct. 07 update

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Eigen Class

Posts: 358
Nickname: eigenclass
Registered: Oct, 2005

Eigenclass is a hardcore Ruby blog.
Changes in Ruby 1.9, Oct. 07 update Posted: Oct 12, 2007 4:21 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Eigen Class.
Original Post: Changes in Ruby 1.9, Oct. 07 update
Feed Title: Eigenclass
Feed URL: http://feeds.feedburner.com/eigenclass
Feed Description: Ruby stuff --- trying to stay away from triviality.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Eigen Class
Latest Posts From Eigenclass

Advertisement

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):

a = [1, 2, 3, 4, 5]
 
a.take(3)             # => [1, 2, 3]
a.take {|i| i < 3 }   # => [1, 2]

drop

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):

a = [1, 2, 3, 4, 5]
 
a.drop(3)             # => [4, 5]
a.drop {|i| i < 3 }   # => [3, 4, 5]

inject (#reduce) without a block

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:

[RUBY_VERSION, RUBY_RELEASE_DATE]                 # => ["1.9.0", "2007-08-03"]
(1..10).reduce(:+)                                # => 55

zip

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

ruby-list:42671


Read more...

Read: Changes in Ruby 1.9, Oct. 07 update

Topic: Ruby/Rails October Meetup Live Blog by Miss604 aka Rebecca Bollwitt Previous Topic   Next Topic Topic: Is Proprietary Software Broken?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use