15
Ruby 2.0 bootstrap for discussion Philippe Antras @_philant_ Riviera.rb

Ruby 2.0 at the Ruby drink-up of Sophia, February 2013

Embed Size (px)

DESCRIPTION

Presented at the Ruby Drink-up of Sophia Antipolis on the 5th of February 2013 by Philippe Antras (@_philant_).

Citation preview

Page 1: Ruby 2.0 at the Ruby drink-up of Sophia, February 2013

Ruby 2.0bootstrap for discussion

Philippe Antras @_philant_ Riviera.rb

Page 2: Ruby 2.0 at the Ruby drink-up of Sophia, February 2013

Matz

The version number goes up to 2.0 but the changes are rather small. Smaller than the ones we made in 1.9.

Page 3: Ruby 2.0 at the Ruby drink-up of Sophia, February 2013

refinements

experimental localized monkey patching

refinements are file scopeonly top-level "using" is availableno module scope refinementno refinement inheritance

Page 4: Ruby 2.0 at the Ruby drink-up of Sophia, February 2013

refinement example

module MyModule refine String do def price? self.include? "$" end endend

using MyModulep "$13".price?

Page 5: Ruby 2.0 at the Ruby drink-up of Sophia, February 2013

keyword arguments

(named parameters)

mandatory argumentsdefault valuesparameter validationlist (with splat operator)readability

Page 6: Ruby 2.0 at the Ruby drink-up of Sophia, February 2013

def ruby_drinkup(topics, # mandatory where: "the Green King", date: "02/05/13", time: "18:30", announced: false, booked: false) ...end

ruby_drinkup(["rubinius", "ruby 2.0"])ruby_drinkup(["???"], date: "03/05/13")ruby_drinkup(["rubinius", "ruby 2.0"], reserved: true, announced: false)

Page 7: Ruby 2.0 at the Ruby drink-up of Sophia, February 2013

lazy enumeratorsbuild an internal block and iterates only onceenumerate infinite or very large datasetsiterate on a file without loading it all

mapflat_mapselectrejectgrepzip taketake_whiledropdrop_whilecycle

Page 8: Ruby 2.0 at the Ruby drink-up of Sophia, February 2013

next drinkup dates ?

def first_tuesday?(date) date.tuesday? && date.day <= 7 end

drinkups = Date.today..Float::INFINITY .lazy .select { |day| first_tuesday?(day) } .take_while { |day| day.year == 2013 } .forceputs drinkups

x

Page 9: Ruby 2.0 at the Ruby drink-up of Sophia, February 2013

lines = File.foreach(BIG_FILE) .lazy .select { |line| line.match /ERR/ } .take(10) .to_a

Page 10: Ruby 2.0 at the Ruby drink-up of Sophia, February 2013

rails

Page 11: Ruby 2.0 at the Ruby drink-up of Sophia, February 2013

takeaway

2.0.0-rc2 delayed yesterday

refinements, safer, cleaner, but experimental

keyword arguments

lazy enumerators

compatible with Rails 3.2

Page 12: Ruby 2.0 at the Ruby drink-up of Sophia, February 2013

so, what's new ?

module#prependdeclarations of the module overwrite those in the class

Array#bsearch { |x| block }elements have to be ordered or nil is returned

Enumerator#size, Range#size#to_h

convert Struct and OpenStruct to Hash%i and %I

%i{one two three} => [:one, :two, :three]

Dtrace support

Page 13: Ruby 2.0 at the Ruby drink-up of Sophia, February 2013

what changed ?

onigmo (new regex engine)positive and negative lookbehind, named captures, backreferences improved

garbage collectorbitmap marking, smaller memory footprint

psych (use libyaml)default encoding now is UTF-8respond_to? returns false for protected methodsiconv has been removed

use String#encode instead

Page 14: Ruby 2.0 at the Ruby drink-up of Sophia, February 2013

referencesfor more details:

https://bugs.ruby-lang.org/projects/ruby-trunk/roadmap#2.0.0https://github.com/ruby/ruby/blob/v2_0_0_rc1/NEWShttps://bugs.ruby-lang.org/projects/ruby-trunk/wiki/RefinementsSpec

http://blog.headius.com/2012/11/refining-ruby.htmlhttp://ruby-doc.org/core-2.0/Enumerator/Lazy.htmlhttp://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/51301http://patshaughnessy.net/2012/3/23/why-you-should-be-excited-about-garbage-collection-in-ruby-2-0http://www.ruby-forum.com/topic/4409612http://www.ruby-forum.com/topic/4408480http://globaldev.co.uk/2012/11/ruby-2-0-0-preview-features/https://speakerdeck.com/a_matsuda/ruby-2-dot-0-on-rails

Page 15: Ruby 2.0 at the Ruby drink-up of Sophia, February 2013

wanna give it a try ?

thank you