Slide 1

Slide 1 text

Ruby's past, present, and future Nov 11th, 2011 Shugo Maeda and Yutaka Hara

Slide 2

Slide 2 text

Self introduction ● Shugo Maeda (Twitter: @shugomaeda) ● A Ruby committer ● The co-chairman of the Ruby Association ● A director of Network Applied Communication Laboratory Ltd.

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

If you have a question:

Slide 6

Slide 6 text

Self introduction ● Yutaka HARA ● Twitter ID: @yhara_en ● Github ID: yhara – BiwaScheme (Scheme in JavaScript) – ruby-dev.info ([ruby-dev] translation to En) – Esoteric Language designer

Slide 7

Slide 7 text

FAQ: Are you in a Ruby core team? ● I'm not, but ... – – – –             ruby-dev.info

Slide 8

Slide 8 text

Esoteric Language ● Professional Esoteric Language Designer ● Do you know the programming language Whitespace ?

Slide 9

Slide 9 text

programming language ModanShogi ● This is a valid Shogi game ▲ 2六歩 △6二銀 ▲6六歩 △7一銀 ▲5六歩 △6二金 ▲ 6八金 △6四歩 ▲6九金 △6三金 ▲6八玉 △5二玉 ▲ 5九金右 △6二金 ▲5八金直 △6三金 ▲5七金 △6二金 ▲ 5九金 △6五歩 ▲6七金 △6三金 ▲5八金 △5一金 ▲ 5九金 △5四歩 ▲5五歩 △5三玉 ▲5七玉 △4二銀 ▲ 6八玉 △5二金 ▲5八金 △5一金 ▲5七金上 △5二金 ▲ 5八金 △5一金 ▲5七金上 △5二金 ▲5八金 △3一銀 ▲ 5七玉 △4二玉 ▲5四歩 △同 金 ▲5五歩 △5一金 ▲ 5四歩 △4一金 ▲5六玉 △5一金 ▲6五玉 △5七歩 ▲ 3六歩 △3二玉 ▲5九金 △6一金 ▲6九金 △5一金 ▲ 5七金 △6二金 ▲5六玉 △6三金 ▲5八金上 △7四金 ▲ 5五玉 △4四歩 ▲3四金 △同 歩 ▲5九金 △3五金 ▲ 4八金 △3六金 ▲6七金 △2六金 ▲3八金 △3六金 ▲ 4八金 △3三玉 ▲5七金上 △2六金 ▲4六金 △2五金 ▲ 4五金 △同 歩 ▲3五歩 △同 金 ▲5七金 △4三金 ▲ 5六金 △4二玉

Slide 10

Slide 10 text

programming language ModanShogi ● and prints Hello World. $ cat yowa.modan ▲ 2六歩 △6二銀 ▲6六歩 △7一銀 ▲5六歩 △6二金 ▲ 6八金 △6四歩 ▲6九金 △6三金 ▲6八玉 △5二玉 (..snip..) ▲ 4八金 △3三玉 ▲5七金上 △2六金 ▲4六金 △2五 金 ▲ 4五金 △同 歩 ▲3五歩 △同 金 ▲5七金 △4三金 ▲ 5六金 △4二玉 $ ./bin/shogimodan yowa.modan Hello, world!

Slide 11

Slide 11 text

Topics ● Ruby's past, ● present, ● and future

Slide 12

Slide 12 text

Ruby's past

Slide 13

Slide 13 text

The birth of Ruby ● Born on February 24th 1993 – Just the name ● Father: Yukihiro Matsumoto, a.k.a. Matz ● Godfather: Keiju Ishitsuka

Slide 14

Slide 14 text

How was the name Ruby chosen? keiju> Come to think of it, did you come up with the name of the language? matz> Hmm, if it's similar enough to shells, Tish. matz> But I need a more cool name ... keiju> ruby keiju> Of course, it should be a jewel name matz> Is it taken from ruby annotation? matz> Why should it be a jewel name? matz> Affected by Mitsubishi? keiju> perl matz> I see ● Translated log of IRC

Slide 15

Slide 15 text

Ruby 0.49 ● The oldest version available – ftp://ftp.ruby-lang.org/pub/ruby/1.0/ruby-0.49.tar.gz – (see [ruby-talk: 206778]) ● Released privately in July 1994

Slide 16

Slide 16 text

Hello world in Ruby 0.49 print("Hello world\n") ● Is it the same as Ruby 1.9? – Both yes and no – You could not omit parentheses in Ruby 0.49 print "Hello world\n" #=> ERROR!

Slide 17

Slide 17 text

Hello world in Python $ python2.6 -c 'print "hello"' hello $ python3.1 -c 'print "hello"' File "", line 1 print "hello" ^ SyntaxError: invalid syntax

Slide 18

Slide 18 text

print() in Ruby and Python Ruby Python print("a") OK OK OK OK print "a" ERROR OK OK ERROR

Slide 19

Slide 19 text

CoC ● Convention over Configuration ● Convenience over Consistency ● In Python, the print statement was removed for consistency ● In Ruby, parenthesis omission was supported for convenience

Slide 20

Slide 20 text

Example code in Ruby 0.49 class greeting # (1) def !=(array) # != can be overridden do array.each using x # (2) @say_hello(x) end fail("error") # (3) end def # (4) def @say_hello(x) # (5) print("Hello, ", x, "!\n") end def end class

Slide 21

Slide 21 text

Example code in Ruby 0.49 (cont'd) protect # (1) g = greeting.new() g != ("Matz" :: "Larry") # (2) print("success\n") resque # (3) print("failed\n") end protect ● resque is a typo by Matz (not me!)

Slide 22

Slide 22 text

Already had essential features ● ruby-0.49/FAQ + インタプリタであり , 手軽にプログラムできる . + 変数宣言が要らない . + ファイルやストリームなどへの UNIX 的な操作が簡単にできる . + バイナリファイルも扱える . + きちんとしたオブジェクト指向言語である . + 特異メソッドの機能が提供されている . + モジュールによる機能の共有ができる . + 例外処理機能がある . + GC があるのでメモリ管理を気にしなくて済む . + 一貫性のある言語仕様 . + 複雑なデータ構造も扱える . + ダイナミックロードをサポートする ( システムによる ).

Slide 23

Slide 23 text

What was Ruby created for? ● For UNIX ● For easy object-oriented programming ● For fun

Slide 24

Slide 24 text

Ruby for UNIX ● Ruby was created for UNIX – Developed on SONY NEWS-OS ● Daily scripting like Perl – Not self-contained in contrast to Smalltalk ● APIs tied closely to UNIX/C

Slide 25

Slide 25 text

APIs came from UNIX/C ● File.open ● IO#read ● IO#gets ● IO#write ● Kernel#printf ● Kernel#puts ● IO#fcntl ● IO#ioctl ● File.stat ● IO.select ● Process::Sys.getuid ● Process::Sys.setuid ● Kernel#fork

Slide 26

Slide 26 text

Better UNIX ● Some API's origins are in UNIX/C ● However, their behavior is improved ● Examples – IO#eof? returns true before read fails while feof(3) does't. – IO.select can be used for buffered IO while select(2) can't.

Slide 27

Slide 27 text

Ruby for object-oriented programming ● Problem – OOP is excellent – But too heavy for daily scripting ● Solution – Pure object-oriented semantics – Support Non-OO style syntax

Slide 28

Slide 28 text

Pure object-oriented semantics ● Pure OO – All data (even integers) are objects – Most operators such as +, - are methods

Slide 29

Slide 29 text

Non-OO style syntax def fib(n) if n < 2 return n else return fib(n - 2) + fib(n - 1) end end puts fib(20) ● fib looks like just a function, isn't it?

Slide 30

Slide 30 text

Non-OO syntax + Pure OO semantics ● Receivers can be omitted – fib(20) is a short form of self.fib(20) ● At top level: – self is an instance of Object – def defines a method in Object

Slide 31

Slide 31 text

Ruby for fun ● For Matz's fun – Creating a new language was his dream ● For your fun – Who are YOU? – YOU = programmers

Slide 32

Slide 32 text

Shugo's first contact of Ruby ● In 1997 ● He was a Java programmer – "Please don't throw a stone at me!" ● Posted his regexp library in a Java ML ● Someone said "Ruby's Regexp is better" ● He fell in love with Ruby, threw away Java ● Got involved with Ruby development

Slide 33

Slide 33 text

How he learned to stop worrying and love Ruby ● His worries were: – Ruby is unpoplular, isn't it? – Ruby is slow, isn't it? – Dynamic typing is unsafe, isn't it?

Slide 34

Slide 34 text

Ruby is unpopular, isn't it? ● Yes, it was. – No books – No real world applications – No recruitment for Ruby programmers ● But all the more, Ruby was worth learning – Ruby was the "Secret Weapon" – Read Paul Graham's "Beating the Averages"

Slide 35

Slide 35 text

Ruby is slow, isn't it? ● Yes, it's slow because it's: – Dynamically typed ● Can't use type information for optimization – Pure object oriented ● No primitive types like int in Java – Extremely dynamic ● Method redefinition etc... ● But, the slowness is acceptable – At least for I/O bound applications

Slide 36

Slide 36 text

Dynamic typing is unsafe, isn't it? ● Yes, so write test code ● Shugo sais... – Ruby programming is like riding a motorcycle ● You can go anywhere anytime you want ● But you may sometimes slip down – It's fun for me

Slide 37

Slide 37 text

Ruby 1.0 - 1.6 ● 1997 - 2002 ● Many changes

Slide 38

Slide 38 text

Ruby 1.8 ● The first release was on Aug 4th 2003 ● Stable ● The most successful version of Ruby

Slide 39

Slide 39 text

Ruby 1.8 is past ● 1.8.8 – Never released ● 1.8.7 – Only bug fixes until June 2012 – Only security fixes until June 2013

Slide 40

Slide 40 text

Some of Shugo's past works ● puts ● callcc ● protected

Slide 41

Slide 41 text

puts ● writeln and println were rejected – They came from Pascal and Java – Matz likes neither Pascal nor Java ● Shugo proposed the name puts [ruby-dev:771] – It came from C (Matz likes C) – The behavior is a bit weird, though... ● puts "A" #=> prints A and \n ● puts "A\n" #=> prints A and \n, too ● puts [1,2,3]

Slide 42

Slide 42 text

calcc ● Introduced callcc into Ruby [ruby-dev:4206] ● Provides first class continuations – Came from Scheme (call-with-current- continuation) ● Ruby's Black Magic! – (.*)eval – callcc – set_trace_func – ObjectSpace

Slide 43

Slide 43 text

callcc ● Saves & loads interpreter state – Used like “goto”, but can jump between methods ● Not encouraged to use – Removed from built-in libraries in Ruby 1.9 – It may be completely removed in Ruby 2.0

Slide 44

Slide 44 text

Why is callcc evil? ● Objects are mutable in Ruby – callcc doesn't restore the state of objects ● C calls and Ruby calls are nested in Ruby – C calls are also restored by callcc – Most C code doesn't take care of it ● Even built-in methods used to crash – [ruby-dev:24301] Hash#rehash [ruby-dev:24303] Hash#each [ruby-dev:24378] sort_by [ruby-dev:24432] String#gsub! [ruby-dev:24463] each_with_index [ruby- dev:24487] Dir.glob [ruby-dev:24499] each_slice

Slide 45

Slide 45 text

Example of callcc 01: require "continuation" 02: 03: cont = nil 04: x = callcc { |c| 05: cont = c 06: "first" 07: } 08: p x 09: if x == "first" 10: # go to line 04 with value "second" 11: cont.call("second") 12: end

Slide 46

Slide 46 text

Non-deterministic problems ● Problem from the “SICP” book – Baker, Cooper, Fletcher, Miller, and Smith live on different floors of an apartment house that contains only five floors. – Baker does not live on the top floor. – Cooper does not live on the bottom floor. – Fletcher does not live on either the top or the bottom floor. – Miller lives on a higher floor than does Cooper. – Smith does not live on a floor adjacent to Fletcher's. – Fletcher does not live on a floor adjacent to Cooper's. – Where does everyone live?

Slide 47

Slide 47 text

Solution require "amb" a = Amb.new baker = a.choose(1, 2, 3, 4, 5) cooper = a.choose(1, 2, 3, 4, 5) fletcher = a.choose(1, 2, 3, 4, 5) miller = a.choose(1, 2, 3, 4, 5) smith = a.choose(1, 2, 3, 4, 5) a.assert([baker, cooper, fletcher, miller, smith].uniq.length == 5) a.assert(baker != 5) a.assert(cooper != 1) a.assert(fletcher != 1 && fletcher != 5) a.assert(miller > cooper) a.assert((smith - fletcher).abs != 1) a.assert((fletcher - cooper).abs != 1) p [baker, cooper, fletcher, miller, smith]

Slide 48

Slide 48 text

Implementation of Amb class Amb ... def choose(*choices) choices.each { |choice| callcc { |fk| @back << fk return choice } } failure end def failure @back.pop.call end def assert(cond) failure unless cond end

Slide 49

Slide 49 text

protected ● Equivalent to Java's protected ● Useful to implement binary operators def ==(other) @value == other.value # error if value is private end protected def value; @value; end

Slide 50

Slide 50 text

“abnormal” use of protected ● Often seen in Rails applications ● Use private instead if possible – private methods can be called from subclasses class ApplicationController < ActionController::Base before_filter :login_required protected # Why not private? def login_required ...

Slide 51

Slide 51 text

Ruby's present

Slide 52

Slide 52 text

Ruby's present ● Popularity ● JIS/ISO Standard ● Ruby 1.9

Slide 53

Slide 53 text

Mainstream Language ● TIOBE Index (October 2011) ● "A" means mainstream

Slide 54

Slide 54 text

Ruby on Rails ● Rails made Ruby more popular ● Rails may be more popular than Ruby

Slide 55

Slide 55 text

Difference between Ruby and Rails?

Slide 56

Slide 56 text

Same question in Japan

Slide 57

Slide 57 text

Ruby standard ● JIS X 3017 – published on March 22nd 2011 – JIS = Japanese Industrial Standards ● ISO/IEC JTC1 Fast-Track procedure – "Voting closed 6 September; it received a 100% approval. Only Japan made comments." ● http://grouper.ieee.org/groups/plv/DocLog/300- 399/360-thru-379/22-WG23-N-0364/n0364.pdf

Slide 58

Slide 58 text

Why Ruby standard? ● Business reasons – Tranquilizer for enterprise users – Necessary for government procurement ● Technical reason – Written specification may help development ● We have found some bugs in CRuby:) ● The standard may also have bugs:(

Slide 59

Slide 59 text

How Ruby has been standardized ● Codified the existing (implicit) specification – No new invention by the standardization WG – Ruby development is kept free ● Asked public comments from the community – Over 100 comments – Most of them were reflected in the standard

Slide 60

Slide 60 text

Ruby 1.9 ● New implementation ● New syntax ● Other new features

Slide 61

Slide 61 text

New implementation ● YARV = Yet Another Ruby VM ● It's now the Ruby VM ● Word-code interpreter – The size of opcode and operands is the size of pointers ● Made 1.9 faster than Ruby 1.8

Slide 62

Slide 62 text

New syntax ● New hash syntax ● Extended splat

Slide 63

Slide 63 text

New hash syntax # Normal hash syntax ("hash rocket") h = {:a => 1, :b => 2} # New hash syntax h = {a: 1, b: 2} # e.g. method options (keyword argument) foo opt1: 1, opt2: 2

Slide 64

Slide 64 text

Extended splat # Splat in the middle # (1) def foo(a, *b, c) … # (2) bar(a *b, c) # (3) baz{|a, *b, c| … } # (4) a, *b, c = ary # Multiple splats x = [1,2,3]; y = [4,5,6] ary = *x, *y foo *x, *y

Slide 65

Slide 65 text

Other new features ● M17N ● Fiber

Slide 66

Slide 66 text

M17N ● M17N = multilingualization – UCS (Universal Character Set) Normalization – CSI (Code Set Independent) ● Strings are character strings – " ルビー "[0] #=> " ル " – In Ruby 1.8, strings are byte strings – " ルビー "[0] #=> 227

Slide 67

Slide 67 text

Fiber ● Semi-coroutine ● Like Thread, but you can control when to switch (Fiber.yield)

Slide 68

Slide 68 text

File.open f = File.open # do something with f p f f.close File.open do |f| # do something with f p f end

Slide 69

Slide 69 text

File.open def initialize # ?? end def process # ?? end def finish # ?? end File.open do |f| # do something with f p f end

Slide 70

Slide 70 text

File.open def initialize @f = File.open end def process p @f end def finish @f.close end File.open do |f| # do something with f p f end

Slide 71

Slide 71 text

Takoyaki.open def initialize # ?? end def process # ?? end def finish # ?? end Takoyaki.open do |t| # do something with t p t end

Slide 72

Slide 72 text

Use Fiber def initialize @fiber = Fiber.new{ Takoyaki.open do |t| Fiber.yield(t) # stop execution # do something with t p t end } @t = @fiber.resume end def process; @fiber.resume; end def finish; @t.close; end

Slide 73

Slide 73 text

Ruby 1.9 is present ● Everyone should use it now ● Migration from Ruby 1.8 to 1.9 is easier than migration from Rails 2 to Rails 3

Slide 74

Slide 74 text

Ruby's future

Slide 75

Slide 75 text

Ruby 2.0 ● Had been a vaporware for a long time – Matz mentioned Ruby 2.0 at RubyConf 2001 ● Something like Web 2.0?

Slide 76

Slide 76 text

Now Ruby 2.0 is real $ ruby-trunk -v ruby 2.0.0dev (2011-10-31 trunk 33588) [i686-linux] $ fgrep -B2 '2.0' ChangeLog Wed Oct 19 17:06:54 2011 Yukihiro Matsumoto * version.h (RUBY_VERSION): finally declare start of 2.0 work! $

Slide 77

Slide 77 text

Ruby 2.0 is future ● It's near future ● Current schedule – Aug 24th 2012 big-feature freeze – Oct 24th 2012 feature freeze – Feb 24th 2013 2.0 release ● Will not be a big change – Matz: 100% upper-compatible to 1.9

Slide 78

Slide 78 text

New features in Ruby 2.0 ● Accepted features – Keyword arguments – Module#prepend ● Features under discussion – Refinements

Slide 79

Slide 79 text

Keyword arguments # Existing syntax: def foo(opts={}) opts[:opt1] ||= ”foo” if opts[:opt2] … # New syntax: def foo(opt1: "foo", opt2: nil) if opt2 ... # Calling is the same foo(opt1: 1, opt2: 2) – Discussion ongoing: [Redmine #5474]

Slide 80

Slide 80 text

Monkey patching ● Redefine methods of existing classes ● Extend classes and modules on runtime

Slide 81

Slide 81 text

Use cases of monkey patching ● 1. Workaround for a bug of a library ● 2. Extensions of built-in classes – Often used for internal DSLs – – ● 3. Plugin system like Rails @empty_array.size.should == 0 # Integer#should is added

Slide 82

Slide 82 text

alias_method_chain ActionView::Helpers::RenderingHelper.module_eval do def render_with_update(options = {}, locals = {}, &block) if options == :update update_page(&block) else render_without_update(options, locals, &block) end end alias_method_chain :render, :update end

Slide 83

Slide 83 text

Module#prepend ● Replaces alias_method_chain ● ● ● ● ● ● – Discussion ongoing: [Redmine: #1102] module RenderUpdate def render(options = {}, locals = {}, &block) if options == :update update_page(&block) else # call the original RenderingHelper#render super(options, locals, &block) end end end ActionView::Helpers::RenderingHelper.module_eval do prepend RenderUpdate end

Slide 84

Slide 84 text

Refinements ● Provides “scoped” monkey patching module MathN refine Fixnum do def /(other) quo(other) end end end module Foo using MathN p 1 / 2 #=> (1/2) end p 1 / 2 #=> 0

Slide 85

Slide 85 text

Considerations for Refinements ● Performance issue – It's slow even if refinements are not used ● Scope of refinements – Lexical scoping is safe, but not flexible – Do we need refinement propagation? ● Discussions: [Redmine: #4085]

Slide 86

Slide 86 text

Who creates Ruby's future? ● It's You! ● Redmine: – http://redmine.ruby-lang.org/projects/ruby-trunk/ ● Mailing list: – http://www.ruby-lang.org/en/community/mailing-lists/

Slide 87

Slide 87 text

Enumerable#lazy ● [Redmine: #4890]

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

2011 Call for grant proposals The Ruby Association is looking to give grants for development projects related to Ruby implementations, libraries and frameworks, including projects that improve existing software.

Slide 90

Slide 90 text

2011 Call for grant proposals ● "looking to give grants for development projects" ● Anyone can submit proposals ● Grant size: ¥500,000 JPY (≒ $6400 USD) – Selection criteria ● Impact on the productivity and performance of Ruby and its environment ● Originality and creativity of the project ● Feasibility of the project ● Details: http://www.ruby-assn.org/en/index.htm

Slide 91

Slide 91 text

Conclusion

Slide 92

Slide 92 text

Ruby's past ● Ruby was created for fun ● Ruby 1.8 is past

Slide 93

Slide 93 text

Ruby's present ● Ruby is now mainstream ● Ruby 1.9 is present

Slide 94

Slide 94 text

Ruby's future ● Ruby 2.0 is future ● You can change it! – http://redmine.ruby-lang.org/projects/ruby-trunk/ – http://www.ruby-lang.org/en/community/mailing-lists/

Slide 95

Slide 95 text

¡Gracias!

Slide 96

Slide 96 text

Question Escape Ladder ● #1 “That is a good question.” ● #2 “Let's talk about it later.” ● #3