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

Ee64bbdcdeffabc6d4166a96e6d2a0d5?s=128

Jano González

August 14, 2013
Tweet

Transcript

  1. RUBY FOR JAVA MINDS

  2. $ whoami

  3. @janogonzalez

  4. @hop_in

  5. HISTORY

  6. THE 90’S

  7. BIG CHANGES

  8. None
  9. COLD WAR IS OVER

  10. None
  11. GRUNGE GOES MAINSTREAM

  12. APATHY

  13. SELF- ALIENATION

  14. ANGST IS THE NEW COOL

  15. MEANWHILE IN THE PROGRAMMING WORLD...

  16. RAISING AGAINST THE MAINSTREAM

  17. JAMES GOSLING

  18. JAMES GOSLING

  19. JAVA

  20. “Write Once, Run Anywhere”

  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"
  22. (THAT WAS NOT RSPEC)

  23. MATZ

  24. MATZ

  25. RUBY

  26. “Ruby is designed to make programmers happy”

  27. MATZ WINS THE NOBEL PEACE PRICE

  28. (HISTORICAL ACCURACY IS OVERRATED)

  29. 2 DIFFERENT PHILOSOPHIES

  30. HOW DO WE EMBRACE RUBY?

  31. MY HISTORY

  32. Y2K

  33. COMPUTER APOCALYPSE DID NOT HAPPEN

  34. I WENT TO A “JAVA SCHOOL”

  35. JAVA EE, SPRING AND CUBICLES

  36. HEY, RAILS LOOK COOL!

  37. HEY, THERE IS A FLY OVER THERE!

  38. (STUPID FLY)

  39. THE 10’S

  40. RAILS VS JAVA EE WAS OVER

  41. HEY, RUBY LOOKS COOL!

  42. WAIT, HOW DO I DECLARE AN INTERFACE?

  43. None
  44. THE PROBLEM

  45. “The determined Real Programmer can write FORTRAN programs in any

    language.” –Ed Post
  46. HOW TO WRITE RUBY THAT FEELS LIKE RUBY?

  47. A ZEN MOMENT

  48. ॳ৺

  49. BEGINNER’S MIND

  50. EMPTY YOUR CUP

  51. BACK TO BASICS

  52. THE KEY TO UNDERSTAND RUBY

  53. EXPRESSIONS + OBJECTS

  54. SYNTAX IS EXPRESSION-ORIENTED

  55. “LISP programmers know the value of everything and the cost

    of nothing.” - Alan Perlis
  56. EXPRESSIONS def average(a, b) (a + b) / 2 end

    # => nil average(10, 2) # => 6
  57. EXPRESSIONS level = case when 1..2 then :excellent when 3..5

    then :normal else :bad end
  58. ALL VALUES ARE OBJECTS

  59. OBJECTS -1.class # => Fixnum

  60. OBJECTS String.object_id # => 70138854779960

  61. OBJECTS nil.nil? # => true

  62. WO IST DEIN GOTT JETZT

  63. MESSAGES

  64. MESSAGES EVERYWHERE

  65. MESSAGES 40 + 2 # => 42

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

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

  68. MESSAGES ≠ METHODS

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

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

    end ... end
  71. DYNAMIC TYPING

  72. FORGET ABOUT JAVA INTERFACES

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

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

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

  76. JAVA public void print(List<Report> reports) { if (reports == null)

    { return; } for (Report r : reports) { ReportFile file = r.generate(); ... } }
  77. DYNAMIC TYPING def print(reports) [*reports].each do |r| file = r.generate

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

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

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

  81. PROGRAM TO INTERFACES, NOT TO interface

  82. USED EVERYWHERE

  83. DYNAMIC TYPING "Jano in " << "Berlin" # => "Jano

    in Berlin" ["Santiago"] << "Berlin" # => ["Santiago", "Berlin"]
  84. IF YOU REALLY NEED TO CHECK...

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

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

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

  88. DYNAMIC BUT NOT WEAK

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

    coerced into Fixnum
  90. TYPE CONVERSIONS

  91. CONVERSIONS 100 + 'cool'.to_i # => 100 100 + Integer('cool')

    # ArgumentError: invalid value for Integer(): "lala"
  92. BLOCKS

  93. BLOCKS ARE USED & ABUSED IN RUBY

  94. PROGRAM LIKE A BOSS

  95. (LITERALLY)

  96. BLOCKS (1..100).select { |n| n % 3 == 0 }

    .map { |n| n * 2 } .reduce(:+)
  97. FLUID CODE

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

    end
  99. BE MORE DECLARATIVE

  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...
  101. EXAMPLE: SELF YIELD

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

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

    j.user = 'janogonzalez' ... end
  104. EXAMPLE: CALLBACKS

  105. BLOCKS class Job def on_finish(&block) @end_callback = block end def

    execute() ... @end_callback.call(self) if @end_callback end end
  106. BLOCKS job.on_finish do |j| puts "Finished job #{j.name}" puts "Status:

    #{j.status}” end
  107. MIXINS

  108. MODULES AS MIXINS

  109. ADD BEHAVIOR TO INSTANCES

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

  111. MODULES class User include Model ... end

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

  113. ADD BEHAVIOR TO CLASSES

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

  115. MODULES class User extends Findable ... end

  116. MODULES user = User.find_by_id(3)

  117. METAPROGRAMMING

  118. (USE WITH CAUTION)

  119. (AKA THE EVAL IS EVIL RULE)

  120. ACCESSORS

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

  122. OPEN CLASSES

  123. OPEN CLASSES class Range def even select(&:even?) end end (0..10).even

    # => [2, 4, 6, 8, 10]
  124. OPEN CLASSES class BuggyClass def buggy_method # the fix ...

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

    old_execute end end
  126. DEFINE METHODS

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

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

  129. SINGLETON METHODS

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

    ... end end obj.represent
  131. (A LOT MORE TO EXPLORE...)

  132. ONE LAST ADVICE

  133. कഁ཭

  134. SHU कഁ཭

  135. HA कഁ཭

  136. RI कഁ཭

  137. None
  138. CONCLUSIONS

  139. EMBRACE DYNAMIC TYPING

  140. BE MORE DECLARATIVE

  141. WRITE LESS DO MORE

  142. CONVENTIONS AND IDIOMS ARE NOT LAWS

  143. THERE IS NO TRUE PATH

  144. ENJOY RUBY! (AND JAVA TOO)

  145. DANKESCHÖN!

  146. ¡MUCHAS GRACIAS!

  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/