Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Ruby for Java minds
Search
Jano González
August 14, 2013
Programming
4
1k
Ruby for Java minds
My talk in JRubyConf EU 2013
Jano González
August 14, 2013
Tweet
Share
More Decks by Jano González
See All by Jano González
Containerizing your monolith
janogonzalez
0
300
Migrando a Microservicios
janogonzalez
1
270
Extracting services from a monolith
janogonzalez
3
240
¿Después de 10 años, realmente entiendo esta industria?
janogonzalez
3
420
Microservices in Practice
janogonzalez
7
620
Two programmers in one
janogonzalez
1
140
The Bipolar Programmer
janogonzalez
4
570
Ruby for your two internal programmers
janogonzalez
4
220
How Ruby Programmed Me
janogonzalez
11
480
Other Decks in Programming
See All in Programming
The rollercoaster of releasing an Android, iOS, and macOS app with Kotlin Multiplatform | droidcon Italy
prof18
0
150
htmxって知っていますか?次世代のHTML
hiro_ghap1
0
330
競技プログラミングへのお誘い@阪大BOOSTセミナー
kotamanegi
0
350
CSC305 Lecture 26
javiergs
PRO
0
140
バグを見つけた?それAppleに直してもらおう!
uetyo
0
170
Monixと常駐プログラムの勘どころ / Scalaわいわい勉強会 #4
stoneream
0
270
暇に任せてProxmoxコンソール 作ってみました
karugamo
1
720
【re:Growth 2024】 Aurora DSQL をちゃんと話します!
maroon1st
0
770
rails statsで大解剖 🔍 “B/43流” のRailsの育て方を歴史とともに振り返ります
shoheimitani
2
930
プロダクトの品質に コミットする / Commit to Product Quality
pekepek
2
770
DevFest Tokyo 2025 - Flutter のアプリアーキテクチャ現在地点
wasabeef
5
900
コンテナをたくさん詰め込んだシステムとランタイムの変化
makihiro
1
120
Featured
See All Featured
GraphQLとの向き合い方2022年版
quramy
44
13k
Agile that works and the tools we love
rasmusluckow
328
21k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
Art, The Web, and Tiny UX
lynnandtonic
298
20k
Rails Girls Zürich Keynote
gr2m
94
13k
Adopting Sorbet at Scale
ufuk
73
9.1k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.5k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
2
290
Building a Scalable Design System with Sketch
lauravandoore
460
33k
Transcript
RUBY FOR JAVA MINDS
$ whoami
@janogonzalez
@hop_in
HISTORY
THE 90’S
BIG CHANGES
None
COLD WAR IS OVER
None
GRUNGE GOES MAINSTREAM
APATHY
SELF- ALIENATION
ANGST IS THE NEW COOL
MEANWHILE IN THE PROGRAMMING WORLD...
RAISING AGAINST THE MAINSTREAM
JAMES GOSLING
JAMES GOSLING
JAVA
“Write Once, Run Anywhere”
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"
(THAT WAS NOT RSPEC)
MATZ
MATZ
RUBY
“Ruby is designed to make programmers happy”
MATZ WINS THE NOBEL PEACE PRICE
(HISTORICAL ACCURACY IS OVERRATED)
2 DIFFERENT PHILOSOPHIES
HOW DO WE EMBRACE RUBY?
MY HISTORY
Y2K
COMPUTER APOCALYPSE DID NOT HAPPEN
I WENT TO A “JAVA SCHOOL”
JAVA EE, SPRING AND CUBICLES
HEY, RAILS LOOK COOL!
HEY, THERE IS A FLY OVER THERE!
(STUPID FLY)
THE 10’S
RAILS VS JAVA EE WAS OVER
HEY, RUBY LOOKS COOL!
WAIT, HOW DO I DECLARE AN INTERFACE?
None
THE PROBLEM
“The determined Real Programmer can write FORTRAN programs in any
language.” –Ed Post
HOW TO WRITE RUBY THAT FEELS LIKE RUBY?
A ZEN MOMENT
ॳ৺
BEGINNER’S MIND
EMPTY YOUR CUP
BACK TO BASICS
THE KEY TO UNDERSTAND RUBY
EXPRESSIONS + OBJECTS
SYNTAX IS EXPRESSION-ORIENTED
“LISP programmers know the value of everything and the cost
of nothing.” - Alan Perlis
EXPRESSIONS def average(a, b) (a + b) / 2 end
# => nil average(10, 2) # => 6
EXPRESSIONS level = case when 1..2 then :excellent when 3..5
then :normal else :bad end
ALL VALUES ARE OBJECTS
OBJECTS -1.class # => Fixnum
OBJECTS String.object_id # => 70138854779960
OBJECTS nil.nil? # => true
WO IST DEIN GOTT JETZT
MESSAGES
MESSAGES EVERYWHERE
MESSAGES 40 + 2 # => 42
MESSAGES 40.+(2) # => 42
MESSAGES 40.send(:+, 2) # => 42
MESSAGES ≠ METHODS
MESSAGES class AccountProtectionProxy def initialize(real_account, owner) @subject = real_account @owner
= owner end ...
MESSAGES class AccountProtectionProxy ... def method_missing(name, *args) check_access @subject.send(name, *args)
end ... end
DYNAMIC TYPING
FORGET ABOUT JAVA INTERFACES
JAVA public interface Report { ReportFile generate(); }
JAVA public class PDFReport implements Report { .... }
JAVA public class CSVReport implements Report { .... }
JAVA public void print(List<Report> reports) { if (reports == null)
{ return; } for (Report r : reports) { ReportFile file = r.generate(); ... } }
DYNAMIC TYPING def print(reports) [*reports].each do |r| file = r.generate
... end end
DYNAMIC TYPING class PDFReport def generate ... end ... end
DYNAMIC TYPING class CVSReport def generate ... end ... end
DYNAMIC TYPING class Duck def generate ... end ... end
PROGRAM TO INTERFACES, NOT TO interface
USED EVERYWHERE
DYNAMIC TYPING "Jano in " << "Berlin" # => "Jano
in Berlin" ["Santiago"] << "Berlin" # => ["Santiago", "Berlin"]
IF YOU REALLY NEED TO CHECK...
DYNAMIC TYPING if report.respond_to?(:generate) report.generate ... else ... end
IF YOU REALLY, REALLY NEED TO CHECK...
DYNAMIC TYPING if report.is_a?(Report) report.generate ... else ... end
DYNAMIC BUT NOT WEAK
STRONG TYPING 100 + 'cool' # TypeError: String can't be
coerced into Fixnum
TYPE CONVERSIONS
CONVERSIONS 100 + 'cool'.to_i # => 100 100 + Integer('cool')
# ArgumentError: invalid value for Integer(): "lala"
BLOCKS
BLOCKS ARE USED & ABUSED IN RUBY
PROGRAM LIKE A BOSS
(LITERALLY)
BLOCKS (1..100).select { |n| n % 3 == 0 }
.map { |n| n * 2 } .reduce(:+)
FLUID CODE
BLOCKS File.open('my.txt').each do |line| puts line if line =~ /jano/
end
BE MORE DECLARATIVE
BLOCKS words.sort do |a, b| a.length <=> b.length end words.min_by?
{ |w| w.length } words.reject { |w| w.length > 8 } # etc...
EXAMPLE: SELF YIELD
BLOCKS class Job def initialize yield self if block_given? end
... end
BLOCKS job = Job.new do |j| j.name = 'Print Reports'
j.user = 'janogonzalez' ... end
EXAMPLE: CALLBACKS
BLOCKS class Job def on_finish(&block) @end_callback = block end def
execute() ... @end_callback.call(self) if @end_callback end end
BLOCKS job.on_finish do |j| puts "Finished job #{j.name}" puts "Status:
#{j.status}” end
MIXINS
MODULES AS MIXINS
ADD BEHAVIOR TO INSTANCES
MODULES module Model def persist ... end end
MODULES class User include Model ... end
MODULES job = User.new('Jano') job.persist
ADD BEHAVIOR TO CLASSES
MODULES module Findable def find_by_id(id) ... end end
MODULES class User extends Findable ... end
MODULES user = User.find_by_id(3)
METAPROGRAMMING
(USE WITH CAUTION)
(AKA THE EVAL IS EVIL RULE)
ACCESSORS
ACCESSORS class User attr_reader :name attr_reader :status end
OPEN CLASSES
OPEN CLASSES class Range def even select(&:even?) end end (0..10).even
# => [2, 4, 6, 8, 10]
OPEN CLASSES class BuggyClass def buggy_method # the fix ...
end end
OPEN CLASSES class Job alias_method :old_execute, :execute def execute ...
old_execute end end
DEFINE METHODS
DEFINE METHODS obj = User.new User.class_eval do def represent ...
end end obj.represent
DEFINE METHODS User.instance_eval do def random ... end end User.random
SINGLETON METHODS
SINGLETON METHOD obj = Person.new class << obj def represent
... end end obj.represent
(A LOT MORE TO EXPLORE...)
ONE LAST ADVICE
कഁ
SHU कഁ
HA कഁ
RI कഁ
None
CONCLUSIONS
EMBRACE DYNAMIC TYPING
BE MORE DECLARATIVE
WRITE LESS DO MORE
CONVENTIONS AND IDIOMS ARE NOT LAWS
THERE IS NO TRUE PATH
ENJOY RUBY! (AND JAVA TOO)
DANKESCHÖN!
¡MUCHAS GRACIAS!
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/