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
290
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
130
The Bipolar Programmer
janogonzalez
4
570
Ruby for your two internal programmers
janogonzalez
4
210
How Ruby Programmed Me
janogonzalez
11
470
Other Decks in Programming
See All in Programming
Quine, Polyglot, 良いコード
qnighy
4
650
Realtime API 入門
riofujimon
0
150
TypeScriptでライブラリとの依存を限定的にする方法
tutinoko
3
700
AI時代におけるSRE、 あるいはエンジニアの生存戦略
pyama86
6
1.2k
What’s New in Compose Multiplatform - A Live Tour (droidcon London 2024)
zsmb
1
480
as(型アサーション)を書く前にできること
marokanatani
10
2.7k
NSOutlineView何もわからん:( 前編 / I Don't Understand About NSOutlineView :( Pt. 1
usagimaru
0
340
2024/11/8 関西Kaggler会 2024 #3 / Kaggle Kernel で Gemma 2 × vLLM を動かす。
kohecchi
5
940
Arm移行タイムアタック
qnighy
0
340
アジャイルを支えるテストアーキテクチャ設計/Test Architecting for Agile
goyoki
9
3.3k
EMになってからチームの成果を最大化するために取り組んだこと/ Maximize team performance as EM
nashiusagi
0
100
macOS でできる リアルタイム動画像処理
biacco42
9
2.4k
Featured
See All Featured
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
8.2k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
Optimising Largest Contentful Paint
csswizardry
33
2.9k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
28
2k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
126
18k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Navigating Team Friction
lara
183
14k
Documentation Writing (for coders)
carmenintech
65
4.4k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
329
21k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.8k
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/