Post on 19-Jan-2015
description
transcript
seesaw
happy 20th birthday!lunedì 4 marzo 13
seesaw
Yukihiro Matsumoto matz
osakaapril 14, 1965
lunedì 4 marzo 13
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
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
seesaw
implementations
• Matz's Ruby Interpreter or MRI, uses its own Ruby-specific virtual machine written in C
• JRuby, runs on the Java virtual machine
• Rubinius, C++ bytecode VM that uses LLVM to compile to machine code at runtime
• MagLev, a Smalltalk implementation
• MacRuby, an OS X implementation on the Objective-C runtime
• IronRuby, an implementation on the .NET Framework
lunedì 4 marzo 13
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
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
seesaw
irb
seesawlunedì 4 marzo 13
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
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
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
seesaw
blocks & iterators
do puts "Hello, World!" end
{ puts "Hello, World!" }
oppure
lunedì 4 marzo 13
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 closure!@block.call("Jon")# => "Hello, Jon!"
lunedì 4 marzo 13
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
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
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
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
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
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
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
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
seesaw
2.0fully backward compatible with Ruby 1.9.3
lunedì 4 marzo 13
seesaw
default source encoding is UTF-8
lunedì 4 marzo 13
seesaw
require improvementslunedì 4 marzo 13
seesaw
GC is copy-on-write friendly
lunedì 4 marzo 13
seesaw
Fine-Grained Asynchronous Interrupt Handling
lunedì 4 marzo 13
seesaw
D-Trace support
lunedì 4 marzo 13
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
seesaw
problems are on method definition
1.9
lunedì 4 marzo 13
seesaw
Caller side doesn't change
lunedì 4 marzo 13
seesaw
lazy enumerables
lunedì 4 marzo 13
seesaw
lazy enumerables
lunedì 4 marzo 13
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
seesaw
Module Prepending
lunedì 4 marzo 13
seesaw
Module Prepending
lunedì 4 marzo 13
seesaw
Module Prepending
no more alias method chain
lunedì 4 marzo 13
seesaw
:ruby and so.why?(‘not’)
seesawlunedì 4 marzo 13
seesaw
:ruby and so.why?(‘not’)
seesawlunedì 4 marzo 13
seesawlunedì 4 marzo 13
seesaw
questions?
lunedì 4 marzo 13
seesaw
!anks.@fuzziness
michele@franzin.netlunedì 4 marzo 13
http://en.wikipedia.org/wiki/Ruby_(programming_language)
http://benhoskin.gs/2013/02/24/ruby-2-0-by-example
http://benhoskin.gs/2013/02/24/getting-to-know-ruby-2-0
https://speakerdeck.com/shyouhei/whats-new-in-ruby-2-dot-0
http://www.slideshare.net/peter_marklund/ruby-on-rails-101-presentation-slides-for-a-five-day-introductory-course
credits
lunedì 4 marzo 13