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
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.4k
受け取る人から提供する人になるということ
little_rubyist
0
230
A Journey of Contribution and Collaboration in Open Source
ivargrimstad
0
880
GitHub Actionsのキャッシュと手を挙げることの大切さとそれに必要なこと
satoshi256kbyte
5
430
Outline View in SwiftUI
1024jp
1
320
よくできたテンプレート言語として TypeScript + JSX を利用する試み / Using TypeScript + JSX outside of Web Frontend #TSKaigiKansai
izumin5210
6
1.7k
Snowflake x dbtで作るセキュアでアジャイルなデータ基盤
tsoshiro
2
520
Jakarta EE meets AI
ivargrimstad
0
120
現場で役立つモデリング 超入門
masuda220
PRO
15
3.2k
subpath importsで始めるモック生活
10tera
0
300
【Kaigi on Rails 2024】YOUTRUST スポンサーLT
krpk1900
1
330
RubyLSPのマルチバイト文字対応
notfounds
0
120
Featured
See All Featured
Building Your Own Lightsaber
phodgson
103
6.1k
Optimizing for Happiness
mojombo
376
70k
Automating Front-end Workflow
addyosmani
1366
200k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
8.2k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
250
21k
Imperfection Machines: The Place of Print at Facebook
scottboms
265
13k
Keith and Marios Guide to Fast Websites
keithpitt
409
22k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
840
Designing on Purpose - Digital PM Summit 2013
jponch
115
7k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.3k
Scaling GitHub
holman
458
140k
Designing for Performance
lara
604
68k
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/