Slide 1

Slide 1 text

RUBY 2.0 WALKTHROUGH

Slide 2

Slide 2 text

The Best Bits THE RUBY 2.0 RUNTHROUGH

Slide 3

Slide 3 text

A sprint, not a stroll

Slide 4

Slide 4 text

BIG PARTS

Slide 5

Slide 5 text

Refinements Experimental scope-limited monkey patching

Slide 6

Slide 6 text

module FloatDivision refine Fixnum do def /(other); self.to_f / other; end end end class MathFun using FloatDivision def self.ratio(a, b) a / b end end p MathFun.ratio(6, 8) THE OLD IDEA (no longer works)

Slide 7

Slide 7 text

module FloatDivision refine Fixnum do def /(other); self.to_f / other; end end end class MathFun using FloatDivision def self.ratio(a, b) a / b end end p MathFun.ratio(6, 8)

Slide 8

Slide 8 text

module FloatDivision refine Fixnum do def /(other); self.to_f / other; end end end using FloatDivision class MathFun def self.ratio(a, b) a / b end end p MathFun.ratio(6, 8) Main context is OK

Slide 9

Slide 9 text

Keyword Arguments definitely a: “pretty”, good: “addition”, to: “the language”

Slide 10

Slide 10 text

def some_method(options = {}) defaults = { x: 10, y: 20, z: 30 } options = defaults.merge(options) p options end some_method x: 1, y: 2

Slide 11

Slide 11 text

def some_method(x: 10, y: 20, z: 30) p x, y, z end some_method x: 1, y: 2, z: 3

Slide 12

Slide 12 text

def some_method(x: 10, **rest) p x, rest end some_method x: 1, y: 2, z: 3, c: “x”

Slide 13

Slide 13 text

Enumerator::Lazy Lazy enumeration, finally made easy.

Slide 14

Slide 14 text

infinite_range = (0..Float::INFINITY) infinite_range.select { |num| num % 74 == 0 }

Slide 15

Slide 15 text

infinite_range = (0..Float::INFINITY) e = infinite_range.lazy.select { |num| num % 74 == 0 } puts e.next puts e.next puts e.next

Slide 16

Slide 16 text

Module#prepend

Slide 17

Slide 17 text

module Bar def my_method "inside the module" end end class Foo include Bar def my_method "inside the class" super end end x = Foo.new p x.my_method 1 2

Slide 18

Slide 18 text

module Bar def my_method "inside the module" super end end class Foo prepend Bar def my_method "inside the class" end end x = Foo.new p x.my_method 1 2

Slide 19

Slide 19 text

INCLUDE PREPEND Foo class Bar module x object NEITHER Foo class x object Object class Object class Foo class Bar module x object Object class

Slide 20

Slide 20 text

DETAILS

Slide 21

Slide 21 text

ABI Breakage Changes to the Application Binary Interface

Slide 22

Slide 22 text

Regex engine changed From Oniguruma to Onigmo

Slide 23

Slide 23 text

%{this\r\nis\n\ncool\vhello!}.gsub(/\R/, '')

Slide 24

Slide 24 text

RubyGems 2.0 Lots of refactoring, initial support for stdlib gemification, gem search is now remote, arbitrary metadata support & more.

Slide 25

Slide 25 text

RDoc 4.0 Adds Markdown support & ri can now show pages (e.g. READMEs with ri rdoc:README)

Slide 26

Slide 26 text

%i and %I To form arrays of symbols, like %w does for words and arrays

Slide 27

Slide 27 text

%i{a b c} == [:a, :b, :c] %I{ #{(rand(26) + 65).chr} } == [:Y]

Slide 28

Slide 28 text

UTF-8 is default source encoding No more # encoding: utf-8 (if you don’t want)

Slide 29

Slide 29 text

Bitmap garbage collector Faster, more efficient

Slide 30

Slide 30 text

CSV.load and CSV.dump gone Not considered such a good idea in light of the early 2013 YAML vulnerabilities

Slide 31

Slide 31 text

String#chars, #lines, #codepoints, etc. Now return arrays rather than enumerators. Use #each_char, #each_line, etc. instead if you need enumerators.

Slide 32

Slide 32 text

TracePoint A more object oriented alternative to set_trace_func

Slide 33

Slide 33 text

tracer = lambda do |event, file, line, id, binding, klass| to_display = [event, File.basename(file), line, klass, id] puts "%10s in %s at line %-2d %s:%s" % to_display end set_trace_func tracer # .. normal code here ..

Slide 34

Slide 34 text

tracer = TracePoint.new do |tp| to_display = [tp.event, File.basename(tp.path), tp.lineno, tp.defined_class, tp.method_id] puts "%10s in %s at line %-2d %s:%s" % to_display end tracer.enable # .. normal code here ..

Slide 35

Slide 35 text

respond_to? respond_to? against a protected method now returns false

Slide 36

Slide 36 text

Method transplants with define_method Now accepts UnboundMethods

Slide 37

Slide 37 text

module M def foo; "foo"; end end define_method :foo, M.instance_method(:foo) p foo

Slide 38

Slide 38 text

Array#bsearch Range#bsearch Binary search for monotonic collections

Slide 39

Slide 39 text

[1, 4, 9, 13, 14, 22, 40].bsearch { |i| p i; i >= 6 } # 13 # 4 # 9 # => 9 (1..1000).bsearch { |i| i > 372 }

Slide 40

Slide 40 text

__dir__ Like __FILE__ but for the current source file’s directory Equivalent to File.dirname(File.realpath(__FILE__))

Slide 41

Slide 41 text

to_h and Hash() A new convention. The hash equivalent to #to_a A key use is with Struct and OpenStruct

Slide 42

Slide 42 text

User = Struct.new(:name, :age, :status) me = User.new("Peter", 31, :admin) me.to_h ENV.to_h Hash(nil) # => {} Hash([]) # => {}

Slide 43

Slide 43 text

FIN https://cooperpress.com/