Skip to content

Instantly share code, notes, and snippets.

@chad
Forked from latompa/dict.rb
Created December 17, 2011 14:42
Show Gist options
  • Select an option

  • Save chad/1490380 to your computer and use it in GitHub Desktop.

Select an option

Save chad/1490380 to your computer and use it in GitHub Desktop.

Revisions

  1. Chad Fowler revised this gist Dec 17, 2011. 1 changed file with 3 additions and 8 deletions.
    11 changes: 3 additions & 8 deletions dict.rb
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,12 @@
    def dictionary
    lambda {|x| nil }
    end

    def add(key, value, dictionary)
    def add(key, value, dictionary = Proc.new{})
    lambda do |x|
    key == x ? value : dictionary.call(x)
    end
    end

    d = dictionary
    d = add("a",5, d)
    d = add("a",5)
    d = add("b",6, d)

    p d.call("b")
    p d.call("a")
    p d.call("c")
    p d.call("c")
  2. @latompa latompa created this gist Dec 17, 2011.
    17 changes: 17 additions & 0 deletions dict.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    def dictionary
    lambda {|x| nil }
    end

    def add(key, value, dictionary)
    lambda do |x|
    key == x ? value : dictionary.call(x)
    end
    end

    d = dictionary
    d = add("a",5, d)
    d = add("b",6, d)

    p d.call("b")
    p d.call("a")
    p d.call("c")