Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ruby for Java minds

Ruby for Java minds

My talk in JRubyConf EU 2013

Jano González

August 14, 2013
Tweet

More Decks by Jano González

Other Decks in Programming

Transcript

  1. RUBY
    FOR JAVA MINDS

    View full-size slide

  2. @janogonzalez

    View full-size slide

  3. COLD WAR
    IS OVER

    View full-size slide

  4. GRUNGE GOES
    MAINSTREAM

    View full-size slide

  5. SELF-
    ALIENATION

    View full-size slide

  6. ANGST IS THE NEW
    COOL

    View full-size slide

  7. MEANWHILE IN THE
    PROGRAMMING
    WORLD...

    View full-size slide

  8. RAISING AGAINST
    THE MAINSTREAM

    View full-size slide

  9. JAMES GOSLING

    View full-size slide

  10. JAMES GOSLING

    View full-size slide

  11. “Write Once,
    Run Anywhere”

    View full-size slide

  12. THE 5 PRINCIPLES
    • It should be "simple, object-oriented and familiar"
    • It should be "robust and secure"
    • It should be "architecture-neutral and portable"
    • It should execute with "high performance"
    • It should be "interpreted, threaded, and dynamic"

    View full-size slide

  13. (THAT WAS NOT RSPEC)

    View full-size slide

  14. “Ruby is designed to make
    programmers happy”

    View full-size slide

  15. MATZ WINS THE
    NOBEL PEACE PRICE

    View full-size slide

  16. (HISTORICAL ACCURACY
    IS OVERRATED)

    View full-size slide

  17. 2 DIFFERENT
    PHILOSOPHIES

    View full-size slide

  18. HOW DO WE
    EMBRACE RUBY?

    View full-size slide

  19. COMPUTER APOCALYPSE
    DID NOT HAPPEN

    View full-size slide

  20. I WENT TO A “JAVA
    SCHOOL”

    View full-size slide

  21. JAVA EE, SPRING
    AND CUBICLES

    View full-size slide

  22. HEY, RAILS LOOK
    COOL!

    View full-size slide

  23. HEY, THERE IS A
    FLY OVER THERE!

    View full-size slide

  24. (STUPID FLY)

    View full-size slide

  25. RAILS VS JAVA EE
    WAS OVER

    View full-size slide

  26. HEY, RUBY LOOKS
    COOL!

    View full-size slide

  27. WAIT, HOW DO I
    DECLARE AN INTERFACE?

    View full-size slide

  28. “The determined Real Programmer can write
    FORTRAN programs in any language.”
    –Ed Post

    View full-size slide

  29. HOW TO WRITE RUBY
    THAT FEELS LIKE RUBY?

    View full-size slide

  30. A ZEN MOMENT

    View full-size slide

  31. BEGINNER’S
    MIND

    View full-size slide

  32. EMPTY YOUR CUP

    View full-size slide

  33. BACK TO BASICS

    View full-size slide

  34. THE KEY TO
    UNDERSTAND RUBY

    View full-size slide

  35. EXPRESSIONS
    +
    OBJECTS

    View full-size slide

  36. SYNTAX IS
    EXPRESSION-ORIENTED

    View full-size slide

  37. “LISP programmers know the value
    of everything and the cost of
    nothing.”
    - Alan Perlis

    View full-size slide

  38. EXPRESSIONS
    def average(a, b)
    (a + b) / 2
    end
    # => nil
    average(10, 2)
    # => 6

    View full-size slide

  39. EXPRESSIONS
    level = case
    when 1..2 then :excellent
    when 3..5 then :normal
    else :bad
    end

    View full-size slide

  40. ALL VALUES ARE
    OBJECTS

    View full-size slide

  41. OBJECTS
    -1.class
    # => Fixnum

    View full-size slide

  42. OBJECTS
    String.object_id
    # => 70138854779960

    View full-size slide

  43. OBJECTS
    nil.nil?
    # => true

    View full-size slide

  44. WO IST DEIN GOTT JETZT

    View full-size slide

  45. MESSAGES
    EVERYWHERE

    View full-size slide

  46. MESSAGES
    40 + 2
    # => 42

    View full-size slide

  47. MESSAGES
    40.+(2)
    # => 42

    View full-size slide

  48. MESSAGES
    40.send(:+, 2)
    # => 42

    View full-size slide

  49. MESSAGES

    METHODS

    View full-size slide

  50. MESSAGES
    class AccountProtectionProxy
    def initialize(real_account, owner)
    @subject = real_account
    @owner = owner
    end
    ...

    View full-size slide

  51. MESSAGES
    class AccountProtectionProxy
    ...
    def method_missing(name, *args)
    check_access
    @subject.send(name, *args)
    end
    ...
    end

    View full-size slide

  52. DYNAMIC TYPING

    View full-size slide

  53. FORGET ABOUT
    JAVA INTERFACES

    View full-size slide

  54. JAVA
    public interface Report {
    ReportFile generate();
    }

    View full-size slide

  55. JAVA
    public class PDFReport
    implements Report {
    ....
    }

    View full-size slide

  56. JAVA
    public class CSVReport
    implements Report {
    ....
    }

    View full-size slide

  57. JAVA
    public void print(List reports) {
    if (reports == null) {
    return;
    }
    for (Report r : reports) {
    ReportFile file = r.generate();
    ...
    }
    }

    View full-size slide

  58. DYNAMIC TYPING
    def print(reports)
    [*reports].each do |r|
    file = r.generate
    ...
    end
    end

    View full-size slide

  59. DYNAMIC TYPING
    class PDFReport
    def generate
    ...
    end
    ...
    end

    View full-size slide

  60. DYNAMIC TYPING
    class CVSReport
    def generate
    ...
    end
    ...
    end

    View full-size slide

  61. DYNAMIC TYPING
    class Duck
    def generate
    ...
    end
    ...
    end

    View full-size slide

  62. PROGRAM TO
    INTERFACES, NOT
    TO interface

    View full-size slide

  63. USED
    EVERYWHERE

    View full-size slide

  64. DYNAMIC TYPING
    "Jano in " << "Berlin"
    # => "Jano in Berlin"
    ["Santiago"] << "Berlin"
    # => ["Santiago", "Berlin"]

    View full-size slide

  65. IF YOU REALLY
    NEED TO CHECK...

    View full-size slide

  66. DYNAMIC TYPING
    if report.respond_to?(:generate)
    report.generate
    ...
    else
    ...
    end

    View full-size slide

  67. IF YOU REALLY, REALLY
    NEED TO CHECK...

    View full-size slide

  68. DYNAMIC TYPING
    if report.is_a?(Report)
    report.generate
    ...
    else
    ...
    end

    View full-size slide

  69. DYNAMIC BUT
    NOT WEAK

    View full-size slide

  70. STRONG TYPING
    100 + 'cool'
    # TypeError: String can't be
    coerced into Fixnum

    View full-size slide

  71. TYPE
    CONVERSIONS

    View full-size slide

  72. CONVERSIONS
    100 + 'cool'.to_i
    # => 100
    100 + Integer('cool')
    # ArgumentError: invalid
    value for Integer(): "lala"

    View full-size slide

  73. BLOCKS ARE USED
    & ABUSED IN RUBY

    View full-size slide

  74. PROGRAM LIKE A
    BOSS

    View full-size slide

  75. BLOCKS
    (1..100).select { |n| n % 3 == 0 }
    .map { |n| n * 2 }
    .reduce(:+)

    View full-size slide

  76. BLOCKS
    File.open('my.txt').each do |line|
    puts line if line =~ /jano/
    end

    View full-size slide

  77. BE MORE
    DECLARATIVE

    View full-size slide

  78. BLOCKS
    words.sort do |a, b|
    a.length <=> b.length
    end
    words.min_by? { |w| w.length }
    words.reject { |w| w.length > 8 }
    # etc...

    View full-size slide

  79. EXAMPLE: SELF
    YIELD

    View full-size slide

  80. BLOCKS
    class Job
    def initialize
    yield self if block_given?
    end
    ...
    end

    View full-size slide

  81. BLOCKS
    job = Job.new do |j|
    j.name = 'Print Reports'
    j.user = 'janogonzalez'
    ...
    end

    View full-size slide

  82. EXAMPLE:
    CALLBACKS

    View full-size slide

  83. BLOCKS
    class Job
    def on_finish(&block)
    @end_callback = block
    end
    def execute()
    ...
    @end_callback.call(self) if @end_callback
    end
    end

    View full-size slide

  84. BLOCKS
    job.on_finish do |j|
    puts "Finished job #{j.name}"
    puts "Status: #{j.status}”
    end

    View full-size slide

  85. MODULES AS
    MIXINS

    View full-size slide

  86. ADD BEHAVIOR TO
    INSTANCES

    View full-size slide

  87. MODULES
    module Model
    def persist
    ...
    end
    end

    View full-size slide

  88. MODULES
    class User
    include Model
    ...
    end

    View full-size slide

  89. MODULES
    job = User.new('Jano')
    job.persist

    View full-size slide

  90. ADD BEHAVIOR TO
    CLASSES

    View full-size slide

  91. MODULES
    module Findable
    def find_by_id(id)
    ...
    end
    end

    View full-size slide

  92. MODULES
    class User
    extends Findable
    ...
    end

    View full-size slide

  93. MODULES
    user = User.find_by_id(3)

    View full-size slide

  94. METAPROGRAMMING

    View full-size slide

  95. (USE WITH CAUTION)

    View full-size slide

  96. (AKA THE EVAL IS EVIL
    RULE)

    View full-size slide

  97. ACCESSORS
    class User
    attr_reader :name
    attr_reader :status
    end

    View full-size slide

  98. OPEN CLASSES

    View full-size slide

  99. OPEN CLASSES
    class Range
    def even
    select(&:even?)
    end
    end
    (0..10).even
    # => [2, 4, 6, 8, 10]

    View full-size slide

  100. OPEN CLASSES
    class BuggyClass
    def buggy_method
    # the fix
    ...
    end
    end

    View full-size slide

  101. OPEN CLASSES
    class Job
    alias_method :old_execute, :execute
    def execute
    ...
    old_execute
    end
    end

    View full-size slide

  102. DEFINE METHODS

    View full-size slide

  103. DEFINE METHODS
    obj = User.new
    User.class_eval do
    def represent
    ...
    end
    end
    obj.represent

    View full-size slide

  104. DEFINE METHODS
    User.instance_eval do
    def random
    ...
    end
    end
    User.random

    View full-size slide

  105. SINGLETON
    METHODS

    View full-size slide

  106. SINGLETON METHOD
    obj = Person.new
    class << obj
    def represent
    ...
    end
    end
    obj.represent

    View full-size slide

  107. (A LOT MORE TO
    EXPLORE...)

    View full-size slide

  108. ONE LAST ADVICE

    View full-size slide

  109. SHU
    कഁ཭

    View full-size slide

  110. EMBRACE
    DYNAMIC TYPING

    View full-size slide

  111. BE MORE
    DECLARATIVE

    View full-size slide

  112. WRITE LESS DO
    MORE

    View full-size slide

  113. CONVENTIONS AND
    IDIOMS ARE NOT LAWS

    View full-size slide

  114. THERE IS NO TRUE
    PATH

    View full-size slide

  115. ENJOY RUBY!
    (AND JAVA TOO)

    View full-size slide

  116. DANKESCHÖN!

    View full-size slide

  117. ¡MUCHAS
    GRACIAS!

    View full-size slide

  118. IMAGES
    • http://006.shanbara.jp/movie/data/fat1367385194099.jpg
    • http://wfiles.brothersoft.com/n/nirvana-desktop_157301-1600x1200.jpg
    • http://stilestili.com/wp-content/uploads/2013/04/Ryan-
    Gosling2-2048x2560.jpg
    • http://upload.wikimedia.org/wikipedia/commons/thumb/1/14/
    James_Gosling_2008.jpg/1018px-James_Gosling_2008.jpg
    • http://www.flickr.com/photos/john_lam/1910968816/

    View full-size slide