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

5 Random Ruby Tips

5 Random Ruby Tips

June 2nd @ RubySG Meetup

Juanito Fatas

June 02, 2015
Tweet

More Decks by Juanito Fatas

Other Decks in Technology

Transcript

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

    at all >= 2.2.2 => warning: circular argument reference - foo
  2. > 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
  3. 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
  4. 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
  5. $PODBUFOBUJPO "foo" + "bar" "foo".concat "bar" "foo" << "bar" "foo"

    "bar" JuanitoFatas/fast-ruby#string-concatenation-code ✈️
  6. -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!"
  7. -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!"
  8. 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