Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ruby 2.0 Walkthrough: The Best Bits

Ruby 2.0 Walkthrough: The Best Bits

A quick overview of a few of the best bits in Ruby 2.0.

Peter Cooper

February 25, 2013
Tweet

More Decks by Peter Cooper

Other Decks in Programming

Transcript

  1. 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)
  2. 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)
  3. 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
  4. def some_method(options = {}) defaults = { x: 10, y:

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

    z end some_method x: 1, y: 2, z: 3
  6. 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
  7. 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
  8. 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
  9. RubyGems 2.0 Lots of refactoring, initial support for stdlib gemification,

    gem search is now remote, arbitrary metadata support & more.
  10. RDoc 4.0 Adds Markdown support & ri can now show

    pages (e.g. READMEs with ri rdoc:README)
  11. CSV.load and CSV.dump gone Not considered such a good idea

    in light of the early 2013 YAML vulnerabilities
  12. String#chars, #lines, #codepoints, etc. Now return arrays rather than enumerators.

    Use #each_char, #each_line, etc. instead if you need enumerators.
  13. 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 ..
  14. 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 ..
  15. [1, 4, 9, 13, 14, 22, 40].bsearch { |i| p

    i; i >= 6 } # 13 # 4 # 9 # => 9 (1..1000).bsearch { |i| i > 372 }
  16. __dir__ Like __FILE__ but for the current source file’s directory

    Equivalent to File.dirname(File.realpath(__FILE__))
  17. to_h and Hash() A new convention. The hash equivalent to

    #to_a A key use is with Struct and OpenStruct
  18. User = Struct.new(:name, :age, :status) me = User.new("Peter", 31, :admin)

    me.to_h ENV.to_h Hash(nil) # => {} Hash([]) # => {}