Ruby 20th birthday

1,378 views

Published on

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

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,378
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
7
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Ruby 20th birthday

  1. 1. happy 20th birthday! seesawlunedì 4 marzo 13
  2. 2. Yukihiro Matsumoto matz osaka april 14, 1965 seesawlunedì 4 marzo 13
  3. 3. “I always thought Smalltalk would beat Java.I just didn’t know if would be called ‘Ruby’ when it did so” - Kent Beck - seesawlunedì 4 marzo 13
  4. 4. 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 seesawlunedì 4 marzo 13
  5. 5. implementations • Matzs 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 seesawlunedì 4 marzo 13
  6. 6. versions v0.95 - December 21, 1995 v1.0 - December 25, 1996 v1.2 - December 1998 v1.3 - year1999 v1.4 - August 1999 v1.6 - September 2000 v1.8 - August 2003 Ruby on Rails - 2005 v1.9 - December 2007 seesawlunedì 4 marzo 13
  7. 7. Everything in Ruby is ‣ Assignment - binding names to objects ‣ Control structures - if/else, while, case ‣ Sending messages to objects - methods seesawlunedì 4 marzo 13
  8. 8. irb seesawlunedì 4 marzo 13
  9. 9. strings a = "nThis is a double-quoted stringn" a = %Q{nThis is a double-quoted stringn} a = %{nThis is a double-quoted stringn} a = %/nThis is a double-quoted stringn/ a = <<-BLOCK This is a double-quoted string BLOCK a = This is a single-quoted string a = %q{This is a single-quoted string} seesawlunedì 4 marzo 13
  10. 10. collections a = [1, hi, 3.14, 1, 2, [4, 5]] puts a[2] # 3.14 puts a.[](2) # 3.14 puts a.reverse # [[4, 5], 2, 1, 3.14, hi, 1] puts a.flatten.uniq # [1, hi, 3.14, 2, 4, 5] seesawlunedì 4 marzo 13
  11. 11. associative arrays hash = Hash.new hash = { :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 seesawlunedì 4 marzo 13
  12. 12. blocks & iterators do puts "Hello, World!" end oppure { puts "Hello, World!" } seesawlunedì 4 marzo 13
  13. 13. closures # In an object instance variable (denoted with @), remember a block. def remember(&a_block) @block = a_block end # 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!" seesawlunedì 4 marzo 13
  14. 14. 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 values setter.call(21) getter.call # => 21 seesawlunedì 4 marzo 13
  15. 15. 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 values setter.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 seesawlunedì 4 marzo 13
  16. 16. yield def use_hello yield "hello" end # Invoke the above method, passing it a block. use_hello {|string| puts string} # => hello seesawlunedì 4 marzo 13
  17. 17. enumeration array = [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 seesawlunedì 4 marzo 13
  18. 18. 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 line end (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] seesawlunedì 4 marzo 13
  19. 19. classes class 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})" end end group = [ Person.new("Bob", 33), Person.new("Chris", 16), Person.new("Ash", 23) ] puts group.sort.reverse seesawlunedì 4 marzo 13
  20. 20. monkey patching # re-open Rubys Time class class Time def yesterday self - 86400 end end today = Time.now # => Thu Aug 14 16:51:50 +1200 2012 yesterday = today.yesterday # => Wed Aug 13 16:51:50 +1200 2012 seesawlunedì 4 marzo 13
  21. 21. metaprogramming COLORS = { 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 end end seesawlunedì 4 marzo 13
  22. 22. 2.0 fully backward compatible with Ruby 1.9.3 seesawlunedì 4 marzo 13
  23. 23. default source encoding is UTF-8 seesawlunedì 4 marzo 13
  24. 24. require improvements seesawlunedì 4 marzo 13
  25. 25. GC is copy-on-write friendly seesawlunedì 4 marzo 13
  26. 26. Fine-Grained Asynchronous Interrupt Handling seesawlunedì 4 marzo 13
  27. 27. D-Trace support seesawlunedì 4 marzo 13
  28. 28. keyword arguments def render(source, opts = {}) opts = {fmt: html}.merge(opts) r = Renderer.for(opts[:fmt]) 1.9 r.render(source) end render(template, fmt: json) def render(source, fmt: html) r = Renderer.for(fmt) r.render(source) 2.0 end render(template, fmt: json) seesawlunedì 4 marzo 13
  29. 29. 1.9 problems are on method definition seesawlunedì 4 marzo 13
  30. 30. Caller side doesnt change seesawlunedì 4 marzo 13
  31. 31. lazy enumerables seesawlunedì 4 marzo 13
  32. 32. lazy enumerables seesawlunedì 4 marzo 13
  33. 33. lazy enumerables def natural_numbers (1..Float::INFINITY).lazy end 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] seesawlunedì 4 marzo 13
  34. 34. Module Prepending seesawlunedì 4 marzo 13
  35. 35. Module Prepending seesawlunedì 4 marzo 13
  36. 36. Module Prepending no more alias method chain seesawlunedì 4 marzo 13
  37. 37. :ruby and so.why?(‘not’) seesawlunedì 4 marzo 13
  38. 38. :ruby and so.why?(‘not’) seesawlunedì 4 marzo 13
  39. 39. http://www.ruby-lang.org/ seesawlunedì 4 marzo 13
  40. 40. http://www.ruby-doc.org/ seesawlunedì 4 marzo 13
  41. 41. http://modulecounts.com/ seesawlunedì 4 marzo 13
  42. 42. http://www.rubygems.org/ seesawlunedì 4 marzo 13
  43. 43. seesawlunedì 4 marzo 13
  44. 44. questions? seesawlunedì 4 marzo 13
  45. 45. !anks. @fuzziness michele@franzin.net seesawlunedì 4 marzo 13
  46. 46. http://en.wikipedia.org/wiki/Ruby_(programming_language) credits 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-courselunedì 4 marzo 13

×