Giter VIP home page Giter VIP logo

evalmimic's Introduction

= evalmimic

* http://github.com/tario/evalmimic

== DESCRIPTION:

 Evalmimic allow the implementation of eval like methods, solves the common problem of binding retrieve: in ruby you can't obtain the binding of the caller, or if you can, is some tricky

== INSTALL:

sudo gem install evalmimic

== USAGE

Use define_eval_method to add a eval like method to the class or as global function:

  class X
    define_eval_method :my_eval
    ...
  end

Define a method named internal_eval to handle the calls

  class X
    define_eval_method :my_eval

    def internal_eval(binding_, args)
       # binding_ is the original binding of the caller of my_eval
       # args is an array of the arguments passed to my_eval
    end
  end

Also, it can be use outside of a class, in analog way:

  define_eval_method :my_eval

  def internal_eval(binding_, args)
     # binding_ is the original binding of the caller of my_eval
     # args is an array of the arguments passed to my_eval
  end

=== Example 1

Creating your own implementation of eval

  require "rubygems"
  require "evalmimic"

  define_eval_method :my_eval

  def internal_eval(b_, args)
    b_.eval("a+b")
  end

  def foo
    a = 4
    b = 5

    my_eval
  end

  print foo,"\n"

=== Example 2

Creating an Evaluator class

  require "rubygems"
  require "evalmimic"

  class MyEvaluator
    define_eval_method :eval

    def internal_eval(b_, args)
      b_.eval(args[0])
    end

  end

  def foo
    m = MyEvaluator.new

    a = 4
    b = 5

    m.eval "a+b"

  end

  print foo,"\n"

=== Example 3

Hooking/monkey patching of the original eval

  require "rubygems"
  require "evalmimic"

  alias original_eval eval
  define_eval_method :eval

  def internal_eval(b_, args)
    print "eval called with code = #{args[0].inspect}\n"

    args[1] = args[1] || b_

    original_eval(*args)
  end

  def foo
    a = 4
    eval "a"
  end

  print foo,"\n"

== LICENSE:

Copyright (c) 2011 Roberto Dario Seminara <[email protected]>

evalhook is free software: you can redistribute it and/or modify
it under the terms of the gnu general public license as published by
the free software foundation, either version 3 of the license, or
(at your option) any later version.

evalhook is distributed in the hope that it will be useful,
but without any warranty; without even the implied warranty of
merchantability or fitness for a particular purpose.  see the
gnu general public license for more details.

you should have received a copy of the gnu general public license
along with evalhook.  if not, see <http://www.gnu.org/licenses/>.

evalmimic's People

Contributors

tario avatar

Stargazers

 avatar  avatar

Watchers

 avatar

evalmimic's Issues

No support for 1.9.x

Same thing here as getsource, but in this case, I'm not sure if there's an easy workaround.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.