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 Slide

  2. $ whoami

    View Slide

  3. @janogonzalez

    View Slide

  4. @hop_in

    View Slide

  5. HISTORY

    View Slide

  6. THE 90’S

    View Slide

  7. BIG
    CHANGES

    View Slide

  8. View Slide

  9. COLD WAR
    IS OVER

    View Slide

  10. View Slide

  11. GRUNGE GOES
    MAINSTREAM

    View Slide

  12. APATHY

    View Slide

  13. SELF-
    ALIENATION

    View Slide

  14. ANGST IS THE NEW
    COOL

    View Slide

  15. MEANWHILE IN THE
    PROGRAMMING
    WORLD...

    View Slide

  16. RAISING AGAINST
    THE MAINSTREAM

    View Slide

  17. JAMES GOSLING

    View Slide

  18. JAMES GOSLING

    View Slide

  19. JAVA

    View Slide

  20. “Write Once,
    Run Anywhere”

    View Slide

  21. 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 Slide

  22. (THAT WAS NOT RSPEC)

    View Slide

  23. MATZ

    View Slide

  24. MATZ

    View Slide

  25. RUBY

    View Slide

  26. “Ruby is designed to make
    programmers happy”

    View Slide

  27. MATZ WINS THE
    NOBEL PEACE PRICE

    View Slide

  28. (HISTORICAL ACCURACY
    IS OVERRATED)

    View Slide

  29. 2 DIFFERENT
    PHILOSOPHIES

    View Slide

  30. HOW DO WE
    EMBRACE RUBY?

    View Slide

  31. MY HISTORY

    View Slide

  32. Y2K

    View Slide

  33. COMPUTER APOCALYPSE
    DID NOT HAPPEN

    View Slide

  34. I WENT TO A “JAVA
    SCHOOL”

    View Slide

  35. JAVA EE, SPRING
    AND CUBICLES

    View Slide

  36. HEY, RAILS LOOK
    COOL!

    View Slide

  37. HEY, THERE IS A
    FLY OVER THERE!

    View Slide

  38. (STUPID FLY)

    View Slide

  39. THE 10’S

    View Slide

  40. RAILS VS JAVA EE
    WAS OVER

    View Slide

  41. HEY, RUBY LOOKS
    COOL!

    View Slide

  42. WAIT, HOW DO I
    DECLARE AN INTERFACE?

    View Slide

  43. View Slide

  44. THE PROBLEM

    View Slide

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

    View Slide

  46. HOW TO WRITE RUBY
    THAT FEELS LIKE RUBY?

    View Slide

  47. A ZEN MOMENT

    View Slide

  48. ॳ৺

    View Slide

  49. BEGINNER’S
    MIND

    View Slide

  50. EMPTY YOUR CUP

    View Slide

  51. BACK TO BASICS

    View Slide

  52. THE KEY TO
    UNDERSTAND RUBY

    View Slide

  53. EXPRESSIONS
    +
    OBJECTS

    View Slide

  54. SYNTAX IS
    EXPRESSION-ORIENTED

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  58. ALL VALUES ARE
    OBJECTS

    View Slide

  59. OBJECTS
    -1.class
    # => Fixnum

    View Slide

  60. OBJECTS
    String.object_id
    # => 70138854779960

    View Slide

  61. OBJECTS
    nil.nil?
    # => true

    View Slide

  62. WO IST DEIN GOTT JETZT

    View Slide

  63. MESSAGES

    View Slide

  64. MESSAGES
    EVERYWHERE

    View Slide

  65. MESSAGES
    40 + 2
    # => 42

    View Slide

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

    View Slide

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

    View Slide

  68. MESSAGES

    METHODS

    View Slide

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

    View Slide

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

    View Slide

  71. DYNAMIC TYPING

    View Slide

  72. FORGET ABOUT
    JAVA INTERFACES

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  81. PROGRAM TO
    INTERFACES, NOT
    TO interface

    View Slide

  82. USED
    EVERYWHERE

    View Slide

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

    View Slide

  84. IF YOU REALLY
    NEED TO CHECK...

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  88. DYNAMIC BUT
    NOT WEAK

    View Slide

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

    View Slide

  90. TYPE
    CONVERSIONS

    View Slide

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

    View Slide

  92. BLOCKS

    View Slide

  93. BLOCKS ARE USED
    & ABUSED IN RUBY

    View Slide

  94. PROGRAM LIKE A
    BOSS

    View Slide

  95. (LITERALLY)

    View Slide

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

    View Slide

  97. FLUID CODE

    View Slide

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

    View Slide

  99. BE MORE
    DECLARATIVE

    View Slide

  100. 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 Slide

  101. EXAMPLE: SELF
    YIELD

    View Slide

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

    View Slide

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

    View Slide

  104. EXAMPLE:
    CALLBACKS

    View Slide

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

    View Slide

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

    View Slide

  107. MIXINS

    View Slide

  108. MODULES AS
    MIXINS

    View Slide

  109. ADD BEHAVIOR TO
    INSTANCES

    View Slide

  110. MODULES
    module Model
    def persist
    ...
    end
    end

    View Slide

  111. MODULES
    class User
    include Model
    ...
    end

    View Slide

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

    View Slide

  113. ADD BEHAVIOR TO
    CLASSES

    View Slide

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

    View Slide

  115. MODULES
    class User
    extends Findable
    ...
    end

    View Slide

  116. MODULES
    user = User.find_by_id(3)

    View Slide

  117. METAPROGRAMMING

    View Slide

  118. (USE WITH CAUTION)

    View Slide

  119. (AKA THE EVAL IS EVIL
    RULE)

    View Slide

  120. ACCESSORS

    View Slide

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

    View Slide

  122. OPEN CLASSES

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  126. DEFINE METHODS

    View Slide

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

    View Slide

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

    View Slide

  129. SINGLETON
    METHODS

    View Slide

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

    View Slide

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

    View Slide

  132. ONE LAST ADVICE

    View Slide

  133. कഁ཭

    View Slide

  134. SHU
    कഁ཭

    View Slide

  135. HA
    कഁ཭

    View Slide

  136. RI
    कഁ཭

    View Slide

  137. View Slide

  138. CONCLUSIONS

    View Slide

  139. EMBRACE
    DYNAMIC TYPING

    View Slide

  140. BE MORE
    DECLARATIVE

    View Slide

  141. WRITE LESS DO
    MORE

    View Slide

  142. CONVENTIONS AND
    IDIOMS ARE NOT LAWS

    View Slide

  143. THERE IS NO TRUE
    PATH

    View Slide

  144. ENJOY RUBY!
    (AND JAVA TOO)

    View Slide

  145. DANKESCHÖN!

    View Slide

  146. ¡MUCHAS
    GRACIAS!

    View Slide

  147. 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 Slide