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

Preview:

DESCRIPTION

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

Citation preview

Ruby 2.0bootstrap for discussion

Philippe Antras @_philant_ Riviera.rb

Matz

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

refinements

experimental localized monkey patching

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

refinement example

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

using MyModulep "$13".price?

keyword arguments

(named parameters)

mandatory argumentsdefault valuesparameter validationlist (with splat operator)readability

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)

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

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

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

rails

takeaway

2.0.0-rc2 delayed yesterday

refinements, safer, cleaner, but experimental

keyword arguments

lazy enumerators

compatible with Rails 3.2

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

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

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

wanna give it a try ?

thank you

Recommended