Skip to content

Instantly share code, notes, and snippets.

@activefx
Created April 4, 2015 03:42
Show Gist options
  • Select an option

  • Save activefx/5460d9236ba0ba7f1800 to your computer and use it in GitHub Desktop.

Select an option

Save activefx/5460d9236ba0ba7f1800 to your computer and use it in GitHub Desktop.

Revisions

  1. activefx created this gist Apr 4, 2015.
    22 changes: 22 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    class Example
    def make_method(&block)
    line_no = __LINE__; method_defs = %{
    def dynamic_method
    #{block}.call(1)
    end
    }
    instance_eval method_defs, __FILE__, line_no
    end
    end

    example = Example.new

    example.make_method do |n|
    n + 1
    end

    # expect to be 2
    example.dynamic_method
    #=> nil