5 Random Ruby Tips

5 Random Ruby Tips

June 2nd @ RubySG Meetup

771951f55ed37335f238e1a80dfda9cd?s=128

Juanito Fatas

June 02, 2015
Tweet

Transcript

  1. 3VCZ5JQT 5

  2. ,WCPKVQ(CVCU !

  3. @JuanitoFatas

  4. @JuanitoFatas

  5. @JuanitoFatas

  6. @JuanitoFatas

  7. 0GY9CTPKPI UKPEG4WD[ 1

  8. $JSDVMBS"SHVNFOU3FGFSFODF&SSPS def foo(foo=foo) foo end < 2.2.2 => No problems

    at all >= 2.2.2 => warning: circular argument reference - foo
  9. $JSDVMBS"SHVNFOU3FGFSFODF&SSPS def foo(foo=foo()) foo end https://bugs.ruby-lang.org/issues/10314 Fixed

  10. 0GY9CTPKPI UKPEG4WD[ 2 4GI'ZR

  11. -JUFSBM /http:\/\/ruby.sg/ %r{http://ruby.sg}

  12. Character Properties http://ruby-doc.org/core-2.2.2/Regexp.html#class-Regexp-label-Character+Properties

  13. \p{} $POTUSVDU

  14. \p{Letter} -FUUFS a, b, c

  15. \p{Mark} .BSL bon appétit qué pasa Köln

  16. \p{Number} /VNCFS 1, 2, 3

  17. \p{Punctuation} 1VODUVBUJPO , ; .

  18. \p{Word} 8PSE Letter, Mark, Number, Connector_Punctuation

  19. \p{Symbol} 4ZNCPM + = |

  20. \p{Han} .BUDI$IJOFTF if your application goes to China

  21. \p{Hiragana,Katakana} .BUDI+BQBOFTF

  22. \p{Hangul} .BUDI,PSFBO

  23. 3 'PWOGTCDNG

  24. users.sort { |a, b| a.balance <=> b.balance } users.sort_by(&:balance) TPSUWTTPSU@CZ

  25. > Benchmark.ips do |x| x.report(" sort") { users.sort { |a,

    b| a.balance <=> b.balance } } x.report("sort_by") { users.sort_by(&:balance) } x.compare! end
  26. Calculating ------------------------------------- sort 1.158k i/100ms sort_by 2.401k i/100ms ------------------------------------------------- sort

    12.140k (± 4.9%) i/s - 61.374k sort_by 24.169k (± 4.0%) i/s - 122.451k Comparison: sort_by: 24168.9 i/s sort: 12139.8 i/s - 1.99x slower
  27. users.sort_by(&:balance).take 3 NJO@CZWTTPSU@CZ users.min_by 3, &:balance R uby 2.2

  28. > Benchmark.ips do |x| x.report("sort_by") { users.sort_by(&:balance).take 3 } x.report("

    min_by") { users.min_by 3, &:balance } x.compare! end
  29. Calculating ------------------------------------- sort_by 12.000 i/100ms min_by 44.000 i/100ms ------------------------------------------------- sort_by

    126.314 (± 4.0%) i/s - 636.000 min_by 462.227 (± 5.2%) i/s - 2.332k Comparison: min_by: 462.2 i/s sort_by: 126.3 i/s - 3.66x slower
  30. 4 5VTKPI

  31. $PODBUFOBUJPO "foo" + "bar" "foo".concat "bar" "foo" << "bar" "foo"

    "bar" JuanitoFatas/fast-ruby#string-concatenation-code ✈️
  32. -POH4USJOH-JUFSBMT "How kind of you to let me come!" +

    "Now once again, where does it rain?" + "On the plain! On the plain!" + "And where's that blasted plain? " + "In Spain! In Spain! The three " + "The rain in Spain stays mainly in the plain! " + "The rain in Spain stays mainly in the plain!"
  33. -POH4USJOH-JUFSBMT "How kind of you to let me come!" \

    "Now once again, where does it rain?" \ "On the plain! On the plain!" \ "And where's that blasted plain? " \ "In Spain! In Spain! The three " \ "The rain in Spain stays mainly in the plain! " \ "The rain in Spain stays mainly in the plain!"
  34. Calculating ------------------------------------- String#+ 37.856k i/100ms String#backslash 144.338k i/100ms ------------------------------------------------- String#+

    480.091k (± 4.7%) i/s - 2.423M String#backslash 5.608M (± 8.4%) i/s - 27.857M Comparison: String#backslash: 5608274.5 i/s String#+: 480091.3 i/s - 11.68x slower
  35. (KPFKPI4WD[6KRU 5

  36. 3VCZ4UEMJC%PDT http://ruby-doc.org/stdlib-2.2.2/

  37. http://thepugautomatic.com http://idiosyncratic-ruby.com http://www.rubytapas.com http://parley.rubyrogues.com

  38. JuanitoFatas/fast-ruby JuanitoFatas/ruby-tips