Skip to content

Instantly share code, notes, and snippets.

@hanachin
Last active May 11, 2020 15:34
Show Gist options
  • Select an option

  • Save hanachin/52625cdcdcca6608bfb2b46af6672b2b to your computer and use it in GitHub Desktop.

Select an option

Save hanachin/52625cdcdcca6608bfb2b46af6672b2b to your computer and use it in GitHub Desktop.

Revisions

  1. hanachin revised this gist May 11, 2020. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion enumerator_pipeline.rb
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,6 @@ def respond_to_missing?(*); true; end
    end
    }


    def plus_one(x)
    x + 1
    end
  2. hanachin created this gist May 11, 2020.
    31 changes: 31 additions & 0 deletions enumerator_pipeline.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    require 'binding_of_caller'

    using Module.new {
    refine(Array) do
    def map(&block)
    caller = binding.of_caller(1)

    cleanroom = Class.new(BasicObject) do
    def respond_to?(*); true; end
    def respond_to_missing?(*); true; end
    define_method(:method_missing) do |name,*|
    caller.method(name)
    end
    end

    super(&cleanroom.new.instance_eval(&block))
    end
    end
    }


    def plus_one(x)
    x + 1
    end

    def double(x)
    x * 2
    end

    pp [1, 2, 3, 4].map { plus_one >> double }
    #=> [4, 6, 8, 10]