This shows how different approaches to memoization work (or don't) in different Ruby engines.
If you're using the idiomatic Ruby approach to memoization, like this:
def data
@memo ||= expensive_action
end
| $ history 1 | awk {'print $2, $3, $4'} | sort | uniq -c | sort -k1 -rn | head -n 30 | |
| 172 fg | |
| 164 git st | |
| 163 ls | |
| 72 ruby create_event_local.rb blah | |
| 62 git push | |
| 59 git pull | |
| 54 git commit -m | |
| 50 git di | |
| 47 bundle |
| # Summary: In MRI 1.9, if you keep threads in an array in a local variable, the threads may outlive the local variable. | |
| # I have not yet been able to produce a scenario in which the threads are ever garbage-collected. | |
| # You have to remove the thread from the array before it goes out of scope, e.g. using #pop or #clear. | |
| # INPUT DESIRED: If anyone can demonstrate a condition in which a local array keeps threads and those threads are eventually GC'd. | |
| # INPUT DESIRED: Is this expected behavior or a bug? | |
| # NOTE: This test will not work as expected in JRuby, because its garbage collection works differently. Whether the same behavior exists in JRuby is an exercise for someone smarter than I am. | |
| class Foo | |
| THREAD_PROC = lambda{ (0..10).to_a.map do Foo.new end } |
| # http://blog.jazzychad.net/2012/08/01/array-iteration-problem.html | |
| def add1(arr, val, n) | |
| range = (0 ... arr.length) | |
| indexes = n < 0 ? range.reverse_each : range.each | |
| n = n.zero? ? arr.length : n.abs | |
| indexes.each do |idx| | |
| if arr[idx] == val | |
| arr[idx] += 1 |
| class Object | |
| private :dup | |
| private :clone | |
| private :freeze | |
| end | |
| module ValueObject | |
| def self.included(into) | |
| into.send :public, :dup | |
| into.send :public, :clone |
| #!/bin/sh | |
| # backdroppify - sizes an image to the minimum size which will fill your entire screen & trims overflow | |
| bgsize=`xwininfo -root | grep -- -geometry | cut -d' ' -f4 | cut -d+ -f1` | |
| convert $1 -resize $bgsize^ -gravity center -extent $bgsize ~/.backdrop.jpg | |
| set-backdrop |
| require 'test/unit/testcase' | |
| require 'test/unit' if $0 == __FILE__ | |
| class Proc | |
| def as_proc | |
| if arity == -1 | |
| self | |
| else | |
| aty = arity.abs - (arity < 0 ? 1 : 0) |
| require 'celluloid' | |
| require 'zip/zipfilesystem' | |
| require 'uri' | |
| require 'net/http' | |
| class ZipManifest | |
| include Celluloid | |
| def initialize(filename) | |
| @urls = {} |
| class Orz | |
| attr_reader :v | |
| def []=(v) | |
| @v = v | |
| end | |
| end | |
| orz = Orz.new | |
| orz[] = :a | |
| orz.v # ==> :a |
| # NOTE: I just slapped this together as a proof of concept. Thread safety etc. is left to the reader. | |
| module RTDocumentation | |
| def self.included(into) | |
| # method_added doesn't seem to work if mixed in | |
| into.send(:define_method, :method_added) do |name| | |
| if @_rtdoc_nextdoc | |
| @_rtdocs ||= {} | |
| @_rtdocs[name.to_s] = @_rtdoc_nextdoc |