Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Elements of Style in Ruby (RubyConf.lt)

The Elements of Style in Ruby (RubyConf.lt)

Slide deck from my talk at RubyConf.lt 2015

Bozhidar Batsov

March 28, 2015
Tweet

More Decks by Bozhidar Batsov

Other Decks in Programming

Transcript

  1. size=2 start=0 c=['Н', 'Н' ,'Н' ,'Н' ,'Н','Н','Н','Н','Н','Н','Н','Н','Н','Н'] code_dictionary=Array.new() begin_size_code_dictionary=Array.new() end_string=String.new()

    code_dictionary=c.uniq.to_a decode_dictionary=c.uniq.to_a for i in 0...code_dictionary.size begin_size_code_dictionary[i]=i end c.size.times{ if !code_dictionary.include?(c[start,size].to_s)#Comparation code_dictionary.push(c[start,size].to_s.chomp) start=start+(code_dictionary.last.size)/2-1 size=2 else size+=1; end } end_code=Array.new(code_dictionary.size) for i in begin_size_code_dictionary.size...code_dictionary.size end_code[i]=code_dictionary.index(code_dictionary[i].chop.chop)+1 end_string+=code_dictionary[i].chop.chop end govnokod.ru
  2. Style guide • curated set of (supposedly) good practices •

    frees you from making trivial decisions • allows you to focus on the real problems • short and to the point
  3. Programs must be written for people to read, and only

    incidentally for machines to execute. — Hal Abelson
  4. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do

    eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Chapter I
  5. LOREM IPSUM DOLOR SIT AMET, CONSECTETUR ADIPISCING ELIT, SED DO

    EIUSMOD TEMPOR INCIDIDUNT UT LABORE ET DOLORE MAGNA ALIQUA. UT ENIM AD MINIM VENIAM, QUIS NOSTRUD EXERCITATION ULLAMCO LABORIS NISI UT ALIQUIP EX EA COMMODO CONSEQUAT. DUIS AUTE IRURE DOLOR IN REPREHENDERIT IN VOLUPTATE VELIT ESSE CILLUM DOLORE EU FUGIAT NULLA PARIATUR. EXCEPTEUR SINT OCCAECAT CUPIDATAT NON PROIDENT, SUNT IN CULPA QUI OFFICIA DESERUNT MOLLIT ANIM ID EST LABORUM. LOREM IPSUM DOLOR SIT AMET, CONSECTETUR ADIPISCING ELIT, SED DO EIUSMOD TEMPOR INCIDIDUNT UT LABORE ET DOLORE MAGNA ALIQUA. UT ENIM AD MINIM VENIAM, QUIS NOSTRUD EXERCITATION ULLAMCO LABORIS NISI UT ALIQUIP EX EA COMMODO CONSEQUAT. DUIS AUTE IRURE DOLOR IN REPREHENDERIT IN VOLUPTATE VELIT ESSE CILLUM DOLORE EU FUGIAT NULLA PARIATUR. EXCEPTEUR SINT OCCAECAT CUPIDATAT NON PROIDENT, SUNT IN CULPA QUI OFFICIA DESERUNT MOLLIT ANIM ID EST LABORUM. chapter i
  6. Vilniaus miesto apylinkės teismas antradienį paskelbė, kad pripažįsta negaliojančiu Leono

    Virginijaus Papirčio išrinkimą Lietuvos advokatų tarybos nariu. Su skundu į teismą buvo kreipęsi keturi advokatai – Rūta Čilinskaitė, Jurgita Jurkšaitytė, Gintaris Trakšelis ir Ramūnas Kazlauskas. Jie teigė, kad buvęs Advokatų tarybos pirmininkas L.V.Papirtis neteisėtai vėl kandidatavo į šias pareigas ir neteisėtai buvo išrinktas į Advokatų tarybą, nes įstatai numato dviejų kadencijų ribojimą. Skyrius 1
  7. VILNIAUS MIESTO APYLINKėS TEISMAS ANTRADIENį PASKELBė, KAD PRIPAžįSTA NEGALIOJANčIU LEONO

    VIRGINIJAUS PAPIRčIO IšRINKIMą LIETUVOS ADVOKATų TARYBOS NARIU. sKyIiUS 1 SU SKUNDU į TEISMą BUVO KREIPęSI KETURI ADVOKATAI – RūTA ČILINSKAITė, JURGITA JURKšAITYTė, GINTARIS TRAKšELIS IR RAMūNAS KAZLAUSKAS. JIE TEIGė, KAD BUVęS ADVOKATų TARYBOS PIRMININKAS L.V.PAPIRTIS NETEISėTAI VėL KANDIDATAVO į šIAS PAREIGAS IR NETEISėTAI BUVO IšRINKTAS į ADVOKATų TARYBą, NES įSTATAI NUMATO DVIEJų KADENCIJų RIBOJIMą.
  8. sum = 1 + 2 a, b = 1, 2

    [1, 2, 3].each { |e| puts e } class FooError < StandardError; end
  9. CPU

  10. VU

  11. $:

  12. $;

  13. $*

  14. i = 1 res = 0 while i <= 10

    res += i i += 1 end
  15. • Make it right before you make it faster. •

    Make it fail-safe before you make it faster. • Make it clear before you make it faster.
  16. # This method does bla bla bla. # It returns

    an array of results. def bla_bla_bla res = Set.new # ... res end
  17. The Rules • Clear and concise • Illustrated with example

    code • Linkable • Neatly organised • Community-approved
  18. Style rules that only reflect what is easily discoverable by

    a trivial examination of the source code seem to me to be the least valuable rules. If the style is not conveying additional information, why bother? — Jim Weirich
  19. International • Chinese Simplified • Chinese Traditional • French •

    Japanese • Portuguese • Russian • Spanish • Vietnamese • Korean
  20. def some_method begin x = 5 h={:a=>1,:b=>2} if not h

    raise "Some error occurred" end [1, 2, 3].select { |e| e.even? } rescue => e end end
  21. def some_method x = 5 h = { a: 1,

    b: 2 } unless h fail 'Some error occurred' end [1, 2, 3].select(&:even?) rescue => e end