SlideShare une entreprise Scribd logo
1  sur  36
Télécharger pour lire hors ligne
RUBY (AND RAILS)
Jan Berdajs	

@mrbrdo
RUBY
•

Japan	


•

Yukihiro Matsumoto a.k.a. Matz	


•

24. Feb. 1993	


•

Matz is nice and so we are nice
(MINASWAN)
RAILS
•

Denmark	


•

David Heinemeier Hansson a.k.a
DHH	


•

BaseCamp / 37 signals	


•

July 2004	


•

“The best frameworks are in my
opinion extracted, not envisioned.
And the best way to extract is first
to actually do.”
STUFF ON RAILS
ECOSYSTEM
RubyGems
PACKAGES
ASP.NET NuGet:	

PHP	

Pear:	

Packagist/Composer:	

Python PyPI:	

Node.JS NPM:	

Ruby RubyGems:

17,770	

!

595	

21,754	

38,607	

53,740	

68,500

!

Honorable mention:	

Java, Scala: Maven etc, too many to count
INTERACTIVE CONSOLE
MANY WAYS TO DO IT
n = 15!
!
(1..n).each do |i|!
n_zvezdic = (i - 1) * 2 + 1!
n_presledkov = n - i!
!
(1..n_presledkov).each do!
putc " "!
end!
!
(1..n_zvezdic).each do!
putc "*"!
end!
!
putc "n"!
end!
!
MANY WAYS TO DO IT
n = 15!
!
(1..n).each do |i|!
n_zvezdic = (i - 1) * 2 + 1!
n_presledkov = n - i!
!
n_presledkov.times do!
print " "!
end!
!
n_zvezdic.times do!
print "*"!
end!
!
print "n"!
end!
!
MANY WAYS TO DO IT
n = 15!
!
(1..n).each do |i|!
n_zvezdic = (i - 1) * 2 + 1!
n_presledkov = n - i!
!
print " " * n_presledkov!
!
!
!
print "*" * n_zvezdic!
!
!
!
print "n"!
end!
!
MANY WAYS TO DO IT
n = 15!
!
(1..n).each do |i|!
n_zvezdic = (i - 1) * 2 + 1!
n_presledkov = n - i!
!
puts " " * n_presledkov + "*" * n_zvezdic!
!
!
!
!
!
!
!
!
end!
!
MANY WAYS TO DO IT
n = 15!
!
n.times do |i|!
n_zvezdic = i * 2 + 1!
n_presledkov = n - i - 1!
!
puts " " * n_presledkov + "*" * n_zvezdic!
end!
!
!
!
!
!
!
!
!
!
MANY WAYS TO DO IT
n = 15!
!
n.times{|i| puts " "*(n-i-1) + "*"*(i*2+1)}!
!
!
!
!
!
!
!
!
!
MANY WAYS TO DO IT
n = 15!
!
(1..n).each do |i|!
n_zvezdic = (i - 1) * 2 + 1!
n_presledkov = n - i!
!
(1..n_presledkov).each do!
putc " "!
end!
!
(1..n_zvezdic).each do!
putc "*"!
end!
!
putc "n"!
end!
!
MANY WAYS TO DO IT
n = 15!
!
n.times{|i| puts " "*(n-i-1) + "*"*(i*2+1)}!
!
!
!
!
!
!
!
!
!
MANY WAYS TO DO IT
n = 15!
!
n.times{|i| puts " "*(n-i-1) + "*"*(i*2+1)}!
!
Java:!
class Test { public static void main(String args[]) {} }!
!
!
!
!
!
!
!
MANY WAYS TO DO IT
n = 15!
!
n.times{|i| puts " "*(n-i-1) + "*"*(i*2+1)}!
!
!
!
!
!
!
!
!
!
JUST COOL
Date.today.thursday? # => true!
!
10.seconds.ago # => 2014-01-09 09:15:10 +0100!
!
10.even? # => true!
!
102.megabytes + 24.kilobytes + 10.bytes # => 106,979,338!
!
10 + 1 # => 11!
!
class Fixnum!
def +(i)!
42!
end!
end!
!
10 + 1 # => 42!
JUST COOL
def alive?!
state != :dead!
end!

!

def clear!!
everything.remove!
end!

!

def setting=(value)!
raise "invalid value" unless value == 42!
end!
obj.setting = 5!

!

def [](key)!
key + 1!
end!
obj[1] # => 2!

!

def []=(key, value)!
whatevz!
end!
obj[1] = 2!
BEST PRACTICES

Short methods, self-commenting code	

+ readability	

+ testing	

!

You only need comments when you know your code	

is written so bad that people won’t understand it.
def clients!
User.where(user_type: "client")!
end!

!

def days_until_next_week(date)!
8 - date.cwday!
end!

!

def next_week_start(after_date)!
after_date + days_until_next_week(after_date).days!
end!
def payments_next_week!
payments = []!

!

User.where(user_type: "client").each do |user|!
next_week_start = Date.today + (8 Date.today.cwday).days!
next_week_end = next_week_start + 7.days!
payments = user.payments.where("due_on >= ? AND
due_on < 1.week.from_now", next_week_start,
next_week_end)!
payments.each do |payment|!
next if payment.due_on.saturday? ||
payment.due_on.sunday?!
payments << payment!
end!
end!
end!

!

def week_end(week_start)!
week_start + 7.days!
end!

!

def client_payments_between(client, range)!
client.payments!
.where("due_on >= ?", range.first)!
.where("due_on < ?", range.last)!
end!

!

def client_payments_next_week(client)!
start_day = next_week_start(Date.today)!
client_payments_between(client,!
start_day..week_end(start_day))!
end!

!

def payment_on_weekend?(payment)!
payment.due_on.saturday? || payment.due_on.sunday?!
end!

!

def payments_next_week!
clients.flat_map do |client|!
client_payments_next_week(client).reject do |payment|!
payment_on_weekend?(payment)!
end!
end!
end!
BEST PRACTICES
Testing
!
!

!
!

describe "#decline!" do!
subject { create :booking }!
context "without reason" do!
before { subject.decline! }!
its(:status) { should == "declined" }!
its(:declined?) { should be_true }!
end!
context "with reason" do!
before { subject.decline!("REASON!") }!
its(:status) { should == "declined" }!
its(:declined?) { should be_true }!
its(:status_reason) { should == "REASON!" }!
end!
end!
BEST PRACTICES

TEST FIRST => great object interfaces/APIs
Write how you want to use it, before you implement it.
BEST PRACTICES
Don’t repeat yourself!
Extract duplicate logic
# user.rb!

!

# file1.rb!

!
User.where(user_type:
!
# file2.rb!
!

"client").first!

User.where(user_type: "client", active: false).first!

class User!
def self.client!
where(user_type: "client")!
end!
end!

!

# file1.rb!

!

User.client.first!

!

# file2.rb!

!

User.client.where(active: false).first!
BEST PRACTICES
Don’t repeat yourself!
Extract duplicate logic
# user.rb!

!

class User!
def self.client!
where(user_type: "client")!
end!
end!

# file1.rb!

!
User.where(user_type:
!
# file2.rb!
!

"client").first!

!

# file1.rb!

User.where(user_type: "client", active: false).first!

!

User.client.first!

!

# file2.rb!

2 places to fix

1 place to fix

!

User.client.where(active: false).first!

+ easier to test
BEST PRACTICES

Convention over configuration
Sensible defaults
MY STUFF
MY JOB STUFF @ D-LABS
QUESTIONS

Contenu connexe

Similaire à Why use Ruby and Rails?

Esoteric, Obfuscated, Artistic Programming in Ruby
Esoteric, Obfuscated, Artistic Programming in RubyEsoteric, Obfuscated, Artistic Programming in Ruby
Esoteric, Obfuscated, Artistic Programming in Rubymametter
 
Pilot Tech Talk #9 — Ember.js: Productivity without the fatigue by Jacek Gala...
Pilot Tech Talk #9 — Ember.js: Productivity without the fatigue by Jacek Gala...Pilot Tech Talk #9 — Ember.js: Productivity without the fatigue by Jacek Gala...
Pilot Tech Talk #9 — Ember.js: Productivity without the fatigue by Jacek Gala...Pilot
 
Python Performance Profiling: The Guts And The Glory
Python Performance Profiling: The Guts And The GloryPython Performance Profiling: The Guts And The Glory
Python Performance Profiling: The Guts And The Gloryemptysquare
 
Hadoop Streaming Tutorial With Python
Hadoop Streaming Tutorial With PythonHadoop Streaming Tutorial With Python
Hadoop Streaming Tutorial With PythonJoe Stein
 
Ruby on Rails - Pengenalan kepada “permata” dalam pengaturcaraan
Ruby on Rails - Pengenalan kepada “permata” dalam pengaturcaraan  Ruby on Rails - Pengenalan kepada “permata” dalam pengaturcaraan
Ruby on Rails - Pengenalan kepada “permata” dalam pengaturcaraan edthix
 
An Introduction to Go
An Introduction to GoAn Introduction to Go
An Introduction to GoCloudflare
 
Front End Dependency Management at CascadiaJS
Front End Dependency Management at CascadiaJSFront End Dependency Management at CascadiaJS
Front End Dependency Management at CascadiaJSJoe Sepi
 
Fluent Refactoring (Lone Star Ruby Conf 2013)
Fluent Refactoring (Lone Star Ruby Conf 2013)Fluent Refactoring (Lone Star Ruby Conf 2013)
Fluent Refactoring (Lone Star Ruby Conf 2013)Sam Livingston-Gray
 
Impossible Programs
Impossible ProgramsImpossible Programs
Impossible ProgramsC4Media
 
Blocks by Lachs Cox
Blocks by Lachs CoxBlocks by Lachs Cox
Blocks by Lachs Coxlachie
 
Migrating To Ruby1.9
Migrating To Ruby1.9Migrating To Ruby1.9
Migrating To Ruby1.9tomaspavelka
 
An Event Apart Boston: Principles of Unobtrusive JavaScript
An Event Apart Boston: Principles of Unobtrusive JavaScriptAn Event Apart Boston: Principles of Unobtrusive JavaScript
An Event Apart Boston: Principles of Unobtrusive JavaScriptPeter-Paul Koch
 
Ruby - Uma Introdução
Ruby - Uma IntroduçãoRuby - Uma Introdução
Ruby - Uma IntroduçãoÍgor Bonadio
 
Class 12 CBSE Computer Science Investigatory Project
Class 12 CBSE Computer Science Investigatory ProjectClass 12 CBSE Computer Science Investigatory Project
Class 12 CBSE Computer Science Investigatory ProjectNandanRamesh2
 
Tour of language landscape (katsconf)
Tour of language landscape (katsconf)Tour of language landscape (katsconf)
Tour of language landscape (katsconf)Yan Cui
 
Test First Teaching
Test First TeachingTest First Teaching
Test First TeachingSarah Allen
 

Similaire à Why use Ruby and Rails? (20)

Esoteric, Obfuscated, Artistic Programming in Ruby
Esoteric, Obfuscated, Artistic Programming in RubyEsoteric, Obfuscated, Artistic Programming in Ruby
Esoteric, Obfuscated, Artistic Programming in Ruby
 
Pilot Tech Talk #9 — Ember.js: Productivity without the fatigue by Jacek Gala...
Pilot Tech Talk #9 — Ember.js: Productivity without the fatigue by Jacek Gala...Pilot Tech Talk #9 — Ember.js: Productivity without the fatigue by Jacek Gala...
Pilot Tech Talk #9 — Ember.js: Productivity without the fatigue by Jacek Gala...
 
Python Performance Profiling: The Guts And The Glory
Python Performance Profiling: The Guts And The GloryPython Performance Profiling: The Guts And The Glory
Python Performance Profiling: The Guts And The Glory
 
Hadoop Streaming Tutorial With Python
Hadoop Streaming Tutorial With PythonHadoop Streaming Tutorial With Python
Hadoop Streaming Tutorial With Python
 
An introduction to Ruby
An introduction to RubyAn introduction to Ruby
An introduction to Ruby
 
Ruby on Rails - Pengenalan kepada “permata” dalam pengaturcaraan
Ruby on Rails - Pengenalan kepada “permata” dalam pengaturcaraan  Ruby on Rails - Pengenalan kepada “permata” dalam pengaturcaraan
Ruby on Rails - Pengenalan kepada “permata” dalam pengaturcaraan
 
An Introduction to Go
An Introduction to GoAn Introduction to Go
An Introduction to Go
 
Front End Dependency Management at CascadiaJS
Front End Dependency Management at CascadiaJSFront End Dependency Management at CascadiaJS
Front End Dependency Management at CascadiaJS
 
Fluent Refactoring (Lone Star Ruby Conf 2013)
Fluent Refactoring (Lone Star Ruby Conf 2013)Fluent Refactoring (Lone Star Ruby Conf 2013)
Fluent Refactoring (Lone Star Ruby Conf 2013)
 
Impossible Programs
Impossible ProgramsImpossible Programs
Impossible Programs
 
Blocks by Lachs Cox
Blocks by Lachs CoxBlocks by Lachs Cox
Blocks by Lachs Cox
 
Migrating To Ruby1.9
Migrating To Ruby1.9Migrating To Ruby1.9
Migrating To Ruby1.9
 
An Event Apart Boston: Principles of Unobtrusive JavaScript
An Event Apart Boston: Principles of Unobtrusive JavaScriptAn Event Apart Boston: Principles of Unobtrusive JavaScript
An Event Apart Boston: Principles of Unobtrusive JavaScript
 
Ruby - Uma Introdução
Ruby - Uma IntroduçãoRuby - Uma Introdução
Ruby - Uma Introdução
 
Class 12 CBSE Computer Science Investigatory Project
Class 12 CBSE Computer Science Investigatory ProjectClass 12 CBSE Computer Science Investigatory Project
Class 12 CBSE Computer Science Investigatory Project
 
Automate Yo' Self
Automate Yo' SelfAutomate Yo' Self
Automate Yo' Self
 
Tour of language landscape (katsconf)
Tour of language landscape (katsconf)Tour of language landscape (katsconf)
Tour of language landscape (katsconf)
 
Test First Teaching
Test First TeachingTest First Teaching
Test First Teaching
 
go.ppt
go.pptgo.ppt
go.ppt
 
Perfect Code
Perfect CodePerfect Code
Perfect Code
 

Dernier

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 

Dernier (20)

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 

Why use Ruby and Rails?

  • 1. RUBY (AND RAILS) Jan Berdajs @mrbrdo
  • 2. RUBY • Japan • Yukihiro Matsumoto a.k.a. Matz • 24. Feb. 1993 • Matz is nice and so we are nice (MINASWAN)
  • 3. RAILS • Denmark • David Heinemeier Hansson a.k.a DHH • BaseCamp / 37 signals • July 2004 • “The best frameworks are in my opinion extracted, not envisioned. And the best way to extract is first to actually do.”
  • 6. PACKAGES ASP.NET NuGet: PHP Pear: Packagist/Composer: Python PyPI: Node.JS NPM: Ruby RubyGems: 17,770 ! 595 21,754 38,607 53,740 68,500 ! Honorable mention: Java, Scala: Maven etc, too many to count
  • 8. MANY WAYS TO DO IT n = 15! ! (1..n).each do |i|! n_zvezdic = (i - 1) * 2 + 1! n_presledkov = n - i! ! (1..n_presledkov).each do! putc " "! end! ! (1..n_zvezdic).each do! putc "*"! end! ! putc "n"! end! !
  • 9. MANY WAYS TO DO IT n = 15! ! (1..n).each do |i|! n_zvezdic = (i - 1) * 2 + 1! n_presledkov = n - i! ! n_presledkov.times do! print " "! end! ! n_zvezdic.times do! print "*"! end! ! print "n"! end! !
  • 10. MANY WAYS TO DO IT n = 15! ! (1..n).each do |i|! n_zvezdic = (i - 1) * 2 + 1! n_presledkov = n - i! ! print " " * n_presledkov! ! ! ! print "*" * n_zvezdic! ! ! ! print "n"! end! !
  • 11. MANY WAYS TO DO IT n = 15! ! (1..n).each do |i|! n_zvezdic = (i - 1) * 2 + 1! n_presledkov = n - i! ! puts " " * n_presledkov + "*" * n_zvezdic! ! ! ! ! ! ! ! ! end! !
  • 12. MANY WAYS TO DO IT n = 15! ! n.times do |i|! n_zvezdic = i * 2 + 1! n_presledkov = n - i - 1! ! puts " " * n_presledkov + "*" * n_zvezdic! end! ! ! ! ! ! ! ! ! !
  • 13. MANY WAYS TO DO IT n = 15! ! n.times{|i| puts " "*(n-i-1) + "*"*(i*2+1)}! ! ! ! ! ! ! ! ! !
  • 14. MANY WAYS TO DO IT n = 15! ! (1..n).each do |i|! n_zvezdic = (i - 1) * 2 + 1! n_presledkov = n - i! ! (1..n_presledkov).each do! putc " "! end! ! (1..n_zvezdic).each do! putc "*"! end! ! putc "n"! end! !
  • 15. MANY WAYS TO DO IT n = 15! ! n.times{|i| puts " "*(n-i-1) + "*"*(i*2+1)}! ! ! ! ! ! ! ! ! !
  • 16. MANY WAYS TO DO IT n = 15! ! n.times{|i| puts " "*(n-i-1) + "*"*(i*2+1)}! ! Java:! class Test { public static void main(String args[]) {} }! ! ! ! ! ! ! !
  • 17. MANY WAYS TO DO IT n = 15! ! n.times{|i| puts " "*(n-i-1) + "*"*(i*2+1)}! ! ! ! ! ! ! ! ! !
  • 18. JUST COOL Date.today.thursday? # => true! ! 10.seconds.ago # => 2014-01-09 09:15:10 +0100! ! 10.even? # => true! ! 102.megabytes + 24.kilobytes + 10.bytes # => 106,979,338! ! 10 + 1 # => 11! ! class Fixnum! def +(i)! 42! end! end! ! 10 + 1 # => 42!
  • 19. JUST COOL def alive?! state != :dead! end! ! def clear!! everything.remove! end! ! def setting=(value)! raise "invalid value" unless value == 42! end! obj.setting = 5! ! def [](key)! key + 1! end! obj[1] # => 2! ! def []=(key, value)! whatevz! end! obj[1] = 2!
  • 20. BEST PRACTICES Short methods, self-commenting code + readability + testing ! You only need comments when you know your code is written so bad that people won’t understand it.
  • 21. def clients! User.where(user_type: "client")! end! ! def days_until_next_week(date)! 8 - date.cwday! end! ! def next_week_start(after_date)! after_date + days_until_next_week(after_date).days! end! def payments_next_week! payments = []! ! User.where(user_type: "client").each do |user|! next_week_start = Date.today + (8 Date.today.cwday).days! next_week_end = next_week_start + 7.days! payments = user.payments.where("due_on >= ? AND due_on < 1.week.from_now", next_week_start, next_week_end)! payments.each do |payment|! next if payment.due_on.saturday? || payment.due_on.sunday?! payments << payment! end! end! end! ! def week_end(week_start)! week_start + 7.days! end! ! def client_payments_between(client, range)! client.payments! .where("due_on >= ?", range.first)! .where("due_on < ?", range.last)! end! ! def client_payments_next_week(client)! start_day = next_week_start(Date.today)! client_payments_between(client,! start_day..week_end(start_day))! end! ! def payment_on_weekend?(payment)! payment.due_on.saturday? || payment.due_on.sunday?! end! ! def payments_next_week! clients.flat_map do |client|! client_payments_next_week(client).reject do |payment|! payment_on_weekend?(payment)! end! end! end!
  • 22. BEST PRACTICES Testing ! ! ! ! describe "#decline!" do! subject { create :booking }! context "without reason" do! before { subject.decline! }! its(:status) { should == "declined" }! its(:declined?) { should be_true }! end! context "with reason" do! before { subject.decline!("REASON!") }! its(:status) { should == "declined" }! its(:declined?) { should be_true }! its(:status_reason) { should == "REASON!" }! end! end!
  • 23. BEST PRACTICES TEST FIRST => great object interfaces/APIs Write how you want to use it, before you implement it.
  • 24. BEST PRACTICES Don’t repeat yourself! Extract duplicate logic # user.rb! ! # file1.rb! ! User.where(user_type: ! # file2.rb! ! "client").first! User.where(user_type: "client", active: false).first! class User! def self.client! where(user_type: "client")! end! end! ! # file1.rb! ! User.client.first! ! # file2.rb! ! User.client.where(active: false).first!
  • 25. BEST PRACTICES Don’t repeat yourself! Extract duplicate logic # user.rb! ! class User! def self.client! where(user_type: "client")! end! end! # file1.rb! ! User.where(user_type: ! # file2.rb! ! "client").first! ! # file1.rb! User.where(user_type: "client", active: false).first! ! User.client.first! ! # file2.rb! 2 places to fix 1 place to fix ! User.client.where(active: false).first! + easier to test
  • 26. BEST PRACTICES Convention over configuration Sensible defaults
  • 28.
  • 29.
  • 30.
  • 31.
  • 32. MY JOB STUFF @ D-LABS
  • 33.
  • 34.
  • 35.