Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
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
470
Other Decks in Programming
See All in Programming
社内活動の取り組み紹介 ~ スリーシェイクでこんな取り組みしてます ~
bells17
0
390
テストコード文化を0から作り、変化し続けた組織
kazatohiei
2
990
Webエンジニア主体のモバイルチームの 生産性を高く保つためにやったこと
igreenwood
0
160
Amazon Aurora Serverless v2のアプデと、Amazon Aurora PostgreSQL Limitless DatabaseのGAについて
satoshi256kbyte
0
100
Jakarta EE meets AI
ivargrimstad
0
980
TypeScript でバックもやるって実際どう? 実運用で困ったこと3選
yuichiro_serita
17
7.6k
第5回日本眼科AI学会総会_AIコンテスト_3位解法
neilsaw
0
140
CSC305 Lecture 26
javiergs
PRO
0
130
Djangoの開発環境で工夫したこと - pre-commit / DevContainer
hiroki_yod
1
670
Symfony Mapper Component
soyuka
2
610
ブラウザ単体でmp4書き出すまで - muddy-web - 2024-12
yue4u
0
100
17年周年のWebアプリケーションにTanStack Queryを導入する / Implementing TanStack Query in a 17th Anniversary Web Application
saitolume
0
130
Featured
See All Featured
VelocityConf: Rendering Performance Case Studies
addyosmani
326
24k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
8.3k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.4k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
126
18k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Fashionably flexible responsive web design (full day workshop)
malarkey
405
65k
Side Projects
sachag
452
42k
Writing Fast Ruby
sferik
627
61k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
Fireside Chat
paigeccino
34
3.1k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
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/