SlideShare una empresa de Scribd logo
1 de 55
Let’s Refactor Some Ruby Code
Ana María Martínez Gómez
http://anamaria.martinezgomez.name | anamma06@gmail.com | @anamma_06
Ruby and I are turning 25
https://build.opensuse.or
g
https://github.com/openSUSE/open-build-
service
OBS = Open Build Service
Let’s Refactor Some Ruby Code
Scary comment
src/api/lib/activexml/node.rb
src/api/lib/activexml/node.rb
src/api/lib/activexml/node.rb
https://github.com/openSUSE/open-build-
service/pull/3947
src/api/lib/activexml/node.r
b
comments are a bad ideacomments are a bad idea
nodocnodoc
protectedprotected
Reusing code
piece_of_code_to_reuse (param1, param2)
piece_of_code_to_reuse
piece_of_code_to_reuse
"Param1_1",
params1_2
"Param2_1",
params2_2
def
# code to be reused which uses two
parameters
code to be reused which uses two
parametersend
piece_of_code_to_reuse (param1, param2)
piece_of_code_to_reuse
piece_of_code_to_reuse
"Param1_1",
params1_2
"Param2_1",
params2_2
def
# code to be reused which uses two
parametersend
{ "Param1_1" => params1_2, "Params2_1" =>
params2_2 }
param1 =
key.to_s# code to be reused which uses two
parametersend
.each do |key,
param2|
param1 =
key.capitalize.to_s
{ param1_1: params1_2, params2_1: params2_2 }.each do |key,
param2|
{ Param1_1: params1_2, Params2_1: params2_2 }.each do |key,
param2|
https://github.com/openSUSE/open-build-
service/pull/4105
src/api/app/models/kiwi/image.rb
https://github.com/openSUSE/open-build-
service/pull/4119
src/api/app/models/kiwi/image.rb
Logical conditions
src/api/app/models/flag.rb
ONLY ONE OF THEM IS TRUE
https://github.com/openSUSE/open-build-
service/pull/2324
src/api/app/models/flag.rb
concat + uniq
https://github.com/openSUSE/open-build-
service/pull/4104
src/api/lib/backend/api/build_results/binaries.rb
https://github.com/openSUSE/open-build-
service/pull/4104
src/api/lib/backend/api/build_results/binaries.rb
https://github.com/openSUSE/open-build-
service/pull/4104
src/api/lib/backend/api/build_results/binaries.rb
https://github.com/openSUSE/open-build-
service/pull/4104
src/api/lib/backend/api/build_results/binaries.rb
a + ba + b
a.concat ba.concat b
a + b + ca + b + c
a.concat b, ca.concat b, c
a | ba | b
a.union ba.union b
a | b | ca | b | c
a.union b, ca.union b, c
https://bugs.ruby-
lang.org/issues/14097
StandStand
up!up!
array1.concat(array2,
array3).uniq!
array1 = (array1 | array2 |
array3)
array1.union(array2, array
3)
11
22
33
https://bugs.ruby-
lang.org/issues/14097
https://github.com/ruby/ruby/pull/1747
Yak
ShavingAny apparently useless activity which, by allowing you to overcome
intermediate difficulties, allows you to solve a larger problem
Refactor
OBS
horrible
code
Add new
method to Ruby
Improve
Ruby Array
efficiency
https://github.com/ruby/ruby/pull/1756
Ruby is using SVNRuby is using SVN
We work with peopleWe work with people
Every team works differentlyEvery team works differently
I need to be more patientI need to be more patient
It is a
reasonable
proposal
https://bugs.ruby-lang.org/issues/14097https://bugs.ruby-lang.org/issues/14097
Rails helpers
https://github.com/openSUSE/open-build-
service/pull/5567
["Executive", "Developer", "Consultant", "Strategist", "Agent", "Producer", "Executive", "Officer", "Agent", "Director", "Orchestrator",
"Administrator", "Officer", "Strategist", "Analyst", "Officer", "Liaison", "Designer", "Producer", "Orchestrator", "Director", "Representative",
"Facilitator", "Strategist", "Representative", "Agent", "Orchestrator", "Associate", "Agent", "Coordinator", "Developer", "Strategist", "Technician",
"Consultant", "Facilitator", "Planner", "Executive", "Strategist", "Facilitator", "Specialist", "Strategist", "Producer", "Executive", "Agent", "Planner",
"Specialist", "Orchestrator", "Orchestrator", "Producer", "Agent"]
[["Executive", "executive"], ["Developer", "developer"], ["Consultant", "consultant"], ["Strategist", "strategist"], ["Agent", "agent"], ["Producer",
"producer"], ["Executive", "executive"], ["Officer", "officer"], ["Agent", "agent"], ["Director", "director"], ["Orchestrator", "orchestrator"],
["Administrator", "administrator"], ["Officer", "officer"], ["Strategist", "strategist"], ["Analyst", "analyst"], ["Officer", "officer"], ["Liaison", "liaison"],
["Designer", "designer"], ["Producer", "producer"], ["Orchestrator", "orchestrator"], ["Director", "director"], ["Representative", "representative"],
["Facilitator", "facilitator"], ["Strategist", "strategist"], ["Representative", "representative"], ["Agent", "agent"], ["Orchestrator", "orchestrator"],
["Associate", "associate"], ["Agent", "agent"], ["Coordinator", "coordinator"], ["Developer", "developer"], ["Strategist", "strategist"], ["Technician",
"technician"], ["Consultant", "consultant"], ["Facilitator", "facilitator"], ["Planner", "planner"], ["Executive", "executive"], ["Strategist", "strategist"],
["Facilitator", "facilitator"], ["Specialist", "specialist"], ["Strategist", "strategist"], ["Producer", "producer"], ["Executive", "executive"], ["Agent",
"agent"], ["Planner", "planner"], ["Specialist", "specialist"], ["Orchestrator", "orchestrator"], ["Orchestrator", "orchestrator"], ["Producer", "producer"],
["Agent", "agent"]]
Can it be improved?Can it be improved?
https://github.com/openSUSE/open-build-
service/pull/5567
- improved code- improved code
- options_from_collection_for_select- options_from_collection_for_select
- itself- itself
- but that’s not all...- but that’s not all...
https://github.com/rails/rails/issues/3354
6
Rails helpers areRails helpers are
exposing privateexposing private
methods!methods!
https://github.com/rails/rails/pull/33547
GETGET
INVOLVED!INVOLVED!
Thanks!
http://anamaria.martinezgomez.name | anamma06@gmail.com | @anamma_06

Más contenido relacionado

Similar a Let's refactor some Ruby code - EuRuKo 2018

Rails Presentation (Anton Dmitriyev)
Rails Presentation (Anton Dmitriyev)Rails Presentation (Anton Dmitriyev)
Rails Presentation (Anton Dmitriyev)
True-Vision
 
The liferay case: lessons learned evolving from RPC to Hypermedia REST APIs
The liferay case: lessons learned evolving from RPC to Hypermedia REST APIsThe liferay case: lessons learned evolving from RPC to Hypermedia REST APIs
The liferay case: lessons learned evolving from RPC to Hypermedia REST APIs
Jorge Ferrer
 
Instruments ruby on rails
Instruments ruby on railsInstruments ruby on rails
Instruments ruby on rails
pmashchak
 

Similar a Let's refactor some Ruby code - EuRuKo 2018 (20)

Merb For The Enterprise
Merb For The EnterpriseMerb For The Enterprise
Merb For The Enterprise
 
Ruby on Rails Presentation
Ruby on Rails PresentationRuby on Rails Presentation
Ruby on Rails Presentation
 
Rails Presentation (Anton Dmitriyev)
Rails Presentation (Anton Dmitriyev)Rails Presentation (Anton Dmitriyev)
Rails Presentation (Anton Dmitriyev)
 
The liferay case: lessons learned evolving from RPC to Hypermedia REST APIs
The liferay case: lessons learned evolving from RPC to Hypermedia REST APIsThe liferay case: lessons learned evolving from RPC to Hypermedia REST APIs
The liferay case: lessons learned evolving from RPC to Hypermedia REST APIs
 
Rspec and Capybara Intro Tutorial at RailsConf 2013
Rspec and Capybara Intro Tutorial at RailsConf 2013Rspec and Capybara Intro Tutorial at RailsConf 2013
Rspec and Capybara Intro Tutorial at RailsConf 2013
 
Ruby On Rails Introduction
Ruby On Rails IntroductionRuby On Rails Introduction
Ruby On Rails Introduction
 
Ams adapters
Ams adaptersAms adapters
Ams adapters
 
All Day DevOps 2023 - Implementing OSSF Scorecards Across an Organisation.pdf
All Day DevOps 2023 - Implementing OSSF Scorecards Across an Organisation.pdfAll Day DevOps 2023 - Implementing OSSF Scorecards Across an Organisation.pdf
All Day DevOps 2023 - Implementing OSSF Scorecards Across an Organisation.pdf
 
Toolbox of a Ruby Team
Toolbox of a Ruby TeamToolbox of a Ruby Team
Toolbox of a Ruby Team
 
Aspect-oriented programming in Perl
Aspect-oriented programming in PerlAspect-oriented programming in Perl
Aspect-oriented programming in Perl
 
The Future of Dependency Management for Ruby
The Future of Dependency Management for RubyThe Future of Dependency Management for Ruby
The Future of Dependency Management for Ruby
 
Introduction to Rails - presented by Arman Ortega
Introduction to Rails - presented by Arman OrtegaIntroduction to Rails - presented by Arman Ortega
Introduction to Rails - presented by Arman Ortega
 
The story of language development
The story of language developmentThe story of language development
The story of language development
 
Crossing the Bridge: Connecting Rails and your Front-end Framework
Crossing the Bridge: Connecting Rails and your Front-end FrameworkCrossing the Bridge: Connecting Rails and your Front-end Framework
Crossing the Bridge: Connecting Rails and your Front-end Framework
 
Instruments ruby on rails
Instruments ruby on railsInstruments ruby on rails
Instruments ruby on rails
 
Efektivni vyvoj webovych aplikaci v Ruby on Rails (Webexpo)
Efektivni vyvoj webovych aplikaci v Ruby on Rails (Webexpo)Efektivni vyvoj webovych aplikaci v Ruby on Rails (Webexpo)
Efektivni vyvoj webovych aplikaci v Ruby on Rails (Webexpo)
 
Php resque
Php resquePhp resque
Php resque
 
Great APIs - Future of Your Progress App
Great APIs - Future of Your Progress AppGreat APIs - Future of Your Progress App
Great APIs - Future of Your Progress App
 
Elasticsearch intro output
Elasticsearch intro outputElasticsearch intro output
Elasticsearch intro output
 
Origins of Serverless
Origins of ServerlessOrigins of Serverless
Origins of Serverless
 

Último

%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 

Último (20)

%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 

Let's refactor some Ruby code - EuRuKo 2018

Notas del editor

  1. 1 month before first Rails release (december 2005). 13 years ago
  2. The Open Build Service (OBS) is a system to build and distribute binary packages from sources
  3. wrote in 2011
  4. Nodoc: public methods not designed for user consumption – don’t want users to rely on this method
  5. Less than one year - october
  6. - We don’t want that both are true - We want that at least one is true exclusive or is known as xor in mathematics, hardware and many other programming languages
  7. We add an error except if only one of them is true
  8. Bitwise OR
  9. Same for the difference