Hacker News new | ask | show | jobs
by dzsekijo 1430 days ago
What I enjoy about Ruby over Python the most is not intricacies of the OO implementation or delicacies of metaprogramming, but a good support for functional style. In ruby, statements are expression-like, they have a return value. Also, with Ruby it's easy to break down code to consecutive pure blocks.

Condiser the following task: take /proc/kallsyms in Linux, that lists symbols in kernel, in an '<address> <type> <name>' format, like:

  0000000000000000 A fixed_percpu_data
  0000000000000000 A __per_cpu_start
  0000000000001000 A cpu_debug_store
  0000000000002000 A irq_stack_backing_store
  0000000000006000 A cpu_tss_rw
  000000000000b000 A gdt_page
  ...
let's make stats on it -- how much of each type is present? Wanna get the result ordered by number of occurrence.

In Ruby:

  # ruby -rset -e '$<.readlines.to_set.classify { |l| l.split[1] }.transform_values(&:size).sort_by { |_,v| v }.to_h.tap { pp _1 }' /proc/kallsyms 
  {"V"=>1,
   "w"=>2,
   "a"=>14,
   "R"=>98,
   "W"=>154,
   "A"=>328,
   "B"=>655,
   "D"=>2910,
   "b"=>3097,
   "T"=>22289,
   "d"=>34424,
   "r"=>49904,
   "t"=>55346}
Of this method call chain, first and last are impure (as they do I/O), the intermediate ones are pure.

In Python you'll have to grind through it procedurally. (Unless you use reach out to some advanced libs... https://gist.github.com/richardbann/5b363096de6b3de2e8178cce...)