ruby 20th birthday

46
seesaw happy 20th birthday! lunedì 4 marzo 13

Upload: michele-franzin

Post on 19-Jan-2015

683 views

Category:

Technology


1 download

DESCRIPTION

Ruby language overview & new features of 2.0 release - talk presented at #64 Java User Group meeting on march 2, 2013

TRANSCRIPT

Page 1: Ruby 20th birthday

seesaw

happy 20th birthday!lunedì 4 marzo 13

Page 2: Ruby 20th birthday

seesaw

Yukihiro Matsumoto matz

osakaapril 14, 1965

lunedì 4 marzo 13

Page 3: Ruby 20th birthday

seesaw

“I always thought Smalltalk would beat Java.I just didn’t know if would be called ‘Ruby’ when it did so”

- Kent Beck -

lunedì 4 marzo 13

Page 4: Ruby 20th birthday

seesaw

The Ruby Language

• Designed for programmer productivity and fun

• Generic, interpreted, reflective, with garbage collection

• Optimized for people rather than computers

• More powerful than Perl, more object oriented than Python

• Everything is an object. There are no primitive types

• Strong dynamic typing

lunedì 4 marzo 13

Page 6: Ruby 20th birthday

seesaw

versions

v0.95 - December 21, 1995v1.0 - December 25, 1996

v1.2 - December 1998v1.3 - year1999

v1.4 - August 1999v1.6 - September 2000

v1.8 - August 2003Ruby on Rails - 2005

v1.9 - December 2007

lunedì 4 marzo 13

Page 7: Ruby 20th birthday

seesaw

Everything in Ruby is

‣ Assignment - binding names to objects

‣ Control structures - if/else, while, case

‣ Sending messages to objects - methods

lunedì 4 marzo 13

Page 8: Ruby 20th birthday

seesaw

irb

seesawlunedì 4 marzo 13

Page 9: Ruby 20th birthday

seesaw

strings

a = "\nThis is a double-quoted string\n"a = %Q{\nThis is a double-quoted string\n}a = %{\nThis is a double-quoted string\n}a = %/\nThis is a double-quoted string\n/a = <<-BLOCK This is a double-quoted stringBLOCK

a = 'This is a single-quoted string'a = %q{This is a single-quoted string}

lunedì 4 marzo 13

Page 10: Ruby 20th birthday

seesaw

collections

a = [1, 'hi', 3.14, 1, 2, [4, 5]] puts a[2] # 3.14puts a.[](2) # 3.14puts a.reverse # [[4, 5], 2, 1, 3.14, 'hi', 1]puts a.flatten.uniq # [1, 'hi', 3.14, 2, 4, 5]

lunedì 4 marzo 13

Page 11: Ruby 20th birthday

seesaw

associative arrayshash = Hash.newhash = { :water => 'wet', :fire => 'hot' }puts hash[:fire]# => hot hash.each do |key, value| puts "#{key} is #{value}"end# => water is wet# fire is hot hash.delete :water # Deletes water: 'wet'hash.delete_if { |key,value| value == 'hot'}# Deletes :fire => 'hot'

lunedì 4 marzo 13

Page 12: Ruby 20th birthday

seesaw

blocks & iterators

do puts "Hello, World!" end

{ puts "Hello, World!" }

oppure

lunedì 4 marzo 13

Page 13: Ruby 20th birthday

seesaw

closures

# In an object instance variable (denoted with '@'), remember a block.def remember(&a_block) @block = a_blockend # Invoke the above method, giving it a block which takes a name.remember {|name| puts "Hello, #{name}!"} # When the time is right (for the object) -- call the [email protected]("Jon")# => "Hello, Jon!"

lunedì 4 marzo 13

Page 14: Ruby 20th birthday

seesaw

closures

def create_set_and_get(initial_value=0) # Note the default value of 0 closure_value = initial_value return Proc.new {|x| closure_value = x}, Proc.new { closure_value }end setter, getter = create_set_and_get # ie. returns two valuessetter.call(21)getter.call # => 21

lunedì 4 marzo 13

Page 15: Ruby 20th birthday

seesaw

closures

def create_set_and_get(initial_value=0) # Note the default value of 0 closure_value = initial_value return Proc.new {|x| closure_value = x}, Proc.new { closure_value }end setter, getter = create_set_and_get # ie. returns two valuessetter.call(21)getter.call # => 21 #You can also use a parameter variable as a binding for the closure.#So the above can be rewritten as... def create_set_and_get(closure_value=0) return proc {|x| closure_value = x } , proc { closure_value }en

lunedì 4 marzo 13

Page 16: Ruby 20th birthday

seesaw

yield

def use_hello yield "hello"end # Invoke the above method, passing it a block.use_hello {|string| puts string} # => 'hello'

lunedì 4 marzo 13

Page 17: Ruby 20th birthday

seesaw

enumerationarray = [1, 'hi', 3.14]array.each {|item| puts item }# => 1# => 'hi'# => 3.14 array.each_index do|index| puts "#{index}: #{array[index]}"end# => 0: 1# => 1: 'hi'# => 2: 3.14 # The following uses a Range(3..6).each {|num| puts num }# => 3# => 4# => 5# => 6

lunedì 4 marzo 13

Page 18: Ruby 20th birthday

seesaw

functional programming[1,3,5].inject(10) {|sum, element| sum + element}# => 19

File.open('file.txt', 'w') do |file| # 'w' denotes "write mode" file.puts 'Wrote some text.'end # File is automatically closed here File.readlines('file.txt').each do |line| puts lineend

(1..10).collect {|x| x*x}# => [1, 4, 9, 16, 25, 36, 49, 64, 81, 100](1..5).collect(&:to_f)# => [1.0, 2.0, 3.0, 4.0, 5.0]

lunedì 4 marzo 13

Page 19: Ruby 20th birthday

seesaw

classesclass Person attr_reader :name, :age def initialize(name, age) @name, @age = name, age end def <=>(person) # Comparison operator for sorting age <=> person.age end def to_s "#{name} (#{age})" endend group = [ Person.new("Bob", 33), Person.new("Chris", 16), Person.new("Ash", 23)] puts group.sort.reverse

lunedì 4 marzo 13

Page 20: Ruby 20th birthday

seesaw

monkey patching

# re-open Ruby's Time classclass Time def yesterday self - 86400 endend today = Time.now # => Thu Aug 14 16:51:50 +1200 2012yesterday = today.yesterday# => Wed Aug 13 16:51:50 +1200 2012

lunedì 4 marzo 13

Page 21: Ruby 20th birthday

seesaw

metaprogrammingCOLORS = { black: "000", red: "f00", green: "0f0", yellow: "ff0", blue: "00f", magenta: "f0f", cyan: "0ff", white: "fff" } class String COLORS.each do |color,code| define_method "in_#{color}" do "<span style=\"color: ##{code}\">#{self}</span>" end endend

lunedì 4 marzo 13

Page 22: Ruby 20th birthday

seesaw

2.0fully backward compatible with Ruby 1.9.3

lunedì 4 marzo 13

Page 23: Ruby 20th birthday

seesaw

default source encoding is UTF-8

lunedì 4 marzo 13

Page 24: Ruby 20th birthday

seesaw

require improvementslunedì 4 marzo 13

Page 25: Ruby 20th birthday

seesaw

GC is copy-on-write friendly

lunedì 4 marzo 13

Page 26: Ruby 20th birthday

seesaw

Fine-Grained Asynchronous Interrupt Handling

lunedì 4 marzo 13

Page 27: Ruby 20th birthday

seesaw

D-Trace support

lunedì 4 marzo 13

Page 28: Ruby 20th birthday

seesaw

keyword argumentsdef render(source, opts = {}) opts = {fmt: 'html'}.merge(opts) r = Renderer.for(opts[:fmt]) r.render(source)end

render(template, fmt: 'json')

def render(source, fmt: 'html') r = Renderer.for(fmt) r.render(source)end

render(template, fmt: 'json')

2.0

1.9

lunedì 4 marzo 13

Page 29: Ruby 20th birthday

seesaw

problems are on method definition

1.9

lunedì 4 marzo 13

Page 30: Ruby 20th birthday

seesaw

Caller side doesn't change

lunedì 4 marzo 13

Page 31: Ruby 20th birthday

seesaw

lazy enumerables

lunedì 4 marzo 13

Page 32: Ruby 20th birthday

seesaw

lazy enumerables

lunedì 4 marzo 13

Page 33: Ruby 20th birthday

seesaw

lazy enumerables

def natural_numbers (1..Float::INFINITY).lazyend

def primes natural_numbers.select {|n| (2..(n**0.5)).all? {|f| n % f > 0 } }end

primes.take(10).force#=> [1, 2, 3, 5, 7, 11, 13, 17, 19, 23]

lunedì 4 marzo 13

Page 34: Ruby 20th birthday

seesaw

Module Prepending

lunedì 4 marzo 13

Page 35: Ruby 20th birthday

seesaw

Module Prepending

lunedì 4 marzo 13

Page 36: Ruby 20th birthday

seesaw

Module Prepending

no more alias method chain

lunedì 4 marzo 13

Page 37: Ruby 20th birthday

seesaw

:ruby and so.why?(‘not’)

seesawlunedì 4 marzo 13

Page 38: Ruby 20th birthday

seesaw

:ruby and so.why?(‘not’)

seesawlunedì 4 marzo 13

Page 39: Ruby 20th birthday

seesaw

http://www.ruby-lang.org/

lunedì 4 marzo 13

Page 40: Ruby 20th birthday

seesaw

http://www.ruby-doc.org/

lunedì 4 marzo 13

Page 41: Ruby 20th birthday

seesaw

http://modulecounts.com/

lunedì 4 marzo 13

Page 42: Ruby 20th birthday

seesaw

http://www.rubygems.org/

lunedì 4 marzo 13

Page 43: Ruby 20th birthday

seesawlunedì 4 marzo 13

Page 44: Ruby 20th birthday

seesaw

questions?

lunedì 4 marzo 13