background preloader

Ruby

Facebook Twitter

Mame/radiation-hardened-quine. Why do singleton methods make Ruby slow? — Charlie Somerville. Did you know that defining singleton methods on an object can make common operations (eg. arithmetic, hash/array indexing, method calls like empty? , size, etc) significantly slower? If you don't believe me, run this script on your machine and take note of the somewhat surprising results: require "benchmark"GC.disable eval "def run; 10_000.times { #{"$a[5]\n" * 10_000} } end" $a = [1,2,3,4,5,6,7,8,9,10] puts "before singleton method definition"puts Benchmark.measure { run } def $a.x; end puts "after singleton method definition"puts Benchmark.measure { run } On my machine, I get the following timings: before singleton method definition 2.620000 0.000000 2.620000 ( 2.622593) after singleton method definition 5.820000 0.000000 5.820000 ( 5.826397) So what gives?

The Ruby VM MRI Ruby 1.9 and up uses a bytecode virtual machine internally. For example, take this simple looking line of Ruby: puts "Hello world".upcase Let's compile it and dump the generated bytecode: Speeding up common method calls. Getting to Know the Ruby Standard Library – TSort - Monkey and Crow. Getting to Know the Ruby Standard Library – TSort TSort is an interesting part of the ruby standard library that performs topological sorting.

Topological sorting is used in package management, analyzing source code, and evaluating prerequisites. RubyGems uses TSort to determine what order to install gems in when there are multiple dependencies. Let’s take a look at TSort in action. Here is an example of a JobRunner which will take a bunch of tasks with prerequisites, and then tell you which order they should be performed in: Running this file will show that you need to buy the eggs and bacon before you can cook and then serve breakfast. buy eggs buy bacon cook serve breakfast Hardly an achievement of complex reasoning, but as the number of prerequisites grow, this becomes vastly more useful. TSort Let’s take a look at the source (qw tsort if you have qwandry). Def tsort result = [] tsort_each {|element| result << element} result end if node_id == minimum_id component = stack.slice!

Recap. Rewrite. Ruby 2.0.0 in Detail. Following on from our preview of the new features in Ruby 2.0.0, that version has now been officially released, so here’s a thorough and detailed run-down of all the tweaks, changes and additions that have made it in to the release. Keyword arguments def wrap(string, before: "<", after: ">") "#{before}#{string}#{after}" # no need to retrieve options from a hashend # optionalwrap("foo") #=> "<foo>"# one or the otherwrap("foo", before: "#<") #=> "#<foo>"wrap("foo", after: "]") #=> "<foo]"# order not importantwrap("foo", after: "]", before: "[") #=> "[foo]" One of the nice things about this compared to hash-as-fake-keyword-args is that it errors when you make a mistake. begin wrap("foo", befoer: "#<")rescue ArgumentError => e e.message #=> "unknown keyword: befoer"end You can use double splat to capture all keyword arguments, just like a single splat to capture all regular arguments.

You can also use the double splat to unpack a hash to keyword arguments. %i and %I symbol array literal outputs: Banister/gen_eval. Index of Files, Classes & Methods in Ruby 2.0.0 (Ruby 2.0.