Ad
  • Custom User Avatar

    Here's the bench mark for both methods.

    require "benchmark"
    
    n = 1_000_000
    ARR = %w(today is a good day isn't it?)
    Benchmark.bmbm do |bm|
      bm.report { n.times { ARR.map(&:capitalize) } }
      bm.report { n.times { ARR.map { |i| i.capitalize } } }
    end
    
    Rehearsal ------------------------------------
       1.890000   0.000000   1.890000 (  1.893212)
       1.930000   0.010000   1.940000 (  1.928549)
    --------------------------- total: 3.830000sec
    
           user     system      total        real
       1.900000   0.000000   1.900000 (  1.910439)
       2.050000   0.000000   2.050000 (  2.050394)
    
  • Custom User Avatar

    The main thing this is good for is being concise. I never believed it was performant since Ruby has special optimization for blocks.

  • Custom User Avatar

    It's not 'clever', it's perfectly readable to any Ruby developer. You could argue that so many things in ruby are not readable to 'the average developer'.
    Passing just the method is more common in many other languages, be it Python, Scheme, Haskell, Dart, Smalltalk or even C.

    As for performance, don't use Ruby, or optimize inner loops. For most Enumerable#map operations the actual operation will cost way more than iterating in whatever way.