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
「とりあえず動く」コードはよい、「読みやすい」コードはもっとよい / Code that 'just works' is good, but code that is 'readable' is even better.
mkmk884
3
610
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
170
php-conference-japan-2024
tasuku43
0
350
これが俺の”自分戦略” プロセスを楽しんでいこう! - Developers CAREER Boost 2024
niftycorp
PRO
0
190
これでLambdaが不要に?!Step FunctionsのJSONata対応について
iwatatomoya
2
3.7k
Итераторы в Go 1.23: зачем они нужны, как использовать, и насколько они быстрые?
lamodatech
0
880
Jakarta EE meets AI
ivargrimstad
0
260
Androidアプリのモジュール分割における:x:commonを考える
okuzawats
1
160
今年のアップデートで振り返るCDKセキュリティのシフトレフト/2024-cdk-security-shift-left
tomoki10
0
210
From Translations to Multi Dimension Entities
alexanderschranz
2
140
Fibonacci Function Gallery - Part 1
philipschwarz
PRO
0
220
情報漏洩させないための設計
kubotak
3
470
Featured
See All Featured
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
Making the Leap to Tech Lead
cromwellryan
133
9k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
How to Ace a Technical Interview
jacobian
276
23k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
A Tale of Four Properties
chriscoyier
157
23k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Building Applications with DynamoDB
mza
91
6.1k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
Rails Girls Zürich Keynote
gr2m
94
13k
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/