Slide 1

Slide 1 text

Ruby 2.0 on Rails in Production Akira Matsuda

Slide 2

Slide 2 text

% ruby -v Who here are already using Ruby 2.0 in production?

Slide 3

Slide 3 text

% ruby -v Who here are still using 1.8?

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

begin

Slide 6

Slide 6 text

Ruby 2.0 goodness Performance Stability Compatibility New features

Slide 7

Slide 7 text

Performance

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

※ taken from @mirakui's slide at RubyKaigi 2013 https://speakerdeck.com/mirakui/high-performance-rails?slide=7

Slide 10

Slide 10 text

※ taken from @mrkn's slide at OedoRubyKaigi 03 https://speakerdeck.com/mrkn/what-a-hard-work-to-make-the-recipe- sharing-service-available-on-ruby-1-dot-9-3?slide=128

Slide 11

Slide 11 text

rails command boot time # ruby 1.9.3p429 % time rails r '1' rails r '1' 7.00s user 1.67s system 99% cpu 8.701 total % time rails r '1' rails r '1' 7.03s user 1.64s system 99% cpu 8.698 total % time rails r '1' rails r '1' 7.01s user 1.66s system 99% cpu 8.710 total # ruby 2.0.0p195 % time rails r '1' rails r '1' 4.80s user 1.29s system 99% cpu 6.124 total % time rails r '1' rails r '1' 4.77s user 1.28s system 99% cpu 6.073 total % time rails r '1' rails r '1' 4.78s user 1.29s system 99% cpu 6.099 total

Slide 12

Slide 12 text

Stability

Slide 13

Slide 13 text

Stability Fact: 2.0.0 was a "stable" release

Slide 14

Slide 14 text

1.9.0 was not 1.9.0: unstable 1.9.1: unstable 1.9.2: stable 1.9.3: stable 2.0.0: stable 2.1.0: stable (?)

Slide 15

Slide 15 text

Stability 2.0.0-p195 was released last month by @nagachika

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

Stability 2.0.0 is no more p0 and so it's ready to use in production

Slide 19

Slide 19 text

Branch Maintenance Policies 1.8.7: maintained by @shyouhei, will be dead soon 1.8.8: no plan 1.9.0: dead 1.9.1: dead 1.9.2: maintained by @yugui, dead (?) 1.9.3: maintained by @unak 1.9.4: no plan 2.0.0: maintained by @nagachika 2.0.1: no plan 2.1.0: managed by @naruse

Slide 20

Slide 20 text

1.9.3 Released by @yugui Maintained by @unak (aka. usa) Sponsored by Ruby Association

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

2.0.0 Released by @mametter (aka. mame) Maintained by @nagachika

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

2.1.0 Would be released by @nalsh (aka. naruse) Would be released in December this year

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

"TRICK 2013" in RubyKaigi Results: http:// www.slideshare.net/mametter/ trick2013-results Winners: https://github.com/tric/ trick2013

Slide 29

Slide 29 text

@unak (usa) $ruby.is_a?(Object){|oriented| language}

Slide 30

Slide 30 text

@mametter (mame) eval$C=%q(at_exit{ open("/dev/dsp","wb"){|g|h=[0]*80 $><<"\s"*18+"eval$C=%q(#$C);S=%:" (S<

Slide 31

Slide 31 text

Compatibility

Slide 32

Slide 32 text

Compatibility "100% compatible" (Ruby level) magic comment

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

99.9% compatible Default Ruby source le encoding to utf-8 [Feature #6679]

Slide 35

Slide 35 text

Hello! puts '͜Μʹͪ͸ʂ'

Slide 36

Slide 36 text

Hello...? % ruby -v hello_utf8.rb ruby 1.8.7 (2012-06-29 patchlevel 370) [i686-darwin12.2.1] ͜Μʹͪ͸ʂ % ruby -v hello_utf8.rb ruby 1.9.3p429 (2013-05-15 revision 40747) [x86_64-darwin12.3.0] encoding_utf8.rb:1: invalid multibyte char (US-ASCII)

Slide 37

Slide 37 text

Magic comment # encoding: utf-8 puts '͜Μʹͪ͸ʂ'

Slide 38

Slide 38 text

͜Μʹͪ͸ɺ1.9.3! % ruby -v hello_utf8.rb ruby 1.9.3p429 (2013-05-15 revision 40747) [x86_64-darwin12.3.0] ͜Μʹͪ͸ʂ

Slide 39

Slide 39 text

Hello! puts '͜Μʹͪ͸ʂ'

Slide 40

Slide 40 text

Hello...? % ruby -v hello_utf8.rb ruby 1.8.7 (2012-06-29 patchlevel 370) [i686-darwin12.2.1] ͜Μʹͪ͸ʂ % ruby -v hello_utf8.rb ruby 1.9.3p429 (2013-05-15 revision 40747) [x86_64-darwin12.3.0] encoding_utf8.rb:1: invalid multibyte char (US-ASCII) % ruby -v hello_utf8.rb ruby 2.0.0p195 (2013-05-14 revision 40734) [x86_64-darwin12.3.0] ͜Μʹͪ͸ʂ

Slide 41

Slide 41 text

Incompatibility s = "\xE3\x81\x82" p s, s.size

Slide 42

Slide 42 text

Incompatibility % ruby -v encoding_binary.rb ruby 1.9.3p429 (2013-05-15 revision 40747) [x86_64-darwin12.3.0] "\xE3\x81\x82" 3 % ruby -v encoding_binary.rb ruby 2.0.0p195 (2013-05-14 revision 40734) [x86_64-darwin12.3.0] "͋" 1

Slide 43

Slide 43 text

Tiny Print Debugging Tip s = "\xE3\x81\x82" # p s, s.size p str: s, size: s.size

Slide 44

Slide 44 text

Tiny Print Debugging Tip % ruby encoding_binary.rb {:str=>"͋", :size=>1} (by @yhara: http://route477.net/d/?date=20130206#p01)

Slide 45

Slide 45 text

Compatibility (GC bug) Ruby 2.0 is so compatible with 1.9.3 that even known bugs are compatible!

Slide 46

Slide 46 text

"GC.disable_lazy_sweep" https://gist.github.com/mrkn/ 5096076

Slide 47

Slide 47 text

New features

Slide 48

Slide 48 text

New Features (Re nements) Module#prepend Keyword arguments Enumerable#lazy

Slide 49

Slide 49 text

Re nements

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

Module#prepend

Slide 52

Slide 52 text

Module#prepend Good for Rails plugins You can replace alias_method_chain with this

Slide 53

Slide 53 text

AMC class A def foo puts 'foo' end end class A def foo_with_bar foo_without_bar puts ‘bar’ end alias_method_chain :foo, :bar end A.new.foo #=> foo bar

Slide 54

Slide 54 text

Module#prepend class A def foo; puts 'foo'; end end module Bar def foo super puts 'bar' end end class A prepend Bar end A.new.foo #=> foo bar

Slide 55

Slide 55 text

Keyword arguments

Slide 56

Slide 56 text

Method#parameters since 1.9.1

Slide 57

Slide 57 text

Method#parameters class C def m(a, b = 1, c = nil) end end C.new.method(:m).parameters #=> [[:req, :a], [:opt, :b], [:opt, :c]]

Slide 58

Slide 58 text

asakusarb/action_args

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

Initial implementation module AbstractController class Base def send_action(method_name, *args) return send method_name, *args unless args.blank? values = method(method_name).parameters.map(&:last).map {|k| params[k]} send method_name, *values end end end

Slide 61

Slide 61 text

Test Patterns params = {a: '1', b: '2'} def m end #=> [] def m(a) end #=> ['1'] def m(a = 'a') end #=> ['1'] def m(c = 'c') end #=> expected: ['c'], actual: [nil]

Slide 62

Slide 62 text

Don't add to the arguments if (type == :opt) && !params.has_key? params = {a: '1', b: '2'} def m(c = 'c') end #=> ['c'] def m(a, b) end #=> ['1', '2'] def m(a, b = 'b') end #=> ['1', '2'] def m(a, c = 'c') end #=> ['1', 'c'] def m(a = 'a', b = 'b') end #=> ['1', '2'] def m(c = 'c', d = 'd') end #=> ['c', 'd'] def m(c = 'c', a) end #=> expected: ['c', '1'], actual: ['1']

Slide 63

Slide 63 text

Default value can't be determined in parse / compile phase def foo(x = rand(3))

Slide 64

Slide 64 text

RubyVM::InstructionSequence # warning: Do never use this black magic!! def foo(x = 'xx', y = :yy, z = 1); end ary = RubyVM::InstructionSequence.of(method(:foo)).to_a default_args = ary[11][1] bytecode = ary[13] p default_args #=> [:label_0, :label_4, :label_8, :label_11] p bytecode #=> [:label_0, 2, [:putstring, "xx"], [:setlocal_OP__WC__0, 4], :label_4, [:putobject, :yy], [:setlocal_OP__WC__0, 3], :label_8, [:putobject_OP_INT2FIX_O_1_C_], [:setlocal_OP__WC__0, 2], :label_11, [:trace, 8], [:putnil], [:trace, 16], [:leave]]

Slide 65

Slide 65 text

method.parameters.reverse_each do |type, key| do the same thing params = {a: '1', b: '2'} def m(c = 'c', a) end => ['c', '1']

Slide 66

Slide 66 text

We needed to do this because, Method parameters are just an Array So the order matters It's a pain I wish if the method parameter is more like a Hash, labeled by the de ned method argument names

Slide 67

Slide 67 text

OK, that's called "Keyword arguments"

Slide 68

Slide 68 text

Keyword arguments # traditional method def m(a, b = 'b', c = 'c') end m('1', '2', '3') # kwargs def m(a, b: 'b', c: 'c') end m('1', b: '2', c: '3')

Slide 69

Slide 69 text

Keyword arguments Optional parameters can be written as a Hash. Order doesn't matter action_args now supports kwargs, so you can use it in your controller code

Slide 70

Slide 70 text

kwargs and Rails Rewriting Rails code using kwargs More comprehensible API def foo(options_with_some_unknown_keys_and_defaults = {}) def bar(*values_maybe_or_maybe_not_with_options) ⊗ def foo(x: 1, y: 2, z: 2) def bar(*values, **options)

Slide 71

Slide 71 text

kwargs and Rails (AS) # AS/lib/active_support/core_ext/class/attribute_accessors.rb - def cattr_reader(*syms) - options = syms.extract_options! + def cattr_reader(*syms, instance_reader: true, instance_accessor: true) ... - unless options[:instance_reader] == false || options[:instance_accessor] == false + unless (instance_reader == false) || (instance_accessor == false)

Slide 72

Slide 72 text

kwargs and Rails (AMo) # AMo/lib/active_model/validations/validates.rb - def validates(*attributes) - defaults = attributes.extract_options!.dup + def validates(*attributes, **defaults)

Slide 73

Slide 73 text

kwargs and Rails (AP) # AP/lib/action_view/helpers/text_helper.rb - def cycle(first_value, *values) - options = values.extract_options! - name = options.fetch(:name, 'default') - + def cycle(first_value, *values, name: 'default')

Slide 74

Slide 74 text

A pitfall I found

Slide 75

Slide 75 text

kwargs and Rails (:if, :unless) User.validates_presence_of :name, :if => -> { true } # AMo/lib/active_model/validations/presence.rb - def validates_presence_of(*attr_names) + def validates_presence_of(*attr_names, if: true, unless: false, on: nil, strict: nil)

Slide 76

Slide 76 text

:if!? def m(if: true) end

Slide 77

Slide 77 text

:if!? def m(if: true) if if ... end end #=> "syntax error, unexpected keyword_end"

Slide 78

Slide 78 text

:if!? def m(if: true) p if end #=> "syntax error, unexpected keyword_end"

Slide 79

Slide 79 text

kwargs and Rails (:end)

Slide 80

Slide 80 text

kwargs and Rails (:end) # actionpack/lib/action_view/helpers/date_helper.rb - def build_options(selected, options = {}) - options = { - leading_zeros: true, ampm: false, use_two_digit_numbers: false - }.merge!(options) - - start = options.delete(:start) || 0 - stop = options.delete(:end) || 59 - step = options.delete(:step) || 1 - leading_zeros = options.delete(:leading_zeros) + def build_options(selected, start: 0, end: 59, step: 1, leading_zeros: true, ampm: false, use_two_digit_numbers: false)

Slide 81

Slide 81 text

:end!? def m(end: 'omg') p end end #=> "syntax error, unexpected keyword_end, expecting end-of-input"

Slide 82

Slide 82 text

Enumerable#lazy

Slide 83

Slide 83 text

Enumerable#lazy "Enumerable#lazy".topics.lazy.each {|it| I.talk it}

Slide 84

Slide 84 text

New Features (internal) TracePoint DTrace support caller_locations debug_inspector API Memory inspection features

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

debug_inspector API

Slide 87

Slide 87 text

The "debugger" gem Works on Ruby 1.9 Partly supports 2.0 Own header les

Slide 88

Slide 88 text

We created a new gem Named it "debugger2" (ko1 named it. Not me!) Based on new debugger API

Slide 89

Slide 89 text

Debugger2 gem "1.0.0.beta2" Still buggy on Rails We need to do more debugging on the debugger...

Slide 90

Slide 90 text

We want debugger!!!

Slide 91

Slide 91 text

end